subdb 0.0.1 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/bin/subdb ADDED
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "rubygems"
4
+ require "bundler/setup"
5
+
6
+ $: << File.expand_path("../../lib", __FILE__)
7
+
8
+ require "subdb"
9
+
10
+ files = []
11
+ video_ext = Subdb::VIDEO_EXTENSIONS.join(",")
12
+
13
+ $*.each do |path|
14
+ if File.directory?(path)
15
+ path = path.chomp("/")
16
+ files = files.concat(Dir.glob("#{path}/**/*{#{video_ext}}"))
17
+ else
18
+ files << path if Subdb::VIDEO_EXTENSIONS.include?(File.extname(path))
19
+ end
20
+ end
21
+
22
+ files = files.sort
23
+
24
+ languages = ["pt", "en"]
25
+ i = 1
26
+
27
+ for path in files
28
+ base = File.dirname(path) + "/" + File.basename(path, File.extname(path))
29
+ sub = nil
30
+
31
+ for subext in Subdb::SUB_EXTESNIONS
32
+ subpath = base + subext
33
+
34
+ if File.exists?(subpath)
35
+ sub = subpath
36
+ break
37
+ end
38
+ end
39
+
40
+ puts "Scanning #{path} [#{i}/#{files.length}]"
41
+
42
+ begin
43
+ subdb = Subdb.new(path)
44
+ puts "Hash: #{subdb.hash}"
45
+
46
+ remote = subdb.search
47
+ puts (remote ? "Found with languages: #{remote}" : "No subtitle found")
48
+
49
+ if sub and !remote
50
+ puts "Local subtitle found and none on server, uploading..."
51
+
52
+ begin
53
+ subdb.upload(sub)
54
+ puts "Upload completed"
55
+ rescue
56
+ puts "Error on upload: #{$!}"
57
+ end
58
+ end
59
+
60
+ if !sub and remote
61
+ puts "downloading from remote"
62
+
63
+ begin
64
+ downloaded = subdb.download(languages)
65
+
66
+ if downloaded
67
+ File.open(base + ".srt", "w") do |f|
68
+ f << downloaded
69
+ end
70
+
71
+ puts "Download done ok"
72
+ else
73
+ puts "No version for your languages"
74
+ end
75
+ rescue
76
+ puts "Error on download: #{$!}"
77
+ end
78
+ end
79
+ rescue
80
+ puts "Can't open: #{$!}"
81
+ end
82
+
83
+ puts
84
+
85
+ i += 1
86
+ end
data/lib/subdb.rb CHANGED
@@ -19,7 +19,13 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
+ require 'net/http'
23
+ require 'uri'
24
+ require 'cgi'
22
25
  require 'digest/md5'
26
+ require 'net/http/post/multipart'
27
+
28
+ require 'subdb/version'
23
29
 
24
30
  class Subdb
25
31
  VIDEO_EXTENSIONS = ['.avi', '.mkv', '.mp4', '.mov', '.mpg', '.wmv', '.rm', '.rmvb', '.divx']
@@ -41,12 +47,52 @@ class Subdb
41
47
  attr_reader :hash
42
48
 
43
49
  def initialize(path)
44
- fail "#{@path} is not a file" unless File.exists?(path)
50
+ fail "#{path} is not a file" unless File.exists?(path)
45
51
 
46
52
  @path = path
47
53
  @hash = build_hash
48
54
  end
49
55
 
56
+ def search
57
+ res = request("search")
58
+ check_get(res)
59
+ end
60
+
61
+ def download(languages = ["en"])
62
+ res = request("download", :language => languages.join(","))
63
+ check_get(res)
64
+ end
65
+
66
+ def upload(path)
67
+ fail "Invalid subtitle file #{path}" unless File.exists?(path)
68
+
69
+ params = {:action => "upload", :hash => @hash}
70
+
71
+ url = URI.parse(self.class.api_url)
72
+
73
+ begin
74
+ file = File.open(path, "r")
75
+
76
+ io = UploadIO.new(file, "application/octet-stream", File.basename(path))
77
+
78
+ req = Net::HTTP::Post::Multipart.new(url.path + stringify_params(params), {"file" => io, "hash" => @hash})
79
+ req["User-Agent"] = user_agent
80
+
81
+ res = Net::HTTP.start(url.host, url.port) do |http|
82
+ http.request(req)
83
+ end
84
+
85
+ case res.code.to_s
86
+ when "201" then true
87
+ when "403" then false
88
+ when "400" then fail "Malformed request"
89
+ when "415" then fail "Invalid subtitle type"
90
+ end
91
+ ensure
92
+ file.close
93
+ end
94
+ end
95
+
50
96
  protected
