imdb 0.6.8 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +18 -0
  4. data/Gemfile +1 -1
  5. data/README.rdoc +21 -0
  6. data/Rakefile +2 -11
  7. data/imdb.gemspec +4 -4
  8. data/lib/imdb.rb +4 -0
  9. data/lib/imdb/base.rb +191 -0
  10. data/lib/imdb/episode.rb +23 -0
  11. data/lib/imdb/movie.rb +1 -169
  12. data/lib/imdb/movie_list.rb +6 -7
  13. data/lib/imdb/search.rb +9 -9
  14. data/lib/imdb/season.rb +36 -0
  15. data/lib/imdb/serie.rb +23 -0
  16. data/lib/imdb/version.rb +1 -1
  17. data/spec/fixtures/plotsummary +975 -0
  18. data/spec/fixtures/search_kannethirey_thondrinal +773 -10
  19. data/spec/fixtures/search_killed_wife +771 -10
  20. data/spec/fixtures/search_star_trek +705 -762
  21. data/spec/fixtures/synopsis +1036 -0
  22. data/spec/fixtures/thewalkingdead-s1 +1295 -0
  23. data/spec/fixtures/thewalkingdead-s1e2 +1232 -0
  24. data/spec/fixtures/top_250 +470 -754
  25. data/spec/fixtures/tt0036855 +541 -366
  26. data/spec/fixtures/tt0083987 +553 -368
  27. data/spec/fixtures/tt0095016 +572 -394
  28. data/spec/fixtures/tt0110912 +578 -376
  29. data/spec/fixtures/tt0111161 +549 -388
  30. data/spec/fixtures/tt0117731 +534 -387
  31. data/spec/fixtures/tt0166222 +1795 -1694
  32. data/spec/fixtures/tt0242653 +544 -352
  33. data/spec/fixtures/tt0330508 +1553 -1474
  34. data/spec/fixtures/tt0468569 +603 -413
  35. data/spec/fixtures/tt1401252 +446 -381
  36. data/spec/fixtures/tt1520211 +1460 -0
  37. data/spec/imdb/cli_spec.rb +7 -7
  38. data/spec/imdb/episode_spec.rb +39 -0
  39. data/spec/imdb/movie_spec.rb +32 -22
  40. data/spec/imdb/search_spec.rb +16 -36
  41. data/spec/imdb/season_spec.rb +19 -0
  42. data/spec/imdb/series_spec.rb +21 -0
  43. data/spec/spec_helper.rb +9 -10
  44. metadata +92 -41
  45. data/spec/spec.opts +0 -1
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ca782282c0207181d57c8732b2028c46e91854fa
4
+ data.tar.gz: 93857f4686f71950e2bbb514805e87ab0c075de4
5
+ SHA512:
6
+ metadata.gz: b64b0288a5ee43eced42d0fe5ebbd6d4f9d4057d3941924f08399e8235a3467d4f950c5186e58b1f1deee95f90413b74b2dc9cfa295f9763c0f7e33ccf093618
7
+ data.tar.gz: 423bb452e2c11f1c74925c5ff591cc1fb11ed197242f65284166e6f2b9864106efcfcdb49648ec0064e313d3b4f697cfbd24b8fe399ed74206eee08f9054a1e9
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ notifications:
4
+ email: false
5
+
6
+ rvm:
7
+ - 1.9.2
8
+ - 1.9.3
9
+ - ruby-head
10
+ - rbx-19mode
11
+
12
+ matrix:
13
+ allow_failures:
14
+ - rvm: rbx-19mode
15
+
16
+ script:
17
+ - "bundle exec rake spec"
18
+
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
 
3
3
  gemspec
data/README.rdoc CHANGED
@@ -1,5 +1,7 @@
1
1
  = imdb
2
2
 
