Class: Spotlite::Movie
- Inherits:
-
Object
- Object
- Spotlite::Movie
- Defined in:
- lib/spotlite/movie.rb
Overview
Represents a movie on IMDb.com
Instance Attribute Summary collapse
-
#imdb_id ⇒ Object
Returns the value of attribute imdb_id.
-
#response ⇒ Object
Returns the value of attribute response.
-
#url ⇒ Object
Returns the value of attribute url.
Class Method Summary collapse
-
.find(query) ⇒ Object
Returns a list of movies as an array of
Spotlite::Movieobjects Takes single parameter and searches for movies by title and alternative titles. -
.search(params = {}) ⇒ Object
Returns a list of movies as an array of
Spotlite::Movieobjects Takes optional parameters as a hash See github.com/defeed/spotlite/wiki/Advanced-movie-search for details.
Instance Method Summary collapse
-
#alternative_titles ⇒ Object
Returns a list of movie alternative titles as an array of hashes with keys
title(string) andcomment(string). -
#cast ⇒ Object
Returns a list of actors as an array
Spotlite::Personobjects. -
#content_rating ⇒ Object
Returns content rating as a string.
-
#countries ⇒ Object
Returns a list of countries as an array of hashes with keys:
code(string) andname(string). -
#credits ⇒ Object
Returns combined ‘cast` and `crew` as an array of
Spotlite::Personobjects. -
#crew ⇒ Object
Combines all crew categories and returns an array of
Spotlite::Personobjects. -
#crew_categories ⇒ Object
Returns available crew categories, e.g.
-
#critic_reviews ⇒ Object
Returns a list of critic reviews as an array of hashes with keys:
source(string),author(string),excerpt(string), andscore(integer). -
#description ⇒ Object
Returns short description as a string.
-
#directors ⇒ Object
Returns a list of directors as an array of
Spotlite::Personobjects. -
#genres ⇒ Object
Returns a list of genres as an array of strings.
-
#images ⇒ Object
Returns URLs of movie still frames as an array of strings.
-
#initialize(imdb_id, title = nil, year = nil) ⇒ Movie
constructor
Initialize a new movie object by its IMDb ID as a string.
-
#keywords ⇒ Object
Returns a list of keywords as an array of strings.
-
#languages ⇒ Object
Returns a list of languages as an array of hashes with keys:
code(string) andname(string). -
#metascore ⇒ Object
Returns Metascore rating as an integer.
-
#original_title ⇒ Object
Returns original non-english title as a string.
-
#parse_crew(category) ⇒ Object
Returns a list of crew members of a certain category as an array
Spotlite::Personobjects. -
#poster_url ⇒ Object
Returns primary poster URL as a string.
-
#producers ⇒ Object
Returns a list of producers as an array of
Spotlite::Personobjects. -
#rating ⇒ Object
Returns IMDb rating as a float.
-
#recommended_movies ⇒ Object
Returns an array of recommended movies as an array of initialized objects of
Movieclass. -
#release_date ⇒ Object
Returns original release date as a date.
-
#release_dates ⇒ Object
Returns a list of regions and corresponding release dates as an array of hashes with keys: region
code(string),regionname (string),date(date), andcomment(string) If day is unknown, 1st day of month is assigned If day and month are unknown, 1st of January is assigned. -
#runtime ⇒ Object
Returns runtime (length) in minutes as an integer.
-
#stars ⇒ Object
Returns a list of starred actors as an array of
Spotlite::Personobjects. -
#storyline ⇒ Object
Returns storyline as a string.
-
#summaries ⇒ Object
Returns a list of plot summaries as an array of strings.
-
#technical ⇒ Object
Returns technical information like film length, aspect ratio, cameras, etc.
-
#title ⇒ Object
Returns title as a string.
-
#trivia ⇒ Object
Returns a list of trivia facts as an array of strings.
-
#votes ⇒ Object
Returns number of votes as an integer.
-
#writers ⇒ Object
Returns a list of writers as an array of
Spotlite::Personobjects. -
#year ⇒ Object
Returns year of original release as an integer.
Constructor Details
#initialize(imdb_id, title = nil, year = nil) ⇒ Movie
Initialize a new movie object by its IMDb ID as a string
movie = Spotlite::Movie.new('0133093')
Spotlite::Movie class objects are lazy loading. No HTTP request will be performed upon object initialization. HTTP request will be performed once when you use a method that needs remote data Currently, all data is spread across 6 pages: main movie page, /releaseinfo, /fullcredits, /keywords, /trivia, and /criticreviews
15 16 17 18 19 20 |
# File 'lib/spotlite/movie.rb', line 15 def initialize(imdb_id, title = nil, year = nil) @imdb_id = "%07d" % imdb_id.to_i @title = title @year = year @url = "http://www.imdb.com/title/tt#{@imdb_id}/" end |
Instance Attribute Details
#imdb_id ⇒ Object
Returns the value of attribute imdb_id.
4 5 6 |
# File 'lib/spotlite/movie.rb', line 4 def imdb_id @imdb_id end |
#response ⇒ Object
Returns the value of attribute response.
4 5 6 |
# File 'lib/spotlite/movie.rb', line 4 def response @response end |
#url ⇒ Object
Returns the value of attribute url.
4 5 6 |
# File 'lib/spotlite/movie.rb', line 4 def url @url end |
Class Method Details
.find(query) ⇒ Object
Returns a list of movies as an array of Spotlite::Movie objects Takes single parameter and searches for movies by title and alternative titles
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/spotlite/movie.rb', line 24 def self.find(query) results = Spotlite::Client.get( 'http://www.imdb.com/find', query: { q: query, s: 'tt', ttype: 'ft' } ) results.css('.result_text').map do |result| imdb_id = result.at('a')['href'].parse_imdb_id title = result.at('a').text.strip year = result.children.take(3).last.text.parse_year [imdb_id, title, year] end.map do |values| self.new(*values) end end |
.search(params = {}) ⇒ Object
Returns a list of movies as an array of Spotlite::Movie objects Takes optional parameters as a hash See github.com/defeed/spotlite/wiki/Advanced-movie-search for details
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/spotlite/movie.rb', line 42 def self.search(params = {}) defaults = { title_type: 'feature', view: 'simple', count: 250, start: 1, sort: 'moviemeter,asc' } params = defaults.merge(params) results = Spotlite::Client.get( 'http://www.imdb.com/search/title', query: params ) results.css('td.title').map do |result| imdb_id = result.at('a')['href'].parse_imdb_id title = result.at('a').text.strip year = result.at('.year_type').text.parse_year [imdb_id, title, year] end.map do |values| self.new(*values) end end |
Instance Method Details
#alternative_titles ⇒ Object
Returns a list of movie alternative titles as an array of hashes with keys title (string) and comment (string)
189 190 191 192 193 194 195 196 197 |
# File 'lib/spotlite/movie.rb', line 189 def alternative_titles array = [] release_info.css('#akas').css('tr').map do |row| cells = row.css('td') array << { :title => cells.last.text.strip, :comment => cells.first.text.strip } end array end |
#cast ⇒ Object
Returns a list of actors as an array Spotlite::Person objects
227 228 229 230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/spotlite/movie.rb', line 227 def cast full_credits.css('table.cast_list tr').reject do |row| # Skip 'Rest of cast' row row.children.size == 1 end.map do |row| imdb_id = row.at('td:nth-child(2) a')['href'].parse_imdb_id name = row.at('td:nth-child(2) a').text.strip_whitespace credits_text = row.last_element_child.text.strip_whitespace [imdb_id, name, 'Cast', credits_text] end.map do |values| Spotlite::Person.new(*values) end end |
#content_rating ⇒ Object
Returns content rating as a string
112 113 114 |
# File 'lib/spotlite/movie.rb', line 112 def details.at(".infobar meta[itemprop='contentRating']")['content'] rescue nil end |
#countries ⇒ Object
Returns a list of countries as an array of hashes with keys: code (string) and name (string)
123 124 125 126 127 128 129 130 |
# File 'lib/spotlite/movie.rb', line 123 def countries array = [] details.css("div.txt-block a[href^='/country/']").each do |node| array << {:code => node['href'].clean_href, :name => node.text.strip} end array end |
#credits ⇒ Object
Returns combined ‘cast` and `crew` as an array of Spotlite::Person objects
269 270 271 |
# File 'lib/spotlite/movie.rb', line 269 def credits cast + crew end |
#crew ⇒ Object
Combines all crew categories and returns an array of Spotlite::Person objects
264 265 266 |
# File 'lib/spotlite/movie.rb', line 264 def crew crew_categories.map{ |category| parse_crew(category) }.flatten end |
#crew_categories ⇒ Object
Returns available crew categories, e.g. “Art Department”, “Writing Credits”, or “Stunts”, as an array of strings
274 275 276 277 278 279 280 281 |
# File 'lib/spotlite/movie.rb', line 274 def crew_categories array = [] full_credits.css('h4.dataHeaderWithBorder').reject{ |h| h['id'] == 'cast' }.map do |node| array << (node.children.size > 1 ? node.children.first.text.strip_whitespace : node.children.text.strip_whitespace) end array end |
#critic_reviews ⇒ Object
Returns a list of critic reviews as an array of hashes with keys: source (string), author (string), excerpt (string), and score (integer)
312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/spotlite/movie.rb', line 312 def critic_reviews array = [] reviews.css("tr[itemprop='reviews']").map do |review| source = review.at("b[itemprop='publisher'] span[itemprop='name']").text = review.at("span[itemprop='author'] span[itemprop='name']").text excerpt = review.at("div[itemprop='reviewbody']").text.strip score = review.at("span[itemprop='ratingValue']").text.to_i array << { :source => source, :author => , :excerpt => excerpt, :score => score } end array end |
#description ⇒ Object
Returns short description as a string
96 97 98 99 |
# File 'lib/spotlite/movie.rb', line 96 def description desc = details.at("p[itemprop='description']").text.strip.clean_description rescue nil (desc.nil? || desc.empty?) ? nil : desc end |
#directors ⇒ Object
Returns a list of directors as an array of Spotlite::Person objects
200 201 202 |
# File 'lib/spotlite/movie.rb', line 200 def directors parse_crew('Directed by') end |
#genres ⇒ Object
Returns a list of genres as an array of strings
117 118 119 |
# File 'lib/spotlite/movie.rb', line 117 def genres details.css("div.infobar a[href^='/genre/']").map { |genre| genre.text } rescue [] end |
#images ⇒ Object
Returns URLs of movie still frames as an array of strings
327 328 329 330 331 332 333 334 335 336 337 338 |
# File 'lib/spotlite/movie.rb', line 327 def images array = [] still_frames.css('#media_index_thumbnail_grid img').map do |image| src = image['src'] rescue nil if src =~ /^(http:.+@@)/ || src =~ /^(http:.+?)\.[^\/]+$/ array << $1 + '.jpg' end end array end |
#keywords ⇒ Object
Returns a list of keywords as an array of strings
178 179 180 |
# File 'lib/spotlite/movie.rb', line 178 def keywords plot_keywords.css("a[href^='/keyword/']").map { |keyword| keyword.text.strip } rescue [] end |
#languages ⇒ Object
Returns a list of languages as an array of hashes with keys: code (string) and name (string)
134 135 136 137 138 139 140 141 |
# File 'lib/spotlite/movie.rb', line 134 def languages array = [] details.css("div.txt-block a[href^='/language/']").each do |node| array << {:code => node['href'].clean_href, :name => node.text.strip} end array end |
#metascore ⇒ Object
Returns Metascore rating as an integer
86 87 88 |
# File 'lib/spotlite/movie.rb', line 86 def details.at("div.star-box-details a[href^=criticreviews]").text.strip.split('/').first.to_i rescue nil end |
#original_title ⇒ Object
Returns original non-english title as a string
71 72 73 |
# File 'lib/spotlite/movie.rb', line 71 def original_title details.at("h1.header span.title-extra[itemprop='name']").children.first.text.gsub('"', '').strip rescue nil end |
#parse_crew(category) ⇒ Object
Returns a list of crew members of a certain category as an array Spotlite::Person objects
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/spotlite/movie.rb', line 243 def parse_crew(category) table = full_credits.search("[text()^='#{category}']").first.next_element rescue nil if table && table.name == 'table' table.css('tr').reject do |row| # Skip empty table rows with one non-braking space row.text.strip.size == 1 end.map do |row| imdb_id = row.first_element_child.at('a')['href'].parse_imdb_id name = row.first_element_child.at('a').text.strip_whitespace credits_text = row.last_element_child.text.strip_whitespace.clean_credits_text [imdb_id, name, category, credits_text] end.map do |values| Spotlite::Person.new(*values) end else [] end end |
#poster_url ⇒ Object
Returns primary poster URL as a string
149 150 151 152 153 154 155 |
# File 'lib/spotlite/movie.rb', line 149 def poster_url src = details.at('#img_primary img')['src'] rescue nil if src =~ /^(http:.+@@)/ || src =~ /^(http:.+?)\.[^\/]+$/ $1 + '.jpg' end end |
#producers ⇒ Object
Returns a list of producers as an array of Spotlite::Person objects
210 211 212 |
# File 'lib/spotlite/movie.rb', line 210 def producers parse_crew('Produced by') end |
#rating ⇒ Object
Returns IMDb rating as a float
81 82 83 |
# File 'lib/spotlite/movie.rb', line 81 def details.at("div.star-box-details span[itemprop='ratingValue']").text.to_f rescue nil end |
#recommended_movies ⇒ Object
Returns an array of recommended movies as an array of initialized objects of Movie class
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/spotlite/movie.rb', line 158 def recommended_movies details.css('.rec-title').reject do |node| # reject movies that don't have a release year yet node.at('span').nil? end.reject do |node| # reject everything other than featured film /\d{4}-\d{4}/.match(node.at('span').text) || /Series|Episode|Video|Documentary|Movie|Special|Short|Game|Unknown/.match(node.at('span').text) end.map do |node| imdb_id = node.at("a[href^='/title/tt']")['href'].parse_imdb_id title = node.at('a').text.strip year = node.at('span').text.parse_year [imdb_id, title, year] end.map do |values| Spotlite::Movie.new(*values) end end |
#release_date ⇒ Object
Returns original release date as a date
306 307 308 |
# File 'lib/spotlite/movie.rb', line 306 def release_date release_dates.first[:date] rescue nil end |
#release_dates ⇒ Object
Returns a list of regions and corresponding release dates as an array of hashes with keys: region code (string), region name (string), date (date), and comment (string) If day is unknown, 1st day of month is assigned If day and month are unknown, 1st of January is assigned
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/spotlite/movie.rb', line 288 def release_dates array = [] table = release_info.at('#release_dates') table.css('tr').map do |row| cells = row.css('td') code = cells.first.at('a')['href'].clean_href.split('=').last.downcase rescue nil region = cells.first.at('a').text rescue nil date = cells.at('.release_date').text.strip.parse_date comment = cells.last.text.strip.clean_release_comment comment = nil if comment.empty? array << { :code => code, :region => region, :date => date, :comment => comment } end unless table.nil? array end |
#runtime ⇒ Object
Returns runtime (length) in minutes as an integer
144 145 146 |
# File 'lib/spotlite/movie.rb', line 144 def runtime details.at("time[itemprop='duration']").text.gsub(',', '').to_i rescue nil end |
#stars ⇒ Object
Returns a list of starred actors as an array of Spotlite::Person objects
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/spotlite/movie.rb', line 215 def stars details.css("td#overview-top div[itemprop='actors'] a[href^='/name/nm']").map do |node| imdb_id = node['href'].parse_imdb_id name = node.text.strip [imdb_id, name] end.map do |values| Spotlite::Person.new(*values) end end |
#storyline ⇒ Object
Returns storyline as a string. Often is the same as description
102 103 104 |
# File 'lib/spotlite/movie.rb', line 102 def storyline details.at("#titleStoryLine div[itemprop='description'] p").text.strip.clean_description rescue nil end |
#summaries ⇒ Object
Returns a list of plot summaries as an array of strings
107 108 109 |
# File 'lib/spotlite/movie.rb', line 107 def summaries plot_summaries.css("p.plotSummary").map { |summary| summary.text.strip } end |
#technical ⇒ Object
Returns technical information like film length, aspect ratio, cameras, etc. as a hash of arrays of strings
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 |
# File 'lib/spotlite/movie.rb', line 341 def technical hash = {} table = technical_info.at_css('#technical_content table') rescue nil table.css('tr').map do |row| hash[row.css('td').first.text.strip] = row.css('td').last.children. map(&:text). map(&:strip_whitespace). reject(&:empty?). reject{|i| i == '|'}. slice_before{|i| /^[^\(]/.match i}. map{|i| i.join(' ')} end unless table.nil? hash end |
#title ⇒ Object
Returns title as a string
66 67 68 |
# File 'lib/spotlite/movie.rb', line 66 def title @title ||= details.at("h1.header span[itemprop='name']").text.strip rescue nil end |
#trivia ⇒ Object
Returns a list of trivia facts as an array of strings
183 184 185 |
# File 'lib/spotlite/movie.rb', line 183 def trivia movie_trivia.css("div.sodatext").map { |node| node.text.strip } rescue [] end |
#votes ⇒ Object
Returns number of votes as an integer
91 92 93 |
# File 'lib/spotlite/movie.rb', line 91 def votes details.at("div.star-box-details span[itemprop='ratingCount']").text.gsub(/[^\d+]/, '').to_i rescue nil end |
#writers ⇒ Object
Returns a list of writers as an array of Spotlite::Person objects
205 206 207 |
# File 'lib/spotlite/movie.rb', line 205 def writers parse_crew('Writing Credits') end |
#year ⇒ Object
Returns year of original release as an integer
76 77 78 |
# File 'lib/spotlite/movie.rb', line 76 def year @year ||= details.at("h1.header a[href^='/year/']").text.parse_year rescue nil end |