Class: Spotlite::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/spotlite/movie.rb

Overview

Represents a movie on IMDb.com

Instance Attribute Summary collapse

Instance Method Summary collapse

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



16
17
18
19
20
21
# File 'lib/spotlite/movie.rb', line 16

def initialize(imdb_id, title = nil, year = nil)
  @imdb_id = imdb_id
  @title   = title
  @year    = year
  @url     = "http://www.imdb.com/title/tt#{imdb_id}/"
end

Instance Attribute Details

#imdb_idObject

Returns the value of attribute imdb_id.



5
6
7
# File 'lib/spotlite/movie.rb', line 5

def imdb_id
  @imdb_id
end

#titleObject

Returns title as a string



24
25
26
# File 'lib/spotlite/movie.rb', line 24

def title
  @title
end

#yearObject

Returns year of original release as an integer



34
35
36
# File 'lib/spotlite/movie.rb', line 34

def year
  @year
end

Instance Method Details

#castObject

Returns a list of actors as an array of hashes with keys: imdb_id (string), name (string), and character (string)



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/spotlite/movie.rb', line 140

def cast
  table = full_credits.css("table.cast")
  names = table.css("td.nm").map { |node| node.text } rescue []
  links = table.css("td.nm a").map { |node| node["href"] } rescue []
  imdb_ids = links.map { |link| link.parse_imdb_id } unless links.empty?
  characters = table.css("td.char").map { |node| node.text }
  
  array = []
  0.upto(names.size - 1) do |i|
    array << {:imdb_id => imdb_ids[i], :name => names[i], :character => characters[i]}
  end
  
  array
end

#content_ratingObject

Returns content rating as a string



64
65
66
# File 'lib/spotlite/movie.rb', line 64

def content_rating
  details.at(".infobar span[itemprop='contentRating']")['title'] rescue nil
end

#countriesObject

Returns a list of countries as an array of hashes with keys: code (string) and name (string)



75
76
77
78
79
80
81
82
# File 'lib/spotlite/movie.rb', line 75

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

#critic_reviewsObject

Returns a list of critic reviews as an array of hashes with keys: source (string), author (string), excerpt (string), and score (integer)



196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/spotlite/movie.rb', line 196

def critic_reviews
  array = []
  reviews.css("tr[itemprop='reviews']").map do |review|
    source = review.at("b[itemprop='publisher'] span[itemprop='name']").text
    author = 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 => author, :excerpt => excerpt, :score => score}
  end
  
  array
end

#descriptionObject

Returns short description as a string



54
55
56
# File 'lib/spotlite/movie.rb', line 54

def description
  details.at("p[itemprop='description']").children.first.text.strip rescue nil
end

#directorsObject

Returns a list of directors as an array of hashes with keys: imdb_id (string) and name (string)



122
123
124
# File 'lib/spotlite/movie.rb', line 122

def directors
  parse_staff(:directors)
end

#genresObject

Returns a list of genres as an array of strings



69
70
71
# File 'lib/spotlite/movie.rb', line 69

def genres
  details.css("div.infobar a[href^='/genre/']").map { |genre| genre.text } rescue []
end

#keywordsObject

Returns a list of keywords as an array of strings



111
112
113
# File 'lib/spotlite/movie.rb', line 111

def keywords
  plot_keywords.css("a[href^='/keyword/']").map { |keyword| keyword.text.strip } rescue []
end

#languagesObject

Returns a list of languages as an array of hashes with keys: code (string) and name (string)



86
87
88
89
90
91
92
93
# File 'lib/spotlite/movie.rb', line 86

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

#metascoreObject

Returns Metascore rating as an integer



44
45
46
# File 'lib/spotlite/movie.rb', line 44

def metascore
  details.at("div.star-box-details a[href^=criticreviews]").text.strip.split("/").first.to_i rescue nil
end

#original_titleObject

Returns original non-english title as a string



29
30
31
# File 'lib/spotlite/movie.rb', line 29

def original_title
  details.at("h1.header span.title-extra[itemprop='name']").children.first.text.gsub('"', "").strip rescue nil
end

#poster_urlObject

Returns primary poster URL as a string



102
103
104
105
106
107
108
# File 'lib/spotlite/movie.rb', line 102

def poster_url
  src = details.at("#img_primary img")["src"] rescue nil
  
  if src =~ /^(http:.+@@)/ || src =~ /^(http:.+?)\.[^\/]+$/
    $1 + ".jpg"
  end
end

#producersObject

Returns a list of producers as an array of hashes with keys: imdb_id (string) and name (string)



134
135
136
# File 'lib/spotlite/movie.rb', line 134

def producers
  parse_staff(:producers)
end

#ratingObject

Returns IMDb rating as a float



39
40
41
# File 'lib/spotlite/movie.rb', line 39

def rating
  details.at("div.star-box-details span[itemprop='ratingValue']").text.to_f rescue nil
end

#release_dateObject

Returns original release date as a date



190
191
192
# File 'lib/spotlite/movie.rb', line 190

def release_date
  release_dates.first[:date] rescue nil
end

#release_datesObject

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



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/spotlite/movie.rb', line 174

def release_dates
  array = []
  release_info.at("#release_dates").css("tr").map do |row|
    code = row.at("a")["href"].clean_href.split("=").last.downcase rescue nil
    region = row.at("a").text rescue nil
    date = row.at("td.release_date").text.strip.parse_date rescue nil
    comment = row.css("td").last.text.strip.clean_release_comment rescue nil
    comment = nil if comment.empty?
    
    array << {:code => code, :region => region, :date => date, :comment => comment}
  end
  
  array
end

#runtimeObject

Returns runtime (length) in minutes as an integer



96
97
98
99
# File 'lib/spotlite/movie.rb', line 96

def runtime
  details.at("time[itemprop='duration']").text.to_i rescue nil ||
  details.at("#overview-top .infobar").text.strip[/\d{2,3} min/].to_i rescue nil
end

#starsObject

Returns a list of starred actors as an array of hashes with keys: imdb_id (string) and name (string)



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/spotlite/movie.rb', line 157

def stars
  array = []
  details.css("td#overview-top div[itemprop='actors'] a[href^='/name/nm']").map do |node|
    name = node.text.strip
    imdb_id = node["href"].parse_imdb_id
    
    array << {:imdb_id => imdb_id, :name => name}
  end
  
  array
end

#storylineObject

Returns storyline as a string. Often is the same as description



59
60
61
# File 'lib/spotlite/movie.rb', line 59

def storyline
  details.at("#titleStoryLine div[itemprop='description'] p").children.first.text.strip rescue nil
end

#triviaObject

Returns a list of trivia facts as an array of strings



116
117
118
# File 'lib/spotlite/movie.rb', line 116

def trivia
  movie_trivia.css("div.sodatext").map { |node| node.text.strip } rescue []
end

#votesObject

Returns number of votes as an integer



49
50
51
# File 'lib/spotlite/movie.rb', line 49

def votes
  details.at("div.star-box-details span[itemprop='ratingCount']").text.gsub(/[^\d+]/, "").to_i rescue nil
end

#writersObject

Returns a list of writers as an array of hashes with keys: imdb_id (string) and name (string)



128
129
130
# File 'lib/spotlite/movie.rb', line 128

def writers
  parse_staff(:writers)
end