Class: Allocine::Movie

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, debug = false) ⇒ Movie

Returns a new instance of Movie.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/allocine/movie.rb', line 24

def initialize(id, debug=false)
  regexps = {
    :title => '<h1 class="TitleFilm">(.*?)<\/h1>',
    :directors => '<h3 class="SpProse">Réalisé par <a .*?>(.*?)<\/a><\/h3>',
    :nat => '<h3 class="SpProse">Film (.*?).&nbsp;</h3>',
    :genres => '<h3 class="SpProse">Genre : (.*?)</h3>',
    :out_date => '<h3 class="SpProse">Date de sortie : <b>(.*?)</b>',
    :duree => '<h3 class="SpProse">Dur\ée : (.*?).&nbsp;</h3>',
    :production_date => '<h3 class="SpProse">Année de production : (.*?)</h3>',
    :original_title => '<h3 class="SpProse">Titre original : <i>(.*?)</i></h3>',
    :actors => '<h3 class="SpProse">Avec (.*?) &nbsp;&nbsp;',
    :synopsis => '<td valign="top" style="padding:10 0 0 0"><div align="justify"><h4>(.*?)</h4>',
    :image => '<td valign="top" width="120".*?img src="(.*?)" border="0" alt=".*?" class="affichette" />',
    :interdit => '<h4 style="color: #D20000;">Interdit(.*?)</h4>'
  }
  str = open(MOVIE_DETAIL_URL % id).read.to_s
  data = Iconv.conv('UTF-8', 'ISO-8859-1', str)
  regexps.each do |reg|
    print "#{reg[0]}: " if debug
    r = data.scan Regexp.new(reg[1], Regexp::MULTILINE)
    r = r.first.to_s.strip
    r.gsub!(/<.*?>/, '')
    if r[0..0] == " "
      r = r.reverse.chop.reverse # that's a little bit ugly, but the only simple way i found to remove the first space in the out date
    end
    self.instance_variable_set("@#{reg[0]}", r)
    print "#{r}\n" if debug
  end
end

Instance Attribute Details

#actorsObject

Returns the value of attribute actors.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def actors
  @actors
end

#directorsObject

Returns the value of attribute directors.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def directors
  @directors
end

#dureeObject

Returns the value of attribute duree.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def duree
  @duree
end

#genresObject

Returns the value of attribute genres.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def genres
  @genres
end

#imageObject

Returns the value of attribute image.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def image
  @image
end

#interditObject

Returns the value of attribute interdit.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def interdit
  @interdit
end

#natObject

Returns the value of attribute nat.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def nat
  @nat
end

#original_titleObject

Returns the value of attribute original_title.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def original_title
  @original_title
end

#out_dateObject

Returns the value of attribute out_date.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def out_date
  @out_date
end

#production_dateObject

Returns the value of attribute production_date.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def production_date
  @production_date
end

#synopsisObject

Returns the value of attribute synopsis.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def synopsis
  @synopsis
end

#titleObject

Returns the value of attribute title.



3
4
5
# File 'lib/allocine/movie.rb', line 3

def title
  @title
end

Class Method Details

.find(search) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/allocine/movie.rb', line 5

def self.find(search)
  search.gsub!(' ', '+')
  str = open(MOVIE_SEARCH_URL % search).read.to_s
  data = Iconv.conv('UTF-8', 'ISO-8859-1', str)
  movies = {}
  while data =~ /<a href="\/film\/fichefilm_gen_cfilm=(\d+).html" class="link(\d+)">(.*?)<\/a>/i
    id, klass, name = $1, $2, $3
    data.gsub!("<a href=\"/film/fichefilm_gen_cfilm=#{id}.html\" class=\"link#{klass}\">#{name}</a>", "")
    name.gsub!(/<(.+?)>/,'')
    movies[id] = name
  end
  movies
end

.lucky_find(search) ⇒ Object



19
20
21
22
# File 'lib/allocine/movie.rb', line 19

def self.lucky_find(search)
  results = find(search)
  new(results.keys.first)
end