51
97
 
52
98
  def build_hash
@@ -58,6 +104,51 @@ class Subdb
58
104
  file.seek(size - chunk_size)
59
105
  data += file.read(chunk_size)
60
106
 
107
+ file.close
108
+
61
109
  Digest::MD5.hexdigest(data)
62
110
  end
111
+
112
+ def user_agent
113
+ "SubDB/1.0 (RubySubDB/#{VERSION}; http://github.com/wilkerlucio/subdb)"
114
+ end
115
+
116
+ def request(action, params = {}, body = nil)
117
+ params = {:action => action, :hash => @hash}.merge(params)
118
+
119
+ url = URI.parse(self.class.api_url)
120
+
121
+ req = Net::HTTP::Get.new(url.path + stringify_params(params))
122
+ req["User-Agent"] = user_agent
123
+ req.set_form_data(body) if body
124
+
125
+ Net::HTTP.start(url.host, url.port) do |http|
126
+ http.request(req)
127
+ end
128
+ end
129
+
130
+ def check_get(res)
131
+ case res.code.to_s
132
+ when "200" then res.body
133
+ when "400" then fail "Malformed request"
134
+ when "404" then nil
135
+ else
136
+ fail "Unexpected response code - #{res.code}"
137
+ end
138
+ end
139
+
140
+ def stringify_params(params)
141
+ params_string = []
142
+
143
+ params.each do |key, value|
144
+ next unless value
145
+
146
+ key = CGI.escape(key.to_s)
147
+ value = CGI.escape(value.to_s)
148
+
149
+ params_string << "#{key}=#{value}"
150
+ end
151
+
152
+ params_string.length.zero? ? "" : "?" + params_string.join("&")
153
+ end
63
154
  end
@@ -19,7 +19,6 @@
19
19
  # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
20
  # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
21
 
22
- require 'test/unit'
23
- require 'subdb'
24
-
25
- Subdb.test_mode = true
22
+ class Subdb
23
+ VERSION = "0.1.0"
24
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: subdb
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 0
9
8
  - 1
10
- version: 0.0.1
9
+ - 0
10
+ version: 0.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Wilker Lucio
@@ -17,35 +17,47 @@ cert_chain: []
17
17
 
18
18
  date: 2011-04-19 00:00:00 -03:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: multipart-post
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 0
34
+ version: 1.1.0
35
+ type: :runtime
36
+ version_requirements: *id001
22
37
  description: API for SubDB
23
- email: wilkerlucio@gmail.com
24
- executables: []
25
-
38
+ email:
39
+ - wilkerlucio@gmail.com
40
+ executables:
41
+ - subdb
26
42
  extensions: []
27
43
 
28
- extra_rdoc_files:
29
- - LICENSE
30
- - README.textile
44
+ extra_rdoc_files: []
45
+
31
46
  files:
32
- - .gitignore
47
+ - bin/subdb
48
+ - lib/subdb/version.rb
49
+ - lib/subdb.rb
33
50
  - LICENSE
34
51
  - README.textile
35
- - Rakefile
36
- - lib/subdb.rb
37
- - subdb.gemspec
38
- - test/test_helper.rb
39
- - test/subdb_test.rb
40
52
  has_rdoc: true
41
- homepage: http://github.com/wilkerlucio/mongoid_taggable
53
+ homepage: http://github.com/wilkerlucio/subdb
42
54
  licenses: []
43
55
 
44
56
  post_install_message:
45
- rdoc_options:
46
- - --charset=UTF-8
57
+ rdoc_options: []
58
+
47
59
  require_paths:
48
- - lib
60
+ - - lib
49
61
  required_ruby_version: !ruby/object:Gem::Requirement
50
62
  none: false
51
63
  requirements:
