rotten-tomatoes47 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gem "nokogiri"
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 atom smith
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.
data/README.rdoc ADDED
@@ -0,0 +1,76 @@
1
+ = rotten-tomatoes
2
+
3
+ print "Movie Name: "
4
+ movie_name = STDIN.gets.chomp
5
+
6
+ require 'rubygems'
7
+ require 'rotten-tomatoes'
8
+
9
+ # get a movie
10
+ movie = Rotten_tomatoes::lucky_get_movie_info movie_name
11
+
12
+ # do stuff with it
13
+ puts movie.title
14
+ puts "Released in #{movie.year}"
15
+ puts "#{movie.runtime} long"
16
+ puts "Rated #{movie.rating}"
17
+ puts "Tomatometer Freshness rating of #{movie.tomatometer}%"
18
+ puts "Average critic rating of #{movie.tomatometer_average_rating}"
19
+ puts "#{movie.tomatometer_reviews_counted} reviews taken into consideration."
20
+ puts "#{movie.tomatometer_fresh} critic/s said it was fresh."
21
+ puts "#{movie.tomatometer_rotten} critic/s said it was rotten."
22
+ puts "Audience rating of #{movie.audience_rating}% fresh."
23
+ puts "Average audience rating of #{movie.audience_average_rating}."
24
+ puts "#{movie.audience_number_of_ratings} audience ratings."
25
+ puts "Released #{movie.release}"
26
+ puts "Distributed by #{movie.distributor}"
27
+
28
+ movie.genres.each do |genre|
29
+ puts "In the #{genre} genre."
30
+ end
31
+
32
+ movie.people.each do |role, people|
33
+ puts "\n#{role}"
34
+ people.each do |person|
35
+ if person[:characters] && !person[:characters].empty?
36
+ print "\t#{person[:characters]} portrayed by "
37
+ else
38
+ print "\t"
39
+ end
40
+ puts "#{person[:name]}"
41
+ end
42
+ end
43
+
44
+ # Alternatively, people can be accessed by role
45
+ # like so:
46
+ # movie.cast.each do |cast_member|
47
+ # p cast_member
48
+ # end
49
+
50
+ # movie.writers.each do |writer|
51
+ # p writer
52
+ # end
53
+
54
+ # movie.directors.each do |director|
55
+ # p director
56
+ # end
57
+ #
58
+ # you can also just:
59
+ # p movie.writer
60
+ # and
61
+ # p movie.director
62
+
63
+
64
+ == Note on Patches/Pull Requests
65
+
66
+ * Fork the project.
67
+ * Make your feature addition or bug fix.
68
+ * Add tests for it. This is important so I don't break it in a
69
+ future version unintentionally.
70
+ * Commit, do not mess with rakefile, version, or history.
71
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
72
+ * Send me a pull request. Bonus points for topic branches.
73
+
74
+ == Copyright
75
+
76
+ Copyright (c) 2010 atom smith. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "rotten-tomatoes"
8
+ gem.summary = 'Fetch information regarding movies from rottentomatoes.com'
9
+ gem.description = 'Allows you to search and get information about movies from rottentomatoes.com. Organizes returned information into easy to access attributes.'
10
+ gem.email = "re5etsmyth@gmail.com"
11
+ gem.homepage = "http://github.com/re5et/rotten-tomatoes"
12
+ gem.authors = ["atom smith"]
13
+ gem.add_development_dependency "thoughtbot-shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "rotten-tomatoes #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,297 @@
1
+ # A scraper to search for and fetch useful information
2
+ # about movies from rottentomatoes.com
3
+ #
4
+ # Author:: atom smith (http://twitter.com/re5et) *Slightly modified by Ponyboy47
5
+ # Copyright:: Copyright (c) 2010 atom smith
6
+ # License:: Distributes under the same terms as Ruby
7
+
8
+ require 'rubygems'
9
+ require 'nokogiri'
10
+ require 'open-uri'
11
+
12
+ # Module to hold everything
13
+ module Rotten_tomatoes
14
+
15
+ Base_url = 'http://www.rottentomatoes.com'
16
+ Person_search_url = Base_url + '/search/person.php?searchby=celebs&page=1&search='
17
+
18
+ # Create a new instance of Rotten_tomatoes::Movie with the
19
+ # rottentomatoes.com movie path as an argument
20
+ def self.get_movie_info movie_path
21
+ Movie.new movie_path
22
+ end
23
+
24
+ def self.get_person_info person_path
25
+ Person.new person_path
26
+ end
27
+
28
+ # Search rottentomatoes.com for the movie you are looking for
29
+ # will return an array of movies for you to select from, each
30
+ # will contain the 'info_url' you need to us Rotten_tomatoes:get_info
31
+ def self.find_movie movie
32
+ movies = []
33
+ url = URI.parse(URI.encode(Base_url + '/search/?search=#{movie.to_s}&sitesearch=rt'))
34
+ results = Nokogiri::HTML(open(url))
35
+ results.css('#movie_search_main tr').each do |row|
36
+ movies.push({
37
+ :title => row.css('td:nth-of-type(3) p:first-of-type a').inner_text,
38
+ :plot => row.css('td:nth-of-type(3) p:nth-of-type(2)').inner_text.gsub(/\APlot:/, '').strip,
39
+ :year => row.css('.date').inner_text,
40
+ :director => row.css('td:nth-of-type(3) p:nth-of-type(3) a:first-of-type').inner_text,
41
+ :movie_url => row.css('td:nth-of-type(3) p:first-of-type a').attr('href').value
42
+ })
43
+ end
44
+ return movies
45
+ end
46
+
47
+ def self.find_person person
48
+ people = []
49
+ url = URI.parse(URI.encode(Person_search_url + person.to_s))
50
+ results = Nokogiri::HTML(open(url))
51
+ results.css('#contrib_search_main tbody a').each do |row|
52
+ people.push({
53
+ :name => row.text,
54
+ :person_url => row.attribute('href').value
55
+ })
56
+ end
57
+ return people
58
+ end
59
+
60
+ # Uses Rotten_tomatoes::get_info to fetch info for the first
61
+ # search result found with Rotten_tomatoes::find_movie
62
+ def self.lucky_get_movie_info movie
63
+ get_movie_info find_movie(movie).first[:movie_url]
64
+ end
65
+
66
+ def self.lucky_get_person_info person
67
+ get_person_info find_person(person).first[:person_url]
68
+ end
69
+
70
+ # Rotten_tomatoes::Movie is a class that organizes the scraped
71
+ # information about the movie into easily accessible attribues.
72
+ class Movie
73
+
74
+ attr_reader :info_page, :title, :year, :people, :cast, :writers, :directors, :runtime, :rating, :tomatometer, :tomatometer_average_rating, :tomatometer_reviews_counted, :tomatometer_fresh, :tomatometer_rotten, :audience_rating, :audience_average_rating, :audience_number_of_ratings, :genres, :release, :distributor
75
+
76
+ def initialize movie_url
77
+ @info_page = Nokogiri::HTML(open(URI.parse(Rotten_tomatoes::Base_url + movie_url)))
78
+ if @info_page
79
+ set_title_and_year
80
+ set_cast
81
+ set_writers
82
+ set_directors
83
+ set_people
84
+ set_runtime
85
+ set_rating
86
+ set_tomatometer
87
+ set_tomatometer_average_rating
88
+ set_tomatometer_reviews_counted
89
+ set_tomatometer_fresh_and_rotten
90
+ set_audience_rating
91
+ set_audience_average_rating
92
+ set_audience_number_of_ratings
93
+ set_genres
94
+ set_release
95
+ set_distributor
96
+ end
97
+ @info_page = nil
98
+ return self
99
+ end
100
+
101
+ private
102
+
103
+ def set_title_and_year
104
+ title_and_year = @info_page.css('h1.movie_title span').first.inner_html
105
+ match = title_and_year.match(/(.*)\((\d{4})\)/)
106
+ @title = match[1].strip
107
+ @year = match[2]
108
+ end
109
+
110
+ def set_cast
111
+ @cast = []
112
+ @info_page.css('#cast-info li').each do |person|
113
+ @cast.push({
114
+ :name => person.css('a').inner_html,
115
+ :info_path => person.css('a').first['href'],
116
+ :thumbnail => person.css('img').first['src'],
117
+ :characters => person.css('.characters').first.text.gsub('(', '').gsub(')', '')
118
+ })
119
+ end
120
+ end
121
+
122
+ def set_writers
123
+ @writers = []
124
+ @info_page.css('.movie_info .right_col p:nth-child(3) a').each do |writer|
125
+ @writers.push({
126
+ :name => writer.inner_html,
127
+ :info_path => writer['href']
128
+ })
129
+ end
130
+ end
131
+
132
+ def set_directors
133
+ @directors = []
134
+ @info_page.css('.movie_info .right_col p:nth-child(2) a').each do |director|
135
+ @directors.push({
136
+ :name => director.inner_html,
137
+ :info_path => director['href']
138
+ })
139
+ end
140
+ end
141
+
142
+ def set_people
143
+ @people = { :cast => @cast, :writers => @writers, :directors => @directors }
144
+ end
145
+
146
+ def set_runtime
147
+ @runtime = @info_page.css('.movie_info .left_col p:nth-child(2) .content').first.inner_html
148
+ end
149
+
150
+ def set_rating
151
+ @rating = @info_page.css('.movie_info .left_col p:first-child .content span').text
152
+ end
153
+
154
+ def set_tomatometer
155
+ @tomatometer = @info_page.css('#all-critics-numbers #all-critics-meter').inner_html
156
+ end
157
+
158
+ def set_tomatometer_average_rating
159
+ @tomatometer_average_rating = @info_page.css('.critic_stats span:first-of-type').text.gsub(/\/.*/, '')
160
+ end
161
+
162
+ def set_tomatometer_reviews_counted
163
+ @tomatometer_reviews_counted = @info_page.css('.critic_stats span:nth-of-type(2)').text
164
+ end
165
+
166
+ def set_tomatometer_fresh_and_rotten
167
+ matches = @info_page.css('#all-critics-numbers .critic_stats').text.scan /Fresh:\s(\d+)\s|\sRotten:\s(\d)+/
168
+ @tomatometer_fresh = matches[0][0]
169
+ @tomatometer_rotten = matches[1][1]
170
+ end
171
+
172
+ def set_audience_rating
173
+ @audience_rating = @info_page.css('.fan_side .meter').text
174
+ end
175
+
176
+ def set_audience_average_rating
177
+ match = @info_page.css('.fan_side .critic_stats').text.match(/Average Rating: ([\d\.]+)\//)
178
+ @audience_average_rating = match[1]
179
+ end
180
+
181
+ def set_audience_number_of_ratings
182
+ match = @info_page.css('.fan_side .critic_stats').text.match(/User Ratings: (\d+)/)
183
+ @audience_number_of_ratings = match[1]
184
+ end
185
+
186
+ def set_genres
187
+ @genres = []
188
+ @info_page.css('.movie_info > p:first-of-type .content a').each do |genre|
189
+ @genres.push genre.text
190
+ end
191
+ end
192
+
193
+ def set_release
194
+ @release = info_page.css('.movie_info .left_col p:nth-of-type(3) .content span').text
195
+ end
196
+
197
+ def set_distributor
198
+ @distributor = @info_page.css('.movie_info .right_col p:nth-of-type(1)').text.gsub('Distributor:', '')
199
+ end
200
+
201
+ public
202
+
203
+ def director
204
+ @directors.first
205
+ end
206
+
207
+ def writer
208
+ @writers.first
209
+ end
210
+
211
+ end
212
+
213
+ class Person
214
+
215
+ attr_reader :name, :main_role, :picture, :highest_rated, :lowest_rated, :birthdate, :birthplace, :age, :bio, :number_of_movies, :box_office, :movies
216
+
217
+ def initialize person_url
218
+
219
+ @info_page = Nokogiri::HTML(open(URI.parse(Rotten_tomatoes::Base_url + person_url + '?items=999')))
220
+ if @info_page
221
+ set_movies
222
+ set_name
223
+ set_main_role
224
+ set_picture
225
+ set_highest_rated
226
+ set_lowest_rated
227
+ set_birthdate
228
+ set_birthplace
229
+ set_age
230
+ set_bio
231
+ set_number_of_movies
232
+ set_box_office
233
+ end
234
+ @info_page = nil
235
+ return self
236
+ end
237
+
238
+ def set_name
239
+ @name = @info_page.css('h1.celeb_title').text
240
+ end
241
+
242
+ def set_main_role
243
+ @main_role = @info_page.css('#breadcrumb .subLevelCrumb').text.gsub('/', '').chomp('s')
244
+ end
245
+
246
+ def set_picture
247
+ @picture = @info_page.css('#mainImage').attribute('src').value
248
+ end
249
+
250
+ def set_highest_rated
251
+ highest_rated_url = @info_page.css('#celebRatings p:first a').attribute('href').value
252
+ @highest_rated = @movies[@movies.index {|movie| movie[:movie_url] == highest_rated_url}]
253
+ end
254
+
255
+ def set_lowest_rated
256
+ lowest_rated_url = @info_page.css('#celebRatings p:nth-of-type(2) a').attribute('href').value
257
+ @lowest_rated = @movies[@movies.index {|movies| movies[:movie_url] == lowest_rated_url}]
258
+ end
259
+
260
+ def set_birthdate
261
+ @birthdate = Time.parse @info_page.css('#celeb_bio p:first').text.split("Birthdate: ").last.strip
262
+ end
263
+
264
+ def set_birthplace
265
+ @birthplace = @info_page.css('#celeb_bio p:nth-of-type(2)').text.split("Birthplace: ").last.strip
266
+ end
267
+
268
+ def set_age
269
+ @age = ((Time.now - @birthdate) / 60 / 60 / 24 / 365).floor
270
+ end
271
+
272
+ def set_bio
273
+ @bio = @info_page.css('#celeb_bio p:nth-of-type(3)').text.split("Bio:").last.gsub('[More...]', '').strip
274
+ end
275
+
276
+ def set_number_of_movies
277
+ @number_of_movies = @movies.size
278
+ end
279
+
280
+ def set_box_office
281
+ @box_office = @info_page.css('#celeb_stats div.celeb_stat.last').text.split(":").last.strip
282
+ end
283
+
284
+ def set_movies
285
+ @movies = []
286
+ @info_page.css('#filmographyTbl tbody tr').each do |movie|
287
+ @movies.push({
288
+ :title => movie.css('.filmographyTbl_titleCol').text.strip,
289
+ :movie_url => movie.css('.filmographyTbl_titleCol a').attribute('href').value,
290
+ :credit => movie.css('.filmographyTbl_creditCol').text.strip
291
+ })
292
+ end
293
+ end
294
+
295
+ end
296
+
297
+ end
@@ -0,0 +1,51 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{rotten-tomatoes47}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = [""]
12
+ s.date = %q{2012-01-30}
13
+ s.description = %q{Allows you to search and get information about movies from rottentomatoes.com. Organizes returned information into easy to access attributes.}
14
+ s.email = %q{}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/rotten-tomatoes.rb",
27
+ "rotten-tomatoes47.gemspec",
28
+ ]
29
+ s.homepage = %q{http://github.com/Ponyboy47/rotten-tomatoes}
30
+ s.require_paths = ["lib"]
31
+ s.rubygems_version = %q{1.3.7}
32
+ s.summary = %q{Fetch information regarding movies from rottentomatoes.com}
33
+ s.test_files = []
34
+
35
+ if s.respond_to? :specification_version then
36
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
37
+ s.specification_version = 3
38
+
39
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
40
+ s.add_runtime_dependency(%q<nokogiri>, [">= 0"])
41
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
42
+ else
43
+ s.add_dependency(%q<nokogiri>, [">= 0"])
44
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
45
+ end
46
+ else
47
+ s.add_dependency(%q<nokogiri>, [">= 0"])
48
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ end
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rotten-tomatoes47
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - ''
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-30 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: nokogiri
16
+ requirement: &80954230 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *80954230
25
+ - !ruby/object:Gem::Dependency
26
+ name: thoughtbot-shoulda
27
+ requirement: &80953910 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *80953910
36
+ description: Allows you to search and get information about movies from rottentomatoes.com. Organizes
37
+ returned information into easy to access attributes.
38
+ email: ''
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files:
42
+ - LICENSE
43
+ - README.rdoc
44
+ files:
45
+ - .document
46
+ - Gemfile
47
+ - LICENSE
48
+ - README.rdoc
49
+ - Rakefile
50
+ - VERSION
51
+ - lib/rotten-tomatoes.rb
52
+ - rotten-tomatoes47.gemspec
53
+ homepage: http://github.com/Ponyboy47/rotten-tomatoes
54
+ licenses: []
55
+ post_install_message:
56
+ rdoc_options: []
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ none: false
61
+ requirements:
62
+ - - ! '>='
63
+ - !ruby/object:Gem::Version
64
+ version: '0'
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 1.8.15
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Fetch information regarding movies from rottentomatoes.com
77
+ test_files: []