Class: Imdb::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/imdb/base.rb

Overview

Represents something on IMDB.com

Direct Known Subclasses

Episode, Movie, Serie

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(imdb_id, title = nil) ⇒ Base

Initialize a new IMDB movie object with it’s IMDB id (as a String)

movie = Imdb::Movie.new("0095016")

Imdb::Movie objects are lazy loading, meaning that no HTTP request will be performed when a new object is created. Only when you use an accessor that needs the remote data, a HTTP request is made (once).



15
16
17
18
19
# File 'lib/imdb/base.rb', line 15

def initialize(imdb_id, title = nil)
  @id = imdb_id
  @url = "http://akas.imdb.com/title/tt#{imdb_id}/combined"
  @title = title.gsub(/"/, "").strip if title
end

Instance Attribute Details

#also_known_asObject

Returns the value of attribute also_known_as.



5
6
7
# File 'lib/imdb/base.rb', line 5

def also_known_as
  @also_known_as
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/imdb/base.rb', line 5

def id
  @id
end

#title(force_refresh = false) ⇒ Object

Returns a string containing the title



129
130
131
# File 'lib/imdb/base.rb', line 129

def title
  @title
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/imdb/base.rb', line 5

def url
  @url
end

Instance Method Details

#cast_charactersObject

Returns an array with cast characters



31
32
33
# File 'lib/imdb/base.rb', line 31

def cast_characters
  document.search("table.cast td.char").map { |link| link.content.strip } rescue []
end

#cast_member_idsObject



26
27
28
# File 'lib/imdb/base.rb', line 26

def cast_member_ids
  document.search("table.cast td.nm a").map {|l| l['href'].sub(%r{^/name/(.*)/}, '\1') }
end

#cast_membersObject

Returns an array with cast members



22
23
24
# File 'lib/imdb/base.rb', line 22

def cast_members
  document.search("table.cast td.nm a").map { |link| link.content.strip } rescue []
end

#cast_members_characters(sep = '=>') ⇒ Object

Returns an array with cast members and characters



36
37
38
39
40
41
42
43
44
# File 'lib/imdb/base.rb', line 36

def cast_members_characters(sep = '=>')
  memb_char = Array.new
  i = 0
  self.cast_members.each{|m|
    memb_char[i] = "#{self.cast_members[i]} #{sep} #{self.cast_characters[i]}"
    i=i+1
  }
  memb_char
end

#companyObject

Returns the company



77
78
79
# File 'lib/imdb/base.rb', line 77

def company
  document.search("h5[text()='Company:'] ~ div a[@href*='/company/']").map { |link| link.content.strip }.first rescue nil
end

#countriesObject

Returns an array of countries as strings.



67
68
69
# File 'lib/imdb/base.rb', line 67

def countries
  document.search("h5[text()='Country:'] ~ div a[@href*='/country/']").map { |link| link.content.strip } rescue []
end

#directorObject

Returns the name of the director



47
48
49
# File 'lib/imdb/base.rb', line 47

def director
  document.search("h5[text()^='Director'] ~ div a").map { |link| link.content.strip } rescue []
end

#filming_locationsObject

Returns filming locations from imdb_url/locations



148
149
150
# File 'lib/imdb/base.rb', line 148

def filming_locations
  locations_document.search("#filming_locations_content .soda dt a").map { |link| link.content.strip } rescue []
end

#genresObject

Returns an array of genres (as strings)



57
58
59
# File 'lib/imdb/base.rb', line 57

def genres
  document.search("h5[text()='Genre:'] ~ div a[@href*='/Sections/Genres/']").map { |link| link.content.strip } rescue []
end

#languagesObject

Returns an array of languages as strings.



62
63
64
# File 'lib/imdb/base.rb', line 62

def languages
  document.search("h5[text()='Language:'] ~ div a[@href*='/language/']").map { |link| link.content.strip } rescue []
end

#lengthObject

Returns the duration of the movie in minutes as an integer.



72
73
74
# File 'lib/imdb/base.rb', line 72

def length
  document.at("h5[text()='Runtime:'] ~ div").content[/\d+ min/].to_i rescue nil
end

#mpaa_ratingObject

Returns a string containing the mpaa rating and reason for rating



124
125
126
# File 'lib/imdb/base.rb', line 124

def mpaa_rating
  document.at("//a[starts-with(.,'MPAA')]/../following-sibling::*").content.strip rescue nil
end

#plotObject

Returns a string containing the plot.



82
83
84
# File 'lib/imdb/base.rb', line 82

def plot
  sanitize_plot(document.at("h5[text()='Plot:'] ~ div").content) rescue nil
end

#plot_summaryObject



92
93
94
95
# File 'lib/imdb/base.rb', line 92

def plot_summary
  doc = Nokogiri::HTML(Imdb::Movie.find_by_id(@id, :plotsummary))
  doc.at("p.plotSummary").inner_html.gsub(/<i.*/im, '').strip.imdb_unescape_html rescue nil
end

#plot_synopsisObject

Returns a string containing the plot summary



87
88
89
90
# File 'lib/imdb/base.rb', line 87

def plot_synopsis
  doc = Nokogiri::HTML(Imdb::Movie.find_by_id(@id, :synopsis))
  doc.at("div[@id='swiki.2.1']").content.strip rescue nil
end

#posterObject

Returns a string containing the URL to the movie poster.



98
99
100
101
102
103
104
105
106
# File 'lib/imdb/base.rb', line 98

def poster
  src = document.at("a[@name='poster'] img")['src'] rescue nil
  case src
  when /^(http:.+@@)/
    $1 + '.jpg'
  when /^(http:.+?)\.[^\/]+$/
    $1 + '.jpg'
  end
end

#ratingObject

Returns a float containing the average user rating



109
110
111
# File 'lib/imdb/base.rb', line 109

def rating
  document.at(".starbar-meta b").content.split('/').first.strip.to_f rescue nil
end

#release_dateObject

Returns release date for the movie.



143
144
145
# File 'lib/imdb/base.rb', line 143

def release_date
  sanitize_release_date(document.at("h5[text()*='Release Date'] ~ div").content) rescue nil
end

#taglineObject

Returns a string containing the tagline



119
120
121
# File 'lib/imdb/base.rb', line 119

def tagline
  document.search("h5[text()='Tagline:'] ~ div").first.inner_html.gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil
end

#trailer_urlObject

Returns the url to the “Watch a trailer” page



52
53
54
# File 'lib/imdb/base.rb', line 52

def trailer_url
  'http://imdb.com' + document.at("a[@href*='/video/screenplay/']")["href"] rescue nil
end

#votesObject

Returns an int containing the number of user ratings



114
115
116
# File 'lib/imdb/base.rb', line 114

def votes
  document.at("#tn15rating .tn15more").content.strip.gsub(/[^\d+]/, "").to_i rescue nil
end

#yearObject

Returns an integer containing the year (CCYY) the movie was released in.



138
139
140
# File 'lib/imdb/base.rb', line 138

def year
  document.at("a[@href^='/year/']").content.to_i rescue nil
end