royw-imdb 0.0.17 → 0.0.19

Sign up to get free protection for your applications and to get access to all the features.
Files changed (31) hide show
  1. data/History.txt +90 -0
  2. data/README +1 -1
  3. data/Rakefile +96 -0
  4. data/VERSION.yml +4 -0
  5. data/lib/imdb/imdb_profile.rb +277 -0
  6. data/spec/cache_extensions.rb +91 -0
  7. data/spec/imdb_image_spec.rb +23 -0
  8. data/spec/imdb_movie_spec.rb +232 -0
  9. data/spec/imdb_profile_spec.rb +182 -0
  10. data/spec/imdb_search_spec.rb +191 -0
  11. data/spec/samples/sample_image.html +409 -0
  12. data/spec/samples/sample_incomplete_movie.html +588 -0
  13. data/spec/samples/sample_jet_pilot.html +929 -0
  14. data/spec/samples/sample_meltdown.html +528 -0
  15. data/spec/samples/sample_movie.html +1135 -0
  16. data/spec/samples/sample_open_season.html +524 -0
  17. data/spec/samples/sample_search.html +446 -0
  18. data/spec/samples/sample_spanish_search.html +390 -0
  19. data/spec/samples/sample_who_am_i_search.html +517 -0
  20. data/spec/samples/www.imdb.com/find?q=Meltdown;s=tt +528 -0
  21. data/spec/samples/www.imdb.com/find?q=National+Treasure%3A+Book+of+Secrets;s=tt +1036 -0
  22. data/spec/samples/www.imdb.com/find?q=National+Treasure+2;s=tt +1036 -0
  23. data/spec/samples/www.imdb.com/find?q=Open+Season;s=tt +524 -0
  24. data/spec/samples/www.imdb.com/find?q=The+Alamo;s=tt +531 -0
  25. data/spec/samples/www.imdb.com/find?q=Who+Am+I%3F;s=tt +517 -0
  26. data/spec/samples/www.imdb.com/find?q=indiana+jones;s=tt +517 -0
  27. data/spec/samples/www.imdb.com/find?q=some+extremely+specific+search+for+indiana+jones;s=tt +1135 -0
  28. data/spec/samples/www.imdb.com/rg/action-box-title/primary-photo/media/rm1203608832/tt0097576 +409 -0
  29. data/spec/spec_helper.rb +5 -0
  30. data/spec/string_extensions_spec.rb +25 -0
  31. metadata +53 -37
