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)



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

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)



209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/spotlite/movie.rb', line 209

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']").text.strip.clean_description rescue nil
end

#directorsObject

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



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

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

#imagesObject

Returns URLs of movie still frames as an array of strings



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/spotlite/movie.rb', line 224

def images
  array = []
  still_frames.css(".thumb_list img").map do |image|
    src = image["src"] rescue nil
  
    if src =~ /^(http:.+@@)/ || src =~ /^(http:.+?)\.[^\/]+$/
      array << $1 + ".jpg"
    end
  end
  
  array
end

#keywordsObject

Returns a list of keywords as an array of strings



124
125
126
# File 'lib/spotlite/movie.rb', line 124

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)



147
148
149
# File 'lib/spotlite/movie.rb', line 147

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

Returns an array of recommended movies as an array of initialized objects of Movie class



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/spotlite/movie.rb', line 111

def recommended_movies
  details.css(".rec-title").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_dateObject

Returns original release date as a date



203
204
205
# File 'lib/spotlite/movie.rb', line 203

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



187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/spotlite/movie.rb', line 187

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)



170
171
172
173
174
175
176
177
178
179
180
# File 'lib/spotlite/movie.rb', line 170

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").text.strip.clean_description rescue nil
end

#triviaObject

Returns a list of trivia facts as an array of strings



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

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)



141
142
143
# File 'lib/spotlite/movie.rb', line 141

def writers
  parse_staff(:writers)
end