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.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/imdb/movie.rb', line 10

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,
        :genres => Array,
        :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

#castArray

Get movie cast listing

Returns:



41
42
43
44
45
46
47
48
49
# File 'lib/imdb/movie.rb', line 41

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
    profile_id = link.children[1].search('a[@href^="/name/nm"]').first["href"] rescue nil
    char = link.children[3].content.strip rescue nil
    IMDB::Person.new(@imdb_id, name, char, profile_id, picture) unless name.nil? and char.nil? and picture.nil? and profile_id.nil?
  end
end

#directorString

Get Director

Returns:

  • (String)


73
74
75
# File 'lib/imdb/movie.rb', line 73

def director
  doc.xpath("//h5[contains(., 'Director')]/..").at("a").content rescue nil
end

#genresArray

Genre List

Returns:



79
80
81
82
83
84
85
# File 'lib/imdb/movie.rb', line 79

def genres
  doc.xpath("//h5[contains(., 'Genre')]/..").search("a").map { |g|
    g.content unless g.content =~ /See more/
    }.compact
  rescue
    nil
end

#photosArray

Get movie photos

Returns:



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/imdb/movie.rb', line 53

def photos
  begin
    doc.search("img").map { |img|
      unless img["src"][/_CR/].nil?
        img["src"]
      end
    }
  rescue
    nil
  end
end

#posterString

Get movie poster address

Returns:

  • (String)


29
30
31
# File 'lib/imdb/movie.rb', line 29

def poster
  doc.at("a[@name='poster'] img")['src'][/http:.+/] + '.jpg' rescue nil
end

#release_dateString

Get release date

Returns:

  • (String)


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

def release_date
  Date.parse(Chronic.parse(doc.xpath("//h5[contains(., 'Release Date')]/..").first.content[/^\d{1,2} \w+ \d{4}/]).strftime('%Y/%m/%d')).to_s rescue nil
end

#titleString

Get movie title

Returns:

  • (String)


35
36
37
# File 'lib/imdb/movie.rb', line 35

def title
  doc.at("//head/meta[@name='title']")["content"].split(/\(\d+\)/)[0].strip!
end

#writersArray

Writer List

Returns:



89
90
91
92
93
94
95
# File 'lib/imdb/movie.rb', line 89

def writers
  doc.xpath("//a[@name='writers']/../../../..").search('a[@href^="/name/nm"]').map {|w|
    name = w.content
    profile = w['href']
    IMDB::Person.new(@imdb_id, name, "nil", profile, "nil")
  }
end