@@ -0,0 +1,91 @@
1
+ # NOTE extremely ugly and non-DRY. Probably good candidate for meta programming.
2
+
3
+ # override the classes' read_page method and replace with one
4
+ # that will cache pages in spec/samples/{url}
5
+
6
+ class ImdbMovie
7
+ private
8
+ def read_page(page)
9
+ html = nil
10
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
11
+ if File.exist?(filespec)
12
+ html = open(filespec).read
13
+ else
14
+ html = open(page).read
15
+ cache_html_files(page, html)
16
+ end
17
+ html
18
+ end
19
+
20
+ # this is used to save imdb pages so they may be used by rspec
21
+ def cache_html_files(page, html)
22
+ begin
23
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
24
+ unless File.exist?(filespec)
25
+ puts "caching #{filespec}"
26
+ File.mkdirs(File.dirname(filespec))
27
+ File.open(filespec, 'w') { |f| f.puts html }
28
+ end
29
+ rescue Exception => eMsg
30
+ puts eMsg.to_s
31
+ end
32
+ end
33
+ end
34
+
35
+ class ImdbSearch
36
+ private
37
+ def read_page(page)
38
+ html = nil
39
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
40
+ if File.exist?(filespec)
41
+ html = open(filespec).read
42
+ else
43
+ html = open(page).read
44
+ cache_html_files(page, html)
45
+ end
46
+ html
47
+ end
48
+
49
+ # this is used to save imdb pages so they may be used by rspec
50
+ def cache_html_files(page, html)
51
+ begin
52
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
53
+ unless File.exist?(filespec)
54
+ puts "caching #{filespec}"
55
+ File.mkdirs(File.dirname(filespec))
56
+ File.open(filespec, 'w') { |f| f.puts html }
57
+ end
58
+ rescue Exception => eMsg
59
+ puts eMsg.to_s
60
+ end
61
+ end
62
+ end
63
+
64
+ class ImdbImage
65
+ private
66
+ def read_page(page)
67
+ html = nil
68
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
69
+ if File.exist?(filespec)
70
+ html = open(filespec).read
71
+ else
72
+ html = open(page).read
73
+ cache_html_files(page, html)
74
+ end
75
+ html
76
+ end
77
+
78
+ # this is used to save imdb pages so they may be used by rspec
79
+ def cache_html_files(page, html)
80
+ begin
81
+ filespec = page.gsub(/^http:\//, 'spec/samples').gsub(/\/$/, '.html')
82
+ unless File.exist?(filespec)
83
+ puts "caching #{filespec}"
84
+ File.mkdirs(File.dirname(filespec))
85
+ File.open(filespec, 'w') { |f| f.puts html }
86
+ end
87
+ rescue Exception => eMsg
88
+ puts eMsg.to_s
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe ImdbMovie do
4
+
5
+ describe 'Indiana Jones and the Last Crusade' do
6
+
7
+ before(:each) do
8
+ @imdb_image = ImdbImage.new("/rg/action-box-title/primary-photo/media/rm1203608832/tt0097576")
9
+ # @imdb_image.stub!(:open).and_return(open("#{$samples_dir}/sample_image.html"))
10
+ end
11
+
12
+ # it "should query IMDB url" do
13
+ # @imdb_image.should_receive(:open).with("http://www.imdb.com/rg/action-box-title/primary-photo/media/rm1203608832/tt0097576").and_return(open("#{$samples_dir}/sample_image.html"))
14
+ # @imdb_image.send(:document)
15
+ # end
16
+
17
+ it "should get the image" do
18
+ @imdb_image.image.should == "http://ia.media-imdb.com/images/M/MV5BMTkzODA5ODYwOV5BMl5BanBnXkFtZTcwMjAyNDYyMQ@@._V1._SX216_SY316_.jpg"
19
+ end
20
+
21
+ end
22
+
23
+ end
@@ -0,0 +1,232 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ describe ImdbMovie do
4
+
5
+ describe 'Jet Pilot' do
6
+
7
+ before(:each) do
8
+ @imdb_movie = ImdbMovie.new('0050562')
9
+ @imdb_movie.stub!(:open).and_return(open("#{$samples_dir}/sample_jet_pilot.html"))
10
+ end
11
+
12
+ it "should query IMDB url" do
13
+ # @imdb_movie.should_receive(:open).with("http://www.imdb.com/title/tt0050562/").and_return(open("#{$samples_dir}/sample_jet_pilot.html"))
14
+ @imdb_movie.should_receive(:fetch).with("http://www.imdb.com/title/tt0050562/").and_return(open("#{$samples_dir}/www.imdb.com/title/tt0050562.html"))
15
+ @imdb_movie.send(:document)
16
+ end
17
+
18
+ it "should convert to xml" do
19
+ xml = @imdb_movie.to_xml
20
+ xml.should =~ /<year>1957<\/year>/
21
+ end
22
+
23
+ it "should convert to yaml" do
24
+ yaml = @imdb_movie.to_yaml
25
+ yaml.should =~ /year: "1957"/
26
+ end
27
+
28
+ end
29
+
30
+ describe 'Indiana Jones and the Last Crusade' do
31
+
32
+ before(:each) do
33
+ @imdb_movie = ImdbMovie.new('0097576')
34
+ # @imdb_movie.stub!(:open).and_return(open("#{$samples_dir}/sample_movie.html"))
35
+ end
36
+
37
+ # it "should query IMDB url" do
38
+ # @imdb_movie.should_receive(:open).with("http://www.imdb.com/title/tt0097576/").and_return(open("#{$samples_dir}/www.imdb.com/title/tt0097576.html"))
39
+ # @imdb_movie.send(:document)
40
+ # end
41
+
42
+ it "should get the title" do
43
+ @imdb_movie.title.should == "Indiana Jones and the Last Crusade"
44
+ end
45
+
46
+ it "should get director(s)" do
47
+ @imdb_movie.directors.should include('Steven Spielberg')
48
+ end
49
+
50
+ it "should get the poster url" do
51
+ @imdb_movie.poster_url.should == "/rg/action-box-title/primary-photo/media/rm1203608832/tt0097576"
52
+ end
53
+
54
+ it "should return an ImdbImage object" do
55
+ @imdb_movie.poster.should be_instance_of(ImdbImage)
56
+ end
57
+
58
+ it "should get the rating" do
59
+ @imdb_movie.rating.should == 8.3
60
+ end
61
+
62
+ it "should get cast members" do
63
+ @imdb_movie.cast_members.should include(['Harrison Ford', 'Indiana Jones'])
64
+ @imdb_movie.cast_members.should include(['Sean Connery', 'Professor Henry Jones'])
65
+ @imdb_movie.cast_members.should include(['Denholm Elliott', 'Dr. Marcus Brody'])
66
+ @imdb_movie.cast_members.should include(['Alison Doody', 'Dr. Elsa Schneider'])
67
+ @imdb_movie.cast_members.should include(['John Rhys-Davies', 'Sallah'])
68
+ @imdb_movie.cast_members.should_not include('more')
69
+ end
70
+
71
+ it "should get the writers" do
72
+ @imdb_movie.writers.should include('George Lucas')
73
+ @imdb_movie.writers.should include('Philip Kaufman')
74
+ @imdb_movie.writers.should_not include('more')
75
+ end
76
+
77
+ it "should get the year" do
78
+ @imdb_movie.year.should == "1989"
79
+ end
80
+
81
+ it "should get the release date" do
82
+ @imdb_movie.release_date.should be_an_instance_of(Date)
83
+ @imdb_movie.release_date.to_s.should == Date.new(1989, 5, 24).to_s
84
+ end
85
+
86
+ it "should get the genres" do
87
+ @imdb_movie.genres.should have(2).strings
88
+ @imdb_movie.genres.should include('Action')
89
+ @imdb_movie.genres.should include('Adventure')
90
+ end
91
+
92
+ it "should get the plot" do
93
+ @imdb_movie.plot.should == "When Dr. Henry Jones Sr. suddenly goes missing while pursuing the Holy Grail, eminent archaeologist Indiana Jones must follow in his father's footsteps and stop the Nazis."
94
+ end
95
+
96
+ it "should get the length" do
97
+ @imdb_movie.length.should == '127 min'
98
+ end
99
+
100
+ it "should get the countries" do
101
+ @imdb_movie.countries.should have(1).string
102
+ @imdb_movie.countries.should include('USA')
103
+ end
104
+
105
+ it "should get the languages" do
106
+ @imdb_movie.languages.should have(3).strings
107
+ @imdb_movie.languages.should include('English')
108
+ @imdb_movie.languages.should include('German')
109
+ @imdb_movie.languages.should include('Greek')
110
+ end
111
+
112
+ it "should get the color" do
113
+ @imdb_movie.color.should == 'Color'
114
+ end
115
+
116
+ it "should get the company" do
117
+ @imdb_movie.company.should == 'Lucasfilm'
118
+ end
119
+
120
+ it "should get some photos" do
121
+ @imdb_movie.photos.should have(10).strings
122
+ @imdb_movie.photos.should include('http://ia.media-imdb.com/images/M/MV5BMTY4MzY3OTY0MF5BMl5BanBnXkFtZTYwODM0OTE3._V1._CR82,0,320,320_SS90_.jpg')
123
+ @imdb_movie.photos.should include('http://ia.media-imdb.com/images/M/MV5BMjAwNTM4ODc3Nl5BMl5BanBnXkFtZTYwNzU0OTE3._V1._CR82,0,320,320_SS90_.jpg')
124
+ end
125
+
126
+ it "should get the tagline" do
127
+ @imdb_movie.tagline.should == "He's back in an all new adventure. Memorial Day 1989."
128
+ end
129
+
130
+ it "should get the aspect ratio" do
131
+ @imdb_movie.aspect_ratio.should == "2.20 : 1"
132
+ end
133
+
134
+ it "should convert to xml" do
135
+ xml = @imdb_movie.to_xml
136
+ xml.should =~ /<year>1989<\/year>/
137
+ end
138
+
139
+ it "should convert to yaml" do
140
+ yaml = @imdb_movie.to_yaml
141
+ yaml.should =~ /year: "1989"/
142
+ end
143
+
144
+ it "should be able to convert to and then from xml" do
145
+ hash = nil
146
+ begin
147
+ xml = @imdb_movie.to_xml
148
+ hash = XmlSimple.xml_in(xml)
149
+ rescue
150
+ hash = nil
151
+ end
152
+ hash.should_not be_nil
153
+ end
154
+ end
155
+
156
+ describe 'Han robado una estrella' do
157
+
158
+ before(:each) do
159
+ @imdb_movie = ImdbMovie.new('0054961')
160
+ # @imdb_movie.stub!(:open).and_return(open("#{$samples_dir}/sample_incomplete_movie.html"))
161
+ end
162
+
163
+ # it "should query IMDB url" do
164
+ # @imdb_movie.should_receive(:open).with("http://www.imdb.com/title/tt0054961/").and_return(open("#{$samples_dir}/sample_incomplete_movie.html"))
165
+ # @imdb_movie.should_receive(:open).with("http://www.imdb.com/title/tt0054961/").and_return(open("#{$samples_dir}/www.imdb.com/title/tt0054961.html"))
166
+ # @imdb_movie.send(:document)
167
+ # end
168
+
169
+ it "should get the title" do
170
+ @imdb_movie.title.should == "Han robado una estrella"
171
+ end
172
+
173
+ it "should get director(s)" do
174
+ @imdb_movie.directors.should include('Javier Setó')
175
+ end
176
+
177
+ it "should not get the poster" do
178
+ @imdb_movie.poster.should be_nil
179
+ end
180
+
181
+ it "should get cast members" do
182
+ @imdb_movie.cast_members.should include(['Rafaela Aparicio', ''])
183
+ @imdb_movie.cast_members.should include(['Marujita Díaz', ''])
184
+ @imdb_movie.cast_members.should include(['Espartaco Santoni', ''])
185
+ @imdb_movie.cast_members.should_not include('more')
186
+ end
187
+
188
+ it "should get the writers" do
189
+ @imdb_movie.writers.should have(1).string
190
+ @imdb_movie.writers.should include('Paulino Rodrigo')
191
+ end
192
+
193
+ it "should get the release date" do
194
+ @imdb_movie.release_date.should be_an_instance_of(Date)
195
+ @imdb_movie.release_date.should == Date.new(1963, 9, 9)
196
+ end
197
+
198
+ it "should get the genres" do
199
+ @imdb_movie.genres.should == ['Comedy', 'Musical']
200
+ end
201
+
202
+ it "should not get the plot" do
203
+ @imdb_movie.plot.should be_nil
204
+ end
205
+
206
+ it "should get the length" do
207
+ @imdb_movie.length.should == '93 min'
208
+ end
209
+
210
+ it "should get the countries" do
211
+ @imdb_movie.countries.should == ['Spain']
212
+ end
213
+
214
+ it "should get the languages" do
215
+ @imdb_movie.languages.should == ['Spanish']
216
+ end
217
+
218
+ it "should not get the color" do
219
+ @imdb_movie.color.should be_nil
220
+ end
221
+
222
+ it "should get the company" do
223
+ @imdb_movie.company.should == 'Brepi Films'
224
+ end
225
+
226
+ it "should not get any photos" do
227
+ @imdb_movie.photos.should be_empty
228
+ end
229
+
230
+ end
231
+
232
+ end
@@ -0,0 +1,182 @@
1
+ require File.dirname(__FILE__) + '/spec_helper'
2
+
3
+ require 'tempfile'
4
+
5
+ TMPDIR = File.join(File.dirname(__FILE__), '../tmp')
6
+
7
+ # Time to add your specs!
8
+ # http://rspec.info/
9
+
10
+ describe "ImdbProfile" do
11
+
12
+ before(:all) do
13
+ File.mkdirs(TMPDIR)
14
+ end
15
+
16
+ after(:each) do
17
+ Dir.glob(File.join(TMPDIR, "imdb_profile_spec*")).each { |filename| File.delete(filename) }
18
+ end
19
+
20
+ it "should find by imdb_id that is prefixed with a 'tt'" do
21
+ profile = ImdbProfile.first(:imdb_id => 'tt0465234')
22
+ profile.should_not == nil
23
+ end
24
+
25
+ it "should find by imdb_id that is not prefixed with 'tt'" do
26
+ profile = ImdbProfile.first(:imdb_id => '0465234')
27
+ profile.should_not == nil
28
+ end
29
+
30
+ it "should find by single title" do
31
+ profile = ImdbProfile.first(:titles => ['National Treasure: Book of Secrets'])
32
+ profile.should_not == nil
33
+ end
34
+
35
+ it "should find by multiple title" do
36
+ profile = ImdbProfile.first(:titles => ['National Treasure: Book of Secrets', 'National Treasure 2'])
37
+ profile.should_not == nil
38
+ end
39
+
40
+ it "should find by multiple title embedded in arrays" do
41
+ profile = ImdbProfile.first(:titles => [['National Treasure: Book of Secrets'], 'National Treasure 2'])
42
+ profile.should_not == nil
43
+ end
44
+
45
+ it "should find by AKA title" do
46
+ profile = ImdbProfile.first(:titles => ['National Treasure 2'])
47
+ profile.should_not == nil
48
+ end
49
+
50
+ # We use two versions of "The Alamo" to test for year discrimination
51
+ # John Wayne's The Alamo produced and released in 1960, IMDB ID => tt0053580
52
+ # John Lee Hancock's The Alamo produced and released in 2004, IMDB ID => tt0318974
53
+
54
+ # test all()
55
+ it "should find multiple versions of The Alamo" do
56
+ profiles = ImdbProfile.all(:titles => ['The Alamo'])
57
+ profiles.length.should > 1
58
+ end
59
+
60
+ it "should find the two versions of The Alamo that we are interested in" do
61
+ profiles = ImdbProfile.all(:titles => ['The Alamo'])
62
+ imdb_ids = profiles.collect{|profile| profile.imdb_id}
63
+ imdb_ids = imdb_ids.select{|ident| ident == 'tt0053580' || ident == 'tt0318974'}
64
+ imdb_ids.uniq.compact.length.should == 2
65
+ end
66
+
67
+ # find first using title and media year
68
+
69
+ it "should find John Wayne's The Alamo by title with media year" do
70
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :media_years => [1960])
71
+ profile.imdb_id.should == 'tt0053580'
72
+ end
73
+
74
+ it "should find John Lee Hancock's The Alamo by title with media year" do
75
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :media_years => [2004])
76
+ profile.imdb_id.should == 'tt0318974'
77
+ end
78
+
79
+ # find first using title and production year
80
+
81
+ it "should find John Wayne's The Alamo by title with production year" do
82
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [1960])
83
+ profile.imdb_id.should == 'tt0053580'
84
+ end
85
+
86
+ it "should find John Lee Hancock's The Alamo by title with production year" do
87
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [2004])
88
+ profile.imdb_id.should == 'tt0318974'
89
+ end
90
+
91
+ # find first using title and released year
92
+
93
+ it "should find John Wayne's The Alamo by title with released year" do
94
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1960])
95
+ profile.imdb_id.should == 'tt0053580'
96
+ end
97
+
98
+ it "should find John Lee Hancock's The Alamo by title with released year" do
99
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [2004])
100
+ profile.imdb_id.should == 'tt0318974'
101
+ end
102
+
103
+ # here we test fuzzy year matching (plus or minus a year)
104
+ # fuzzy years are only supported for production and released years, not media years
105
+
106
+ it "should not find John Wayne's The Alamo by title with the media year off by a year" do
107
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :media_years => [1959])
108
+ profile2 = ImdbProfile.first(:titles => ['The Alamo'], :media_years => [1961])
109
+ (profile1.should be_nil) && (profile2.should be_nil)
110
+ end
111
+
112
+ it "should find John Wayne's The Alamo by title with the production year off by a year" do
113
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [1959])
114
+ profile2 = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [1961])
115
+ (profile1.imdb_id.should == 'tt0053580') && (profile2.imdb_id.should == 'tt0053580')
116
+ end
117
+
118
+ it "should find John Wayne's The Alamo by title with the released year off by a year" do
119
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1959])
120
+ profile2 = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1961])
121
+ (profile1.imdb_id.should == 'tt0053580') && (profile2.imdb_id.should == 'tt0053580')
122
+ end
123
+
124
+ it "should not find John Wayne's The Alamo by title with the production year off by two years" do
125
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [1958])
126
+ profile2 = ImdbProfile.first(:titles => ['The Alamo'], :production_years => [1962])
127
+ (profile1.should be_nil) && (profile2.should be_nil)
128
+ end
129
+
130
+ it "should not find John Wayne's The Alamo by title with the released year off by two years" do
131
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1958])
132
+ profile2 = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1962])
133
+ (profile1.should be_nil) && (profile2.should be_nil)
134
+ end
135
+
136
+ # let's make sure the generated xml from to_xml() is valid
137
+
138
+ it "should be able to convert to xml and then from xml" do
139
+ hash = nil
140
+ begin
141
+ profile = ImdbProfile.first(:imdb_id => 'tt0465234')
142
+ xml = profile.to_xml
143
+ hash = XmlSimple.xml_in(xml)
144
+ rescue
145
+ hash = nil
146
+ end
147
+ hash.should_not be_nil
148
+ end
149
+
150
+ # now let's test caching the profile to/from a file
151
+
152
+ it "should not create a file if a :filespec option is passed that is nil" do
153
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1960], :filespec => nil)
154
+ Dir.glob(File.join(TMPDIR, "imdb_profile_spec*")).empty?.should be_true
155
+ end
156
+
157
+ it "should create a file if a :filespec option is passed" do
158
+ filespec = get_temp_filename
159
+ profile = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1960], :filespec => filespec)
160
+ (File.exist?(filespec) && (File.size(filespec) > 0)).should be_true
161
+ end
162
+
163
+ it "should load from a file if a :filespec option is passed and the file exists" do
164
+ filespec = get_temp_filename
165
+ profile1 = ImdbProfile.first(:titles => ['The Alamo'], :released_years => [1960], :filespec => filespec)
166
+ profile2 = ImdbProfile.first(:filespec => filespec)
167
+ profile1.imdb_id.should == profile2.imdb_id
168
+ end
169
+
170
+ it "should not load from a file if a :filespec option is passed and the file does not exists" do
171
+ filespec = get_temp_filename
172
+ profile = ImdbProfile.first(:filespec => filespec)
173
+ profile.should be_nil
174
+ end
175
+
176
+ def get_temp_filename
177
+ outfile = Tempfile.new('imdb_profile_spec', TMPDIR)
178
+ filespec = outfile.path
179
+ outfile.unlink
180
+ filespec
181
+ end
182
+ end