osdb 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Rakefile CHANGED
@@ -1,28 +1,9 @@
1
1
  require 'rubygems'
2
2
  require 'rake'
3
+ require 'rspec/core/rake_task'
4
+ require 'bundler'
3
5
 
4
- # TODO: update rspec to 2.X
5
- # require 'spec/rake/spectask'
6
- #
7
- # Spec::Rake::SpecTask.new(:spec) do |spec|
8
- # spec.libs << 'lib' << 'spec'
9
- # spec.spec_files = FileList['spec/**/*_spec.rb']
10
- # end
6
+ Bundler::GemHelper.install_tasks
7
+ RSpec::Core::RakeTask.new(:spec)
11
8
 
12
-
13
- begin
14
- require 'jeweler'
15
- Jeweler::Tasks.new do |gem|
16
- gem.name = "osdb"
17
- gem.summary = %Q{Ruby library to access OSDb services like OpenSubtitles.org}
18
- gem.email = "jean.boussier @nospam@ gmail.com"
19
- gem.homepage = "http://github.com/byroot/ruby-osdb"
20
- gem.authors = ["Jean Boussier"]
21
- gem.add_development_dependency "rspec", "~> 1.3"
22
- end
23
- Jeweler::GemcutterTasks.new
24
- rescue LoadError
25
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
26
- end
27
-
28
- task :default => :spec
9
+ task :default => :spec
data/bin/getsub CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
-
2
+ # TODO: getsub is becomming a bit to complexe, rewrite it OO or move it in another gem
3
3
  require 'optparse'
4
4
  require 'uri'
5
5
  require 'rubygems'
@@ -60,25 +60,54 @@ def group_by_movie_name(subs)
60
60
  end
61
61
 
62
62
  def ask_user_to_identify_movie(movies)
63
- puts "D'oh! You stumbled upon a hash conflict, please resolve it:"
64
- puts
65
63
  movies.keys.each_with_index do |name, index|
66
64
  puts " #{index} - #{name}"
67
65
  end
68
- puts
69
66
  print 'id: '
70
- str = STDIN.gets
67
+ str = STDIN.gets # TODO: rule #1, never trust user input
68
+ puts
71
69
  movies[movies.keys[str.to_i]]
72
70
  end
73
71
 
72
+ def identify_by_imdb!(server, movie)
73
+ puts "* could not find sub by hash, trying IMDB"
74
+ imdb_results = server.search_imdb(:query => movie.name)
75
+ if imdb_results.any?
76
+ if imdb_results.length == 1
77
+ imdb_result = imdb_results.first
78
+ puts "* found on IMDB with ID: #{imdb_result.imdbid}"
79
+ else
80
+ movies = Hash[imdb_results.map{ |r| [r.title, r] }]
81
+ imdb_result = ask_user_to_identify_movie(movies)
82
+ end
83
+ subs = server.search_subtitles(:sublanguageid => @options[:language], :imdbid => imdb_result.imdbid)
84
+ if subs.any?
85
+ select_and_download!(subs, movie)
86
+ else
87
+ puts "* no sub found for this movie"
88
+ end
89
+ else
90
+ puts "* no movie found on IMDB"
91
+ end
92
+ end
93
+
74
94
  def select_sub(subs)
75
- return subs.first if subs.length == 1
95
+ return subs.first if subs.length >= 1
76
96
  movies = group_by_movie_name(subs)
77
97
  return movies.values.first.max if movies.length == 1
98
+
99
+ puts "D'oh! You stumbled upon a hash conflict, please resolve it:"
100
+ puts
78
101
  selected_movie_subs = ask_user_to_identify_movie(movies)
79
102
  selected_movie_subs.max
80
103
  end
81
104
 
105
+ def select_and_download!(subs, movie)
106
+ sub = select_sub(subs)
107
+ sub_path = movie.sub_path(sub.format)
108
+ download!(sub.url, sub_path)
109
+ end
110
+
82
111
  def arg_files
83
112
  return ARGV unless @options[:dir]
84
113
  Dir.glob(File.join(@options[:dir], '**', "*.{#{OSDb::Movie::EXTENSIONS.join(',')}}"))
@@ -106,11 +135,9 @@ movies.each do |movie|
106
135
  puts "* search subs for: #{movie.path}"
107
136
  subs = server.search_subtitles(:moviehash => movie.hash, :moviebytesize => movie.size, :sublanguageid => @options[:language])
108
137
  if subs.any?
109
- sub = select_sub(subs)
110
- sub_path = movie.sub_path(sub.format)
111
- download!(sub.url, sub_path)
138
+ select_and_download!(subs, movie)
112
139
  else
