Class: IMDB::Movie
Overview
Get movie information with IMDB movie id.
Instance Attribute Summary collapse
-
#imdb_id ⇒ Object
Returns the value of attribute imdb_id.
-
#link ⇒ Object
Returns the value of attribute link.
Attributes inherited from Skeleton
Instance Method Summary collapse
-
#cast ⇒ Cast[]
Get movie cast listing.
-
#director ⇒ String
Get Director.
-
#director_person ⇒ Person
Get Director Person class.
-
#genres ⇒ Array
Genre List.
-
#initialize(id_of) ⇒ Movie
constructor
A new instance of Movie.
-
#movielength ⇒ Integer
Get the movielength of the movie in minutes.
-
#photos ⇒ Array
Get movie photos.
-
#poster ⇒ String
Get movie poster address.
-
#rating ⇒ Float
Writer List.
-
#release_date ⇒ String
Get release date.
- #short_description ⇒ String
-
#title ⇒ String
Get movie title.
-
#writers ⇒ Array
Writer List.
Methods inherited from Skeleton
json_create, #to_hash, #to_json
Constructor Details
#initialize(id_of) ⇒ Movie
Returns a new instance of Movie.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/imdb/movie.rb', line 9 def initialize(id_of) # !!!DON'T FORGET DEFINE NEW METHODS IN SUPER!!! super("Movie", { :imdb_id => String, :poster => String, :title => String, :release_date => String, :cast => Array, :photos => Array, :director => String, :director_person => Person, :genres => Array, :rating => Float, :movielength => Integer, :short_description => String, :writers => Array }, [:imdb_id]) @imdb_id = id_of @link = "http://www.imdb.com/title/tt#{@imdb_id}" end |
Instance Attribute Details
#imdb_id ⇒ Object
Returns the value of attribute imdb_id.
7 8 9 |
# File 'lib/imdb/movie.rb', line 7 def imdb_id @imdb_id end |
#link ⇒ Object
Returns the value of attribute link.
7 8 9 |
# File 'lib/imdb/movie.rb', line 7 def link @link end |
Instance Method Details
#cast ⇒ Cast[]
Get movie cast listing
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/imdb/movie.rb', line 54 def cast doc.search("table.cast tr").map do |link| #picture = link.children[0].search("img")[0]["src"] rescue nil #name = link.children[1].content.strip rescue nil id = link.children[1].search('a[@href^="/name/nm"]').first["href"].match(/\/name\/nm([0-9]+)/)[1] rescue nil char = link.children[3].content.strip rescue nil unless id.nil? person = IMDB::Person.new(id) IMDB::Cast.new(self, person, char) end end.compact end |
#director ⇒ String
Get Director
97 98 99 |
# File 'lib/imdb/movie.rb', line 97 def director self.director_person.name rescue nil end |
#director_person ⇒ Person
Get Director Person class
103 104 105 106 107 108 109 110 111 |
# File 'lib/imdb/movie.rb', line 103 def director_person begin link=doc.xpath("//h4[contains(., 'Director')]/..").at('a[@href^="/name/nm"]') profile = link['href'].match(/\/name\/nm([0-9]+)/)[1] rescue nil IMDB::Person.new(profile) unless profile.nil? rescue nil end end |
#genres ⇒ Array
Genre List
115 116 117 118 119 120 121 |
# File 'lib/imdb/movie.rb', line 115 def genres doc.xpath("//h4[contains(., 'Genre')]/..").search("a").map { |g| g.content.strip unless g.content =~ /See more/ }.compact rescue nil end |
#movielength ⇒ Integer
Get the movielength of the movie in minutes
133 134 135 |
# File 'lib/imdb/movie.rb', line 133 def movielength doc.at("//h4[text()='Runtime:']/..").inner_html[/\d+ min/].to_i rescue nil end |
#photos ⇒ Array
Get movie photos
69 70 71 72 73 74 75 |
# File 'lib/imdb/movie.rb', line 69 def photos begin doc.search('#main .media_index_thumb_list img').map { |i| i["src"] } rescue nil end end |
#poster ⇒ String
Get movie poster address
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/imdb/movie.rb', line 32 def poster src = doc.at("#img_primary img")["src"] rescue nil unless src.nil? if src.match(/\._V1/) return src.match(/(.*)\._V1.*(.jpg)/)[1, 2].join else return src end end src end |
#rating ⇒ Float
Writer List
125 126 127 128 129 |
# File 'lib/imdb/movie.rb', line 125 def @rating ||= doc.search(".star-box-giga-star").text.strip.to_f rescue nil end |
#release_date ⇒ String
Get release date
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/imdb/movie.rb', line 79 def release_date if (node =doc.css('.infobar span.nobr meta[itemprop="datePublished"]')).length > 0 date = node.first['content'] if date.match /^\d{4}$/ "#{date}-01-01" else Date.parse(date).to_s end else year = doc.at("h1.header .nobr").text[/\d{4}/] "#{year}-01-01" end rescue nil end |
#short_description ⇒ String
147 148 149 |
# File 'lib/imdb/movie.rb', line 147 def short_description doc.at("#overview-top p[itemprop=description]").try(:text).try(:strip) end |
#title ⇒ String
Get movie title
46 47 48 49 50 |
# File 'lib/imdb/movie.rb', line 46 def title doc.at("//head/meta[@name='title']")["content"].split(/\(.*\)/)[0].strip! || doc.at("h1.header").children.first.text.strip end |
#writers ⇒ Array
Writer List
139 140 141 142 143 144 |
# File 'lib/imdb/movie.rb', line 139 def writers doc.css("h4:contains('Writing')").first.next_element.css('a[@href^="/name/nm"]').map { |w| profile = w['href'].match(/\/name\/nm([0-9]+)/)[1] rescue nil IMDB::Person.new(profile) unless profile.nil? } end |