3
+ {<img src="https://travis-ci.org/ariejan/imdb.png?branch=master" alt="Build Status" />}[https://travis-ci.org/ariejan/imdb]
4
+
3
5
  home :: http://github.com/ariejan/imdb
4
6
  rdoc :: http://ariejan.github.com/imdb/
5
7
  bugs :: http://github.com/ariejan/imdb/issues
@@ -27,6 +29,25 @@ Movies:
27
29
  i.cast_members.first
28
30
  #=> "Bruce Willis"
29
31
 
32
+ Series:
33
+
34
+ serie = Imdb::Serie.new("1520211")
35
+
36
+ serie.title
37
+ #=> "\"The Walking Dead\""
38
+
39
+ serie.rating
40
+ #=> 8.8
41
+
42
+ serie.seasons.size
43
+ #=> 3
44
+
45
+ serie.seaon(1).episodes.size
46
+ #=> 6
47
+
48
+ series.season(1).episode(2).title
49
+ #=> "Guts"
50
+
30
51
  Searching:
31
52
 
32
53
  i = Imdb::Search.new("Star Trek")
data/Rakefile CHANGED
@@ -3,17 +3,8 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  load File.expand_path(File.dirname(__FILE__) + "/tasks/fixtures.rake")
5
5
 
6
- require 'spec/rake/spectask'
7
- Spec::Rake::SpecTask.new(:spec) do |spec|
8
- spec.libs << 'lib' << 'spec'
9
- spec.spec_files = FileList['spec/**/*_spec.rb']
10
- end
11
-
12
- Spec::Rake::SpecTask.new(:rcov) do |spec|
13
- spec.libs << 'lib' << 'spec'
14
- spec.pattern = 'spec/**/*_spec.rb'
15
- spec.rcov = true
16
- end
6
+ require 'rspec/core/rake_task'
7
+ RSpec::Core::RakeTask.new(:spec)
17
8
 
18
9
  task :default => :spec
19
10
 
data/imdb.gemspec CHANGED
@@ -19,11 +19,11 @@ Gem::Specification.new do |s|
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
21
 
22
- s.add_dependency 'hpricot', '~> 0.8.4'
22
+ s.add_dependency 'hpricot', '~> 0.8.6'
23
23
 
24
- s.add_development_dependency 'rake', '~> 0.9.2'
25
- s.add_development_dependency 'rspec', '~> 1.3.2'
24
+ s.add_development_dependency 'rake', '~> 10.0.3'
25
+ s.add_development_dependency 'rspec', '~> 2.13.0'
26
26
  s.add_development_dependency 'gokdok'
27
- s.add_development_dependency 'rdoc', '~> 3.11'
27
+ s.add_development_dependency 'rdoc', '~> 4.0'
28
28
  s.add_development_dependency 'fakeweb'
29
29
  end
data/lib/imdb.rb CHANGED
@@ -5,7 +5,11 @@ require 'open-uri'
5
5
  require 'rubygems'
6
6
  require 'hpricot'
7
7
 
8
+ require 'imdb/base'
8
9
  require 'imdb/movie'
10
+ require 'imdb/serie'
11
+ require 'imdb/season'
12
+ require 'imdb/episode'
9
13
  require 'imdb/movie_list'
10
14
  require 'imdb/search'
11
15
  require 'imdb/top_250'
data/lib/imdb/base.rb ADDED
@@ -0,0 +1,191 @@
1
+ module Imdb
2
+
3
+ # Represents something on IMDB.com
4
+ class Base
5
+ attr_accessor :id, :url, :title, :also_known_as
6
+
7
+ # Initialize a new IMDB movie object with it's IMDB id (as a String)
8
+ #
9
+ # movie = Imdb::Movie.new("0095016")
10
+ #
11
+ # Imdb::Movie objects are lazy loading, meaning that no HTTP request
12
+ # will be performed when a new object is created. Only when you use an
13
+ # accessor that needs the remote data, a HTTP request is made (once).
14
+ #
15
+ def initialize(imdb_id, title = nil)
16
+ @id = imdb_id
17
+ @url = "http://akas.imdb.com/title/tt#{imdb_id}/combined"
18
+ @title = title.gsub(/"/, "").strip if title
19
+ end
20
+
21
+ # Returns an array with cast members
22
+ def cast_members
23
+ document.search("table.cast td.nm a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
24
+ end
25
+
26
+ def cast_member_ids
27
+ document.search("table.cast td.nm a").map {|l| l['href'].sub(%r{^/name/(.*)/}, '\1') }
28
+ end
29
+
30
+ # Returns an array with cast characters
31
+ def cast_characters
32
+ document.search("table.cast td.char").map { |link| link.innerText } rescue []
33
+ end
34
+
35
+ # Returns an array with cast members and characters
36
+ def cast_members_characters(sep = '=>')
37
+ memb_char = Array.new
38
+ i = 0
39
+ self.cast_members.each{|m|
40
+ memb_char[i] = "#{self.cast_members[i]} #{sep} #{self.cast_characters[i]}"
41
+ i=i+1
42
+ }
43
+ return memb_char
44
+ end
45
+
46
+ # Returns the name of the director
47
+ def director
48
+ document.search("h5[text()^='Director'] ~ a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
49
+ end
50
+
51
+ # Returns the url to the "Watch a trailer" page
52
+ def trailer_url
53
+ 'http://imdb.com' + document.at("a[@href*=/video/screenplay/]")["href"] rescue nil
54
+ end
55
+
56
+ # Returns an array of genres (as strings)
57
+ def genres
58
+ document.search("h5[text()='Genre:'] ~ a[@href*=/Sections/Genres/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
59
+ end
60
+
61
+ # Returns an array of languages as strings.
62
+ def languages
63
+ document.search("h5[text()='Language:'] ~ a[@href*=/language/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
64
+ end
65
+
66
+ # Returns an array of countries as strings.
67
+ def countries
68
+ document.search("h5[text()='Country:'] ~ a[@href*=/country/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
69
+ end
70
+
71
+ # Returns the duration of the movie in minutes as an integer.
72
+ def length
73
+ document.search("//h5[text()='Runtime:']/..").innerHTML[/\d+ min/].to_i rescue nil
74
+ end
75
+
76
+ # Returns the company
77
+ def company
78
+ document.search("h5[text()='Company:'] ~ a[@href*=/company/']").map { |link| link.innerHTML.strip.imdb_unescape_html }.first rescue nil
79
+ end
80
+
81
+ # Returns a string containing the plot.
82
+ def plot
83
+ sanitize_plot(document.search("h5[text()='Plot:'] ~ div").first.innerHTML) rescue nil
84
+ end
85
+
86
+ # Returns a string containing the plot summary
87
+ def plot_synopsis
88
+ doc = Hpricot(Imdb::Movie.find_by_id(@id, :synopsis))
89
+ doc.search("div[@id='swiki.2.1']").innerHTML.strip.imdb_unescape_html.imdb_strip_tags rescue nil
90
+ end
91
+
92
+ def plot_summary
93
+ doc = Hpricot(Imdb::Movie.find_by_id(@id, :plotsummary))
94
+ doc.search("p[@class='plotpar']").first.innerHTML.gsub(/<i.*/im, '').strip.imdb_unescape_html rescue nil
95
+ end
96
+
97
+ # Returns a string containing the URL to the movie poster.
98
+ def poster
99
+ src = document.at("a[@name='poster'] img")['src'] rescue nil
100
+ case src
101
+ when /^(http:.+@@)/
102
+ $1 + '.jpg'
103
+ when /^(http:.+?)\.[^\/]+$/
104
+ $1 + '.jpg'
105
+ end
106
+ end
107
+
108
+ # Returns a float containing the average user rating
109
+ def rating
110
+ document.at(".starbar-meta b").innerHTML.strip.imdb_unescape_html.split('/').first.to_f rescue nil
111
+ end
112
+
113
+ # Returns an int containing the number of user ratings
114
+ def votes
115
+ document.at("#tn15rating .tn15more").innerHTML.strip.imdb_unescape_html.gsub(/[^\d+]/, "").to_i rescue nil
116
+ end
117
+
118
+ # Returns a string containing the tagline
119
+ def tagline
120
+ document.search("h5[text()='Tagline:'] ~ div").first.innerHTML.gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil
121
+ end
122
+
123
+ # Returns a string containing the mpaa rating and reason for rating
124
+ def mpaa_rating
125
+ document.search("h5[text()='MPAA:'] ~ div").first.innerHTML.strip.imdb_unescape_html rescue nil
126
+ end
127
+
128
+ # Returns a string containing the title
129
+ def title(force_refresh = false)
130
+ if @title && !force_refresh
131
+ @title
132
+ else
133
+ @title = document.at("h1").innerHTML.split('<span').first.strip.imdb_unescape_html rescue nil
134
+ end
135
+ end
136
+
137
+ # Returns an integer containing the year (CCYY) the movie was released in.
138
+ def year
139
+ document.search('a[@href^="/year/"]').innerHTML.to_i
140
+ end
141
+
142
+ # Returns release date for the movie.
143
+ def release_date
144
+ sanitize_release_date(document.search('h5[text()*=Release Date]').first.next_sibling.innerHTML.to_s) rescue nil
145
+ end
146
+
147
+ private
148
+
149
+ # Returns a new Hpricot document for parsing.
150
+ def document
151
+ @document ||= Hpricot(Imdb::Movie.find_by_id(@id))
152
+ end
153
+
154
+ # Use HTTParty to fetch the raw HTML for this movie.
155
+ def self.find_by_id(imdb_id, page = :combined)
156
+ open("http://akas.imdb.com/title/tt#{imdb_id}/#{page}")
157
+ end
158
+
159
+ # Convenience method for search
160
+ def self.search(query)
161
+ Imdb::Search.new(query).movies
162
+ end
163
+
164
+ def self.top_250
165
+ Imdb::Top250.new.movies
166
+ end
167
+
168
+ def sanitize_plot(the_plot)
169
+ the_plot = the_plot.imdb_strip_tags
170
+
171
+ the_plot = the_plot.gsub(/add\ssummary|full\ssummary/i, "")
172
+ the_plot = the_plot.gsub(/add\ssynopsis|full\ssynopsis/i, "")
173
+ the_plot = the_plot.gsub(/&nbsp;|&raquo;/i, "")
174
+ the_plot = the_plot.gsub(/see|more/i, "")
175
+ the_plot = the_plot.gsub(/\|/i, "")
176
+
177
+ the_plot = the_plot.strip.imdb_unescape_html
178
+ end
179
+
180
+ def sanitize_release_date(the_release_date)
181
+ the_release_date = the_release_date.gsub(/<a.*a>/,"")
182
+ the_release_date = the_release_date.gsub(/&nbsp;|&raquo;/i, "")
183
+ the_release_date = the_release_date.gsub(/see|more/i, "")
184
+
185
+ the_release_date = the_release_date.strip.imdb_unescape_html
186
+ end
187
+
188
+ end # Movie
189
+
190
+ end # Imdb
191
+
@@ -0,0 +1,23 @@
1
+ module Imdb
2
+ class Episode < Base
3
+ attr_accessor :season, :episode, :episode_title
4
+
5
+ def initialize(imdb_id, season, episode, episode_title)
6
+ super(imdb_id, episode_title)
7
+ @url = "http://akas.imdb.com/title/tt#{imdb_id}/combined"
8
+ @season = season
9
+ @episode = episode
10
+ end
11
+
12
+ # Return the original air date for this episode
13
+ def air_date
14
+ document.search('h5[text()*=Original Air Date]').first.next_sibling.innerHTML.to_s.strip.split("\n").first.strip rescue nil
15
+ end
16
+
17
+ private
18
+
19
+ def document
20
+ @document ||= Hpricot(open(@url))
21
+ end
22
+ end
23
+ end
data/lib/imdb/movie.rb CHANGED
@@ -1,175 +1,7 @@
1
1
  module Imdb
2
2
 
3
3
  # Represents a Movie on IMDB.com
4
- class Movie
5
- attr_accessor :id, :url, :title, :also_known_as
6
-
7
- # Initialize a new IMDB movie object with it's IMDB id (as a String)
8
- #
9
- # movie = Imdb::Movie.new("0095016")
10
- #
11
- # Imdb::Movie objects are lazy loading, meaning that no HTTP request
12
- # will be performed when a new object is created. Only when you use an
13
- # accessor that needs the remote data, a HTTP request is made (once).
14
- #
15
- def initialize(imdb_id, title = nil, also_known_as = [])
16
- @id = imdb_id
17
- @url = "http://akas.imdb.com/title/tt#{imdb_id}/combined"
18
- @title = title.gsub(/"/, "") if title
19
- @also_known_as = also_known_as
20
- end
21
-
22
- # Returns an array with cast members
23
- def cast_members
24
- document.search("table.cast td.nm a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
25
- end
26
-
27
- def cast_member_ids
28
- document.search("table.cast td.nm a").map {|l| l['href'].sub(%r{^/name/(.*)/}, '\1') }
29
- end
30
-
31
- # Returns an array with cast characters
32
- def cast_characters
33
- document.search("table.cast td.char").map { |link| link.innerText } rescue []
34
- end
35
-
36
- # Returns an array with cast members and characters
37
- def cast_members_characters(sep = '=>')
38
- memb_char = Array.new
39
- i = 0
40
- self.cast_members.each{|m|
41
- memb_char[i] = "#{self.cast_members[i]} #{sep} #{self.cast_characters[i]}"
42
- i=i+1
43
- }
44
- return memb_char
45
- end
46
-
47
- # Returns the name of the director
48
- def director
49
- document.search("h5[text()^='Director'] ~ a").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
50
- end
51
-
52
- # Returns the url to the "Watch a trailer" page
53
- def trailer_url
54
- 'http://imdb.com' + document.at("a[@href*=/video/screenplay/]")["href"] rescue nil
55
- end
56
-
57
- # Returns an array of genres (as strings)
58
- def genres
59
- document.search("h5[text()='Genre:'] ~ a[@href*=/Sections/Genres/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
60
- end
61
-
62
- # Returns an array of languages as strings.
63
- def languages
64
- document.search("h5[text()='Language:'] ~ a[@href*=/language/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
65
- end
66
-
67
- # Returns an array of countries as strings.
68
- def countries
69
- document.search("h5[text()='Country:'] ~ a[@href*=/country/']").map { |link| link.innerHTML.strip.imdb_unescape_html } rescue []
70
- end
71
-
72
- # Returns the duration of the movie in minutes as an integer.
73
- def length
74
- document.search("//h5[text()='Runtime:']/..").innerHTML[/\d+ min/].to_i rescue nil
75
- end
76
-
77
- # Returns a string containing the plot.
78
- def plot
79
- sanitize_plot(document.search("h5[text()='Plot:'] ~ div").first.innerHTML) rescue nil
80
- end
81
-
82
- # Returns a string containing the URL to the movie poster.
83
- def poster
84
- src = document.at("a[@name='poster'] img")['src'] rescue nil
85
- case src
86
- when /^(http:.+@@)/
87
- $1 + '.jpg'
88
- when /^(http:.+?)\.[^\/]+$/
89
- $1 + '.jpg'
90
- end
91
- end
92
-
93
- # Returns a float containing the average user rating
94
- def rating
95
- document.at(".starbar-meta b").innerHTML.strip.imdb_unescape_html.split('/').first.to_f rescue nil
96
- end
97
-
98
- # Returns an int containing the number of user ratings
99
- def votes
100
- document.at("#tn15rating .tn15more").innerHTML.strip.imdb_unescape_html.gsub(/[^\d+]/, "").to_i rescue nil
101
- end
102
-
103
- # Returns a string containing the tagline
104
- def tagline
105
- document.search("h5[text()='Tagline:'] ~ div").first.innerHTML.gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil
106
- end
107
-
108
- # Returns a string containing the mpaa rating and reason for rating
109
- def mpaa_rating
110
- document.search("h5[text()='MPAA:'] ~ div").first.innerHTML.strip.imdb_unescape_html rescue nil
111
- end
112
-
113
- # Returns a string containing the title
114
- def title(force_refresh = false)
115
- if @title && !force_refresh
116
- @title
117
- else
118
- @title = document.at("h1").innerHTML.split('<span').first.strip.imdb_unescape_html rescue nil
119
- end
120
- end
121
-
122
- # Returns an integer containing the year (CCYY) the movie was released in.
123
- def year
124
- document.search('a[@href^="/year/"]').innerHTML.to_i
125
- end
126
-
127
- # Returns release date for the movie.
128
- def release_date
129
- sanitize_release_date(document.search('h5[text()*=Release Date]').first.next_sibling.innerHTML.to_s) rescue nil
130
- end
131
-
132
- private
133
-
134
- # Returns a new Hpricot document for parsing.
135
- def document
136
- @document ||= Hpricot(Imdb::Movie.find_by_id(@id))
137
- end
138
-
139
- # Use HTTParty to fetch the raw HTML for this movie.
140
- def self.find_by_id(imdb_id)
141
- open("http://akas.imdb.com/title/tt#{imdb_id}/combined")
142
- end
143
-
144
- # Convenience method for search
145
- def self.search(query)
146
- Imdb::Search.new(query).movies
147
- end
148
-
149
- def self.top_250
150
- Imdb::Top250.new.movies
151
- end
152
-
153
- def sanitize_plot(the_plot)
154
- the_plot = the_plot.imdb_strip_tags
155
-
156
- the_plot = the_plot.gsub(/add\ssummary|full\ssummary/i, "")
157
- the_plot = the_plot.gsub(/add\ssynopsis|full\ssynopsis/i, "")
158
- the_plot = the_plot.gsub(/&nbsp;|&raquo;/i, "")
159
- the_plot = the_plot.gsub(/see|more/i, "")
160
- the_plot = the_plot.gsub(/\|/i, "")
161
-
162
- the_plot = the_plot.strip.imdb_unescape_html
163
- end
164
-
165
- def sanitize_release_date(the_release_date)
166
- the_release_date = the_release_date.gsub(/<a.*a>/,"")
167
- the_release_date = the_release_date.gsub(/&nbsp;|&raquo;/i, "")
168
- the_release_date = the_release_date.gsub(/see|more/i, "")
169
-
170
- the_release_date = the_release_date.strip.imdb_unescape_html
171
- end
172
-
4
+ class Movie < Base
173
5
  end # Movie
174
6
 
175
7
  end # Imdb