113
- puts "* no sub found"
140
+ identify_by_imdb!(server, movie)
114
141
  end
115
142
  puts
116
143
  rescue Exception => e
data/lib/osdb/movie.rb CHANGED
@@ -26,6 +26,10 @@ module OSDb
26
26
  def size
27
27
  @size ||= File.size(path)
28
28
  end
29
+
30
+ def name
31
+ @name ||= File.basename(path, File.extname(path))
32
+ end
29
33
 
30
34
  CHUNK_SIZE = 64 * 1024 # in bytes
31
35
 
@@ -54,4 +58,4 @@ module OSDb
54
58
  end
55
59
 
56
60
  end
57
- end
61
+ end
data/lib/osdb/server.rb CHANGED
@@ -1,12 +1,14 @@
1
+ require 'ostruct'
2
+
1
3
  module OSDb
2
-
4
+
3
5
  class LoginFailed < ::Exception
4
6
  end
5
-
7
+
6
8
  class Server
7
-
9
+
8
10
  attr_reader :username, :password, :language, :useragent, :client
9
-
11
+
10
12
  def initialize(options={})
11
13
  @username = options[:username] || ''
12
14
  @password = options[:password] || ''
@@ -14,11 +16,11 @@ module OSDb
14
16
  @useragent = options[:useragent] || 'ruby-osdb v0.1'
15
17
  @client = ::XMLRPC::Client.new(*options.values_at(:host, :path, :port, :proxy_host, :proxy_port, :http_user, :http_password, :use_ssl, :timeout))
16
18
  end
17
-
19
+
18
20
  def token
19
21
  @token ||= login
20
22
  end
21
-
23
+
22
24
  def login
23
25
  response = client.call('LogIn', username, password, language, useragent)
24
26
  if response['status'] != '200 OK'
@@ -26,24 +28,30 @@ module OSDb
26
28
  end
27
29
  response['token']
28
30
  end
29
-
31
+
30
32
  def logout
31
33
  client.call('LogOut', token)
32
34
  @token = nil
33
35
  end
34
-
36
+
35
37
  def check_movie_hash(*hashes)
36
38
  client.call('CheckMovieHash', token, hashes)
37
39
  end
38
-
40
+
39
41
  def search_subtitles(*queries)
40
42
  subs = client.call('SearchSubtitles', token, queries)['data']
41
43
  subs ? subs.map{ |s| Sub.new(s) } : []
42
44
  end
43
-
45
+
46
+ def search_imdb(options={})
47
+ query = options.delete(:query)
48
+ imdb = client.call('SearchMoviesOnIMDB', token, query)['data']
49
+ imdb ? imdb.map{ |i| OpenStruct.new(:imdbid => i['id'], :title => i['title']) } : []
50
+ end
51
+
44
52
  def info
45
53
  client.call('ServerInfo')
46
54
  end
47
-
55
+
48
56
  end
49
- end
57
+ end
@@ -0,0 +1,3 @@
1
+ module OSDb
2
+ VERSION = '0.0.7'
3
+ end
data/osdb.gemspec CHANGED
@@ -1,54 +1,21 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "osdb/version"
5
4
 
6
5
  Gem::Specification.new do |s|
