Class: Imdb::Base
- Inherits:
-
Object
- Object
- Imdb::Base
- Defined in:
- lib/imdb/base.rb
Overview
Represents something on IMDB.com
Instance Attribute Summary collapse
-
#also_known_as ⇒ Object
Returns alternative titles from imdb_url/releaseinfo.
-
#id ⇒ Object
Returns the value of attribute id.
-
#title(force_refresh = false) ⇒ Object
Returns a string containing the title.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
-
#cast_characters ⇒ Object
Returns an array with cast characters.
- #cast_member_ids ⇒ Object
-
#cast_members ⇒ Object
Returns an array with cast members.
-
#cast_members_characters(sep = '=>') ⇒ Object
Returns an array with cast members and characters.
-
#company ⇒ Object
Returns the company.
-
#countries ⇒ Object
Returns an array of countries as strings.
-
#director ⇒ Object
Returns the name of the director.
-
#filming_locations ⇒ Object
Returns filming locations from imdb_url/locations.
-
#genres ⇒ Object
Returns an array of genres (as strings).
-
#initialize(imdb_id, title = nil) ⇒ Base
constructor
Initialize a new IMDB movie object with it’s IMDB id (as a String).
-
#languages ⇒ Object
Returns an array of languages as strings.
-
#length ⇒ Object
Returns the duration of the movie in minutes as an integer.
-
#mpaa_rating ⇒ Object
Returns a string containing the mpaa rating and reason for rating.
-
#plot ⇒ Object
Returns a string containing the plot.
- #plot_summary ⇒ Object
-
#plot_synopsis ⇒ Object
Returns a string containing the plot summary.
-
#poster ⇒ Object
Returns a string containing the URL to the movie poster.
-
#rating ⇒ Object
Returns a float containing the average user rating.
-
#release_date ⇒ Object
Returns release date for the movie.
-
#tagline ⇒ Object
Returns a string containing the tagline.
-
#trailer_url ⇒ Object
Returns the url to the “Watch a trailer” page.
-
#votes ⇒ Object
Returns an int containing the number of user ratings.
-
#writers ⇒ Object
Returns the names of Writers.
-
#year ⇒ Object
Returns an integer containing the year (CCYY) the movie was released in.
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).
16 17 18 19 20 |
# File 'lib/imdb/base.rb', line 16 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_as ⇒ Object
Returns alternative titles from imdb_url/releaseinfo
166 167 168 |
# File 'lib/imdb/base.rb', line 166 def also_known_as @also_known_as end |
#id ⇒ Object
Returns the value of attribute id.
6 7 8 |
# File 'lib/imdb/base.rb', line 6 def id @id end |
#title(force_refresh = false) ⇒ Object
Returns a string containing the title
142 143 144 |
# File 'lib/imdb/base.rb', line 142 def title @title end |
#url ⇒ Object
Returns the value of attribute url.
6 7 8 |
# File 'lib/imdb/base.rb', line 6 def url @url end |
Instance Method Details
#cast_characters ⇒ Object
Returns an array with cast characters
32 33 34 |
# File 'lib/imdb/base.rb', line 32 def cast_characters document.search("table.cast td.char").map { |link| link.content.strip } rescue [] end |
#cast_member_ids ⇒ Object
27 28 29 |
# File 'lib/imdb/base.rb', line 27 def cast_member_ids document.search("table.cast td.nm a").map {|l| l['href'].sub(%r{^/name/(.*)/}, '\1') } end |
#cast_members ⇒ Object
Returns an array with cast members
23 24 25 |
# File 'lib/imdb/base.rb', line 23 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
37 38 39 40 41 42 43 44 45 |
# File 'lib/imdb/base.rb', line 37 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 |
#company ⇒ Object
Returns the company
90 91 92 |
# File 'lib/imdb/base.rb', line 90 def company document.search("h5[text()='Company:'] ~ div a[@href*='/company/']").map { |link| link.content.strip }.first rescue nil end |
#countries ⇒ Object
Returns an array of countries as strings.
80 81 82 |
# File 'lib/imdb/base.rb', line 80 def countries document.search("h5[text()='Country:'] ~ div a[@href*='/country/']").map { |link| link.content.strip } rescue [] end |
#director ⇒ Object
Returns the name of the director
48 49 50 |
# File 'lib/imdb/base.rb', line 48 def director document.search("h5[text()^='Director'] ~ div a").map { |link| link.content.strip } rescue [] end |
#filming_locations ⇒ Object
Returns filming locations from imdb_url/locations
161 162 163 |
# File 'lib/imdb/base.rb', line 161 def filming_locations locations_document.search("#filming_locations_content .soda dt a").map { |link| link.content.strip } rescue [] end |
#genres ⇒ Object
Returns an array of genres (as strings)
70 71 72 |
# File 'lib/imdb/base.rb', line 70 def genres document.search("h5[text()='Genre:'] ~ div a[@href*='/Sections/Genres/']").map { |link| link.content.strip } rescue [] end |
#languages ⇒ Object
Returns an array of languages as strings.
75 76 77 |
# File 'lib/imdb/base.rb', line 75 def languages document.search("h5[text()='Language:'] ~ div a[@href*='/language/']").map { |link| link.content.strip } rescue [] end |
#length ⇒ Object
Returns the duration of the movie in minutes as an integer.
85 86 87 |
# File 'lib/imdb/base.rb', line 85 def length document.at("h5[text()='Runtime:'] ~ div").content[/\d+ min/].to_i rescue nil end |
#mpaa_rating ⇒ Object
Returns a string containing the mpaa rating and reason for rating
137 138 139 |
# File 'lib/imdb/base.rb', line 137 def document.at("//a[starts-with(.,'MPAA')]/../following-sibling::*").content.strip rescue nil end |
#plot ⇒ Object
Returns a string containing the plot.
95 96 97 |
# File 'lib/imdb/base.rb', line 95 def plot sanitize_plot(document.at("h5[text()='Plot:'] ~ div").content) rescue nil end |
#plot_summary ⇒ Object
105 106 107 108 |
# File 'lib/imdb/base.rb', line 105 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_synopsis ⇒ Object
Returns a string containing the plot summary
100 101 102 103 |
# File 'lib/imdb/base.rb', line 100 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 |
#poster ⇒ Object
Returns a string containing the URL to the movie poster.
111 112 113 114 115 116 117 118 119 |
# File 'lib/imdb/base.rb', line 111 def poster src = document.at("a[@name='poster'] img")['src'] rescue nil case src when /^(http:.+@@)/ $1 + '.jpg' when /^(http:.+?)\.[^\/]+$/ $1 + '.jpg' end end |
#rating ⇒ Object
Returns a float containing the average user rating
122 123 124 |
# File 'lib/imdb/base.rb', line 122 def document.at(".starbar-meta b").content.split('/').first.strip.to_f rescue nil end |
#release_date ⇒ Object
Returns release date for the movie.
156 157 158 |
# File 'lib/imdb/base.rb', line 156 def release_date sanitize_release_date(document.at("h5[text()*='Release Date'] ~ div").content) rescue nil end |
#tagline ⇒ Object
Returns a string containing the tagline
132 133 134 |
# File 'lib/imdb/base.rb', line 132 def tagline document.search("h5[text()='Tagline:'] ~ div").first.inner_html.gsub(/<.+>.+<\/.+>/, '').strip.imdb_unescape_html rescue nil end |
#trailer_url ⇒ Object
Returns the url to the “Watch a trailer” page
65 66 67 |
# File 'lib/imdb/base.rb', line 65 def trailer_url 'http://imdb.com' + document.at("a[@href*='/video/screenplay/']")["href"] rescue nil end |
#votes ⇒ Object
Returns an int containing the number of user ratings
127 128 129 |
# File 'lib/imdb/base.rb', line 127 def votes document.at("#tn15rating .tn15more").content.strip.gsub(/[^\d+]/, "").to_i rescue nil end |
#writers ⇒ Object
Returns the names of Writers
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/imdb/base.rb', line 53 def writers writers_list = Array.new i = 0 fullcredits_document.search("h4[text()^='Writing Credits'] + table tbody tr td[class='name']").map {|name| writers_list[i] = name.content.strip if !writers_list.include? name.content.strip i=i+1 } rescue [] writers_list end |
#year ⇒ Object
Returns an integer containing the year (CCYY) the movie was released in.
151 152 153 |
# File 'lib/imdb/base.rb', line 151 def year document.at("a[@href^='/year/']").content.to_i rescue nil end |