Class: IMDB::Movie

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Movie

Returns a new instance of Movie.



6
7
8
9
10
11
12
# File 'lib/imdb/movie.rb', line 6

def initialize(attributes={})
  self.extra = {}

  attributes.each_pair do |key, value|
    self.instance_variable_set "@#{key}", value
  end
end

Instance Attribute Details

#akaObject

Returns the value of attribute aka.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def aka
  @aka
end

#directorObject

Returns the value of attribute director.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def director
  @director
end

#extraObject

Returns the value of attribute extra.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def extra
  @extra
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def id
  @id
end

#titleObject

Returns the value of attribute title.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def title
  @title
end

#urlObject

Returns the value of attribute url.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def url
  @url
end

#yearObject

Returns the value of attribute year.



4
5
6
# File 'lib/imdb/movie.rb', line 4

def year
  @year
end

Class Method Details

.new_from_doc(doc) ⇒ Object



14
15
16
17
18
# File 'lib/imdb/movie.rb', line 14

def self.new_from_doc(doc)
  movie = self.new
  movie.parse_doc(doc)
  movie
end

.new_from_id(id) ⇒ Object



20
21
22
23
24
25
# File 'lib/imdb/movie.rb', line 20

def self.new_from_id(id)
  movie = self.new
  movie.id = id.to_s
  movie.get_full_details
  movie
end

Instance Method Details

#get_full_detailsObject



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

def get_full_details
  doc = Hpricot(open(IMDB::TITLE_URL+CGI.escape(self.id)))
  self.parse_doc(doc)
  self
end

#parse_doc(doc) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/imdb/movie.rb', line 33

def parse_doc(doc)
  akalink = (doc/"a[@href$='releaseinfo#akas']")[0]

  self.id    = (doc/"a[@href$='fullcredits]")[0].attributes["href"][/title\/(.*?)\//, 1]
  self.title = IMDB.str_to_utf8(CGI.unescapeHTML((doc/"h1").inner_html[/^(.*?)(?: <span)/, 1]))
  self.year  = (doc/"h1 a[@href^='/Sections/Years/']").inner_html
  self.aka   = akalink ? akalink.parent.inner_html.scan(/(?:>)([^<]*?)\(/).collect { |x| IMDB.str_to_utf8(CGI.unescapeHTML(x[0].strip)) } : []

  parse_full_details(doc)

  self
end

#parse_full_details(doc) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/imdb/movie.rb', line 46

def parse_full_details(doc)
  director_links = (doc/"#director-info/a")
  writer_links   = (doc/"a[@onclick*='writerlist']")
  tagline_header = (doc/"h5[text()='Tagline:']")[0]
  plot_link      = (doc/"a[@href$='/plotsummary]")[0]
  mpaa_link      = (doc/"a[@href='/mpaa']")[0]

  if director_links.length > 0
    self.director = director_links.collect { |l| IMDB.str_to_utf8(CGI.unescapeHTML(l.inner_html)) }
  end

  self.extra["writers"]     = writer_links.collect { |w| IMDB.str_to_utf8(CGI.unescapeHTML(w.inner_html)) } if writer_links
  self.extra["tagline"]     = IMDB.str_to_utf8(CGI.unescapeHTML(tagline_header.parent.inner_html[/\/h5>(.+?)(<|$)/m, 1].strip)) if tagline_header
  self.extra["plot"]        = IMDB.str_to_utf8(CGI.unescapeHTML(plot_link.parent.inner_html[/\/h5>(.+?)<a/m, 1].strip)) if plot_link
  self.extra["mpaa_rating"] = IMDB.str_to_utf8(CGI.unescapeHTML(mpaa_link.parent.parent.inner_html[/\/h5>(.+)$/m, 1].strip)) if mpaa_link
  self.extra["cast"]        = ((doc/"table.cast")[0]/"tr").collect { |tr| { "actor" => IMDB.str_to_utf8(CGI.unescapeHTML((tr/"td.nm")[0].inner_text)), "character" => IMDB.str_to_utf8(CGI.unescapeHTML((tr/"td.char")[0].inner_text)) } }

  self
end

#to_json(*a) ⇒ Object



71
72
73
74
75
76
77
78
79
80
# File 'lib/imdb/movie.rb', line 71

def to_json(*a)
  {
    "id"       => self.id,
    "title"    => self.title,
    "year"     => self.year,
    "aka"      => self.aka,
    "director" => self.director,
    "extra"    => self.extra
  }.to_json(*a)
end