7
- s.name = %q{osdb}
8
- s.version = "0.0.6"
6
+ s.name = "osdb"
7
+ s.version = OSDb::VERSION
8
+ s.authors = ["Jean Boussier"]
9
+ s.email = ["jean.boussier @nospam@ gmail.com"]
10
+ s.homepage = %q{http://github.com/byroot/ruby-osdb}
11
+ s.summary = %q{Ruby library to access OSDb services like OpenSubtitles.org}
9
12
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Jean Boussier}]
12
- s.date = %q{2011-10-11}
13
- s.email = %q{jean.boussier @nospam@ gmail.com}
14
- s.executables = [%q{getsub}]
15
- s.extra_rdoc_files = [
16
- "README.md"
17
- ]
18
- s.files = [
19
- "Manifest",
20
- "README.md",
21
- "Rakefile",
22
- "VERSION",
23
- "bin/getsub",
24
- "lib/osdb.rb",
25
- "lib/osdb/language.rb",
26
- "lib/osdb/movie.rb",
27
- "lib/osdb/server.rb",
28
- "lib/osdb/sub.rb",
29
- "osdb.gemspec",
30
- "spec/fixtures/somemovie.avi",
31
- "spec/osdb/language_spec.rb",
32
- "spec/osdb/movie_spec.rb",
33
- "spec/osdb/server_spec.rb",
34
- "spec/osdb/sub_spec.rb",
35
- "spec/spec_helper.rb"
36
- ]
37
- s.homepage = %q{http://github.com/byroot/ruby-osdb}
38
- s.require_paths = [%q{lib}]
39
- s.rubygems_version = %q{1.8.6}
40
- s.summary = %q{Ruby library to access OSDb services like OpenSubtitles.org}
13
+ s.rubyforge_project = "osdb"
41
14
 
42
- if s.respond_to? :specification_version then
43
- s.specification_version = 3
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
44
19
 
45
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
46
- s.add_development_dependency(%q<rspec>, ["~> 1.3"])
47
- else
48
- s.add_dependency(%q<rspec>, ["~> 1.3"])
49
- end
50
- else
51
- s.add_dependency(%q<rspec>, ["~> 1.3"])
52
- end
20
+ s.add_development_dependency 'rspec'
53
21
  end
54
-
@@ -7,5 +7,6 @@ describe OSDb::Movie do
7
7
  end
8
8
 
9
9
  its(:hash) { should == '243339b48f4e8741' }
10
+ its(:name) { should == 'somemovie' }
10
11
 
11
- end
12
+ end
@@ -1,44 +1,51 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
2
2
 
3
3
  describe OSDb::Server do
4
-
4
+
5
5
  before :all do
6
6
  @server = OSDb::Server.new(
7
- :host => 'api.opensubtitles.org',
7
+ :host => 'api.opensubtitles.org',
8
8
  :path => '/xml-rpc',
9
9
  :timeout => 60, # OS.org is very very slow ....
10
10
  :useragent => 'OS Test User Agent'
11
11
  )
12
12
  end
13
-
13
+
14
14
  subject { @server }
15
-
16
- it 'should respond to #info' do
17
- hash_including(
18
- "seconds" => instance_of(Float),
19
- "last_update_strings" => instance_of(Hash),
15
+
16
+ let(:expected_info) do
17
+ {
20
18
  "website_url"=>"http://www.opensubtitles.org",
21
19
  "contact"=>"admin@opensubtitles.org",
22
20
  "application"=>"OpenSuber v0.2",
23
21
  "xmlrpc_version"=>"0.1",
24
- "xmlrpc_url"=>"http://api.opensubtitles.net/xml-rpc"
25
- ).should == subject.info
22
+ "xmlrpc_url"=>"http://api.opensubtitles.org/xml-rpc"
23
+ }
26
24
  end
27
-
25
+
26
+ it 'should respond to #info' do
27
+ info = subject.info
28
+ expected_info.each do |key, value|
29
+ info[key].should == value
30
+ end
31
+ info['seconds'].should be_a(Float)
32
+ info['last_update_strings'].should be_a(Hash)
33
+ end
34
+
28
35
  it 'should automatically call #login when token is needed' do
29
36
  subject.instance_variable_get('@token').should be_nil
30
37
  subject.token.should match(/[a-z0-9]{26}/)
31
38
  subject.instance_variable_get('@token').should == subject.token
32
39
  end
33
-
40
+
34
41
  it 'should clear @login after #logout' do
35
42
  expect{
36
43
  subject.logout
37
44
  }.to change{ subject.instance_variable_get('@token') }.from(instance_of(String)).to(nil)
38
45
  end
39
-
46
+
40
47
  describe "#check_movie_hash" do
41
-
48
+
42
49
  it 'should identify movie' do
43
50
  subject.check_movie_hash('37d0c7d0cfcbe280')['data'].should == {
44
51
  "37d0c7d0cfcbe280" => {
@@ -49,11 +56,11 @@ describe OSDb::Server do
49
56
  }
50
57
  }
51
58
  end
52
-
59
+
53
60
  end
54
-
61
+
55
62
  describe '#search_subtitles' do
56
-
63
+
57
64
  it 'can search by hash and size' do
58
65
  subs = subject.search_subtitles(:moviehash => 'bd71526264fd8bd9', :moviebytesize => '183406990', :sublanguageid => 'fre')
59
66
  subs.should be_a(Array)
@@ -63,7 +70,7 @@ describe OSDb::Server do
63
70
  sub.raw_data['MovieName'].should == 'How I Met Your Mother'
64
71
  end
65
72
  end
66
-
73
+
67
74
  it 'can search by imdbid' do
68
75
  subs = subject.search_subtitles(:imdbid => "0117500", :sublanguageid => 'fre')
69
76
  subs.should be_a(Array)
@@ -73,7 +80,18 @@ describe OSDb::Server do
73
80
  sub.raw_data['MovieName'].should == 'The Rock'
74
81
  end
75
82
  end
76
-
83
+
77
84
  end
78
-
79
- end
85
+
86
+ describe "#search_imdb" do
87
+ it "can search imdb by title" do
88
+ imdb = subject.search_imdb(:query => "How I Met Your Mother")
89
+ imdb.length.should == 1
90
+ imdb.each do |movie|
91
+ movie.imdbid.should == '0460649'
92
+ movie.title.should == 'How I Met Your Mother'
93
+ end
94
+ end
95
+ end
96
+
97
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  require File.dirname(__FILE__) + '/../lib/osdb'
2
- require 'spec/autorun'
2
+ require 'rspec'
3
3
 
4
- # Spec Helpers
5
- Dir[File.dirname(__FILE__) + '/*_spec_helper.rb'].each do |f|
6
- require File.expand_path(f)
7
- end
4
+ RSpec.configure do |c|
5
+ c.mock_with :rspec
6
+ end
metadata CHANGED
@@ -1,36 +1,47 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: osdb
3
- version: !ruby/object:Gem::Version
4
- version: 0.0.6
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 7
9
+ version: 0.0.7
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Jean Boussier
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2011-10-11 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2011-10-17 00:00:00 +02:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: rspec
16
- requirement: &70177107254940 !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ~>
20
- - !ruby/object:Gem::Version
21
- version: '1.3'
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: *70177107254940
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
25
32
  description:
26
- email: jean.boussier @nospam@ gmail.com
27
- executables:
33
+ email:
34
+ - jean.boussier @nospam@ gmail.com
35
+ executables:
28
36
  - getsub
29
37
  extensions: []
30
- extra_rdoc_files:
31
- - README.md
32
- files:
33
- - Manifest
38
+
39
+ extra_rdoc_files: []
40
+
41
+ files:
42
+ - .gitignore
43
+ - .rspec
44
+ - Gemfile
34
45
  - README.md
35
46
  - Rakefile
36
47
  - VERSION
@@ -40,6 +51,7 @@ files:
40
51
  - lib/osdb/movie.rb
41
52
  - lib/osdb/server.rb
42
53
  - lib/osdb/sub.rb
54
+ - lib/osdb/version.rb
43
55
  - osdb.gemspec
44
56
  - spec/fixtures/somemovie.avi
45
57
  - spec/osdb/language_spec.rb
@@ -47,28 +59,40 @@ files:
47
59
  - spec/osdb/server_spec.rb
48
60
  - spec/osdb/sub_spec.rb
49
61
  - spec/spec_helper.rb
62
+ has_rdoc: true
50
63
  homepage: http://github.com/byroot/ruby-osdb
51
64
  licenses: []
65
+
52
66
  post_install_message:
53
67
  rdoc_options: []
54
- require_paths:
68
+
69
+ require_paths:
55
70
  - lib
56
- required_ruby_version: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- required_rubygems_version: !ruby/object:Gem::Requirement
63
- none: false
64
- requirements:
65
- - - ! '>='
66
- - !ruby/object:Gem::Version
67
- version: '0'
71
+ required_ruby_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ segments:
83
+ - 0
84
+ version: "0"
68
85
  requirements: []
69
- rubyforge_project:
70
- rubygems_version: 1.8.6
86
+
87
+ rubyforge_project: osdb
88
+ rubygems_version: 1.3.6
71
89
  signing_key:
72
90
  specification_version: 3
73
91
  summary: Ruby library to access OSDb services like OpenSubtitles.org
74
- test_files: []
92
+ test_files:
93
+ - spec/fixtures/somemovie.avi
94
+ - spec/osdb/language_spec.rb
95
+ - spec/osdb/movie_spec.rb
96
+ - spec/osdb/server_spec.rb
97
+ - spec/osdb/sub_spec.rb
98
+ - spec/spec_helper.rb
data/Manifest DELETED
@@ -1,16 +0,0 @@
1
- Manifest
2
- README.md
3
- Rakefile
4
- bin/getsub
5
- lib/osdb.rb
6
- lib/osdb/language.rb
7
- lib/osdb/movie.rb
8
- lib/osdb/server.rb
9
- lib/osdb/sub.rb
10
- osdb.gemspec
11
- spec/fixtures/somemovie.avi
12
- spec/osdb/language_spec.rb
13
- spec/osdb/movie_spec.rb
14
- spec/osdb/server_spec.rb
15
- spec/osdb/sub_spec.rb
16
- spec/spec_helper.rb