Class: IMDB::Movie

Inherits:
Skeleton show all
Defined in:
lib/imdb/movie.rb

Overview

Get movie information with IMDB movie id.

Examples:

Get Yahsi Bati movie title and cast listing [www.imdb.com/title/tt1567448/]

m = IMDB::Movie.new('1567448')
puts m.title

Instance Attribute Summary collapse

Attributes inherited from Skeleton

#method_names, #model

Instance Method Summary collapse

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_idObject

Returns the value of attribute imdb_id.



7
8
9
# File 'lib/imdb/movie.rb', line 7

def imdb_id
  @imdb_id
end

Returns the value of attribute link.



7
8
9
# File 'lib/imdb/movie.rb', line 7

def link
  @link
end

Instance Method Details

#castCast[]

Get movie cast listing

Returns:



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

#directorString

Get Director

Returns:

  • (String)


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

def director
  self.director_person.name rescue nil
end

#director_personPerson

Get Director Person class

Returns:



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

#genresArray

Genre List

Returns:



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

#movielengthInteger

Get the movielength of the movie in minutes

Returns:

  • (Integer)


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

#photosArray

Get movie photos

Returns:



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

#posterString

Get movie poster address

Returns:

  • (String)


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

#ratingFloat

Writer List

Returns:

  • (Float)


125
126
127
128
129
# File 'lib/imdb/movie.rb', line 125

def rating
  @rating ||= doc.search(".star-box-giga-star").text.strip.to_f
rescue
  nil
end

#release_dateString

Get release date

Returns:

  • (String)


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_descriptionString

Returns:

  • (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

#titleString

Get movie title

Returns:

  • (String)


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

#writersArray

Writer List

Returns:



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