@@ -71,6 +83,5 @@ rubygems_version: 1.3.7
71
83
  signing_key:
72
84
  specification_version: 3
73
85
  summary: SubDB Ruby API
74
- test_files:
75
- - test/test_helper.rb
76
- - test/subdb_test.rb
86
+ test_files: []
87
+
data/.gitignore DELETED
@@ -1 +0,0 @@
1
- .DS_Store
data/Rakefile DELETED
@@ -1,16 +0,0 @@
1
- task :default => [:test]
2
-
3
- desc "Download fixture files for tests"
4
- task :download_fixtures do
5
- puts "need implementation"
6
- end
7
-
8
- desc "Run tests"
9
- task :test do
10
- $: << File.expand_path("../lib", __FILE__)
11
- $: << File.expand_path("../test", __FILE__)
12
-
13
- Dir["test/**/*_test.rb"].each do |test|
14
- require test
15
- end
16
- end
data/subdb.gemspec DELETED
@@ -1,40 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = %q{subdb}
3
- s.version = "0.0.1"
4
-
5
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
- s.authors = ["Wilker Lucio"]
7
- s.date = %q{2011-04-19}
8
- s.description = %q{API for SubDB}
9
- s.email = %q{wilkerlucio@gmail.com}
10
- s.extra_rdoc_files = [
11
- "LICENSE",
12
- "README.textile"
13
- ]
14
- s.files = [
15
- ".gitignore",
16
- "LICENSE",
17
- "README.textile",
18
- "Rakefile",
19
- "lib/subdb.rb",
20
- "subdb.gemspec",
21
- ]
22
- s.homepage = %q{http://github.com/wilkerlucio/mongoid_taggable}
23
- s.rdoc_options = ["--charset=UTF-8"]
24
- s.require_paths = ["lib"]
25
- s.rubygems_version = %q{1.7.2}
26
- s.summary = %q{SubDB Ruby API}
27
- s.test_files = [
28
- "test/test_helper.rb",
29
- "test/subdb_test.rb"
30
- ]
31
-
32
- if s.respond_to? :specification_version then
33
- s.specification_version = 3
34
-
35
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
36
- else
37
- end
38
- else
39
- end
40
- end
data/test/subdb_test.rb DELETED
@@ -1,55 +0,0 @@
1
- # Copyright (c) 2011 Wilker Lucio da Silva
2
- #
3
- # Permission is hereby granted, free of charge, to any person obtaining
4
- # a copy of this software and associated documentation files (the
5
- # "Software"), to deal in the Software without restriction, including
6
- # without limitation the rights to use, copy, modify, merge, publish,
7
- # distribute, sublicense, and/or sell copies of the Software, and to
8
- # permit persons to whom the Software is furnished to do so, subject to
9
- # the following conditions:
10
- #
11
- # The above copyright notice and this permission notice shall be
12
- # included in all copies or substantial portions of the Software.
13
- #
14
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
- # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
- # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
- # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
- # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
- # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
- # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
-
22
- require 'test_helper'
23
-
24
- class SubdbTest < Test::Unit::TestCase
25
- TEST_FILES = {
26
- :dexter => {
27
- :path => File.expand_path("../fixtures/dexter.mp4", __FILE__),
28
- :hash => "ffd8d4aa68033dc03d1c8ef373b9028c"
29
- },
30
-
31
- :justified => {
32
- :path => File.expand_path("../fixtures/justified.mp4", __FILE__),
33
- :hash => "edc1981d6459c6111fe36205b4aff6c2"
34
- }
35
- }
36
-
37
- def test_self_api_url
38
- Subdb.test_mode = false
39
- assert_equal("http://api.thesubdb.com/", Subdb.api_url)
40
-
41
- Subdb.test_mode = true
42
- assert_equal("http://sandbox.thesubdb.com/", Subdb.api_url)
43
- end
44
-
45
- def test_initialize_with_invalid_file
46
- assert_raise(RuntimeError) { Subdb.new("invalid") }
47
- end
48
-
49
- def test_file_hash
50
- TEST_FILES.each do |name, file|
51
- sub = Subdb.new(file[:path])
52
- assert_equal(file[:hash], sub.hash)
53
- end
54
- end
55
- end