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.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/allocine/movie.rb', line 58

def initialize(id, debug=false)
  self.id = id
  regexps = {
    :title => '<div class="titlebar"><h1>(.*?)<\/h1>',
    :directors => 'Réalisé par <span .*?><a .*?>(.*?)<\/a><\/span>',
    :nat => 'Long-métrage(.*?)\.',
    :genres => 'Genre : (.*?)<br\/>',
    :out_date => 'Date de sortie cinéma : <span .*?><a .*?>(.*?)<\/a><\/span>',
    :duree => 'Durée :(.*?) min',
    :production_date => 'Année de production.*?<a .*?>(.*?)<\/a>.*?<br \/>',
    :original_title => 'Titre original : <span .*?><em>(.*?)<\/em><\/span>',
    :actors => 'Avec (.*?), <a class=',
    :synopsis => '<p><span class="bold">Synopsis : </span>(.*?)</p>',
    :image => '<div class="poster"><em class="imagecontainer"><a .*?><img src=\'(.*?)\'alt=".*?"title=".*?"\/><img .*?><\/a><\/em>',
    :press_rate => '<p class="withstars"><a href=\'/film/revuedepresse_gen_cfilm=[0-9]*?.html\'><img .*? /></a><span class="moreinfo">\((.*?)\)</span></p></div>',
    :trailer => "<li class=\"\"><a href=\"\/video\/player_gen_cmedia=(.*?)&cfilm=#{id}\.html\">Bandes-annonces<\/a><\/li>"
  }
  data = Allocine::Web.new(MOVIE_DETAIL_URL % id).data.gsub(/\r|\n|\t/,"")
  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!(/<.*?>/, '')
    r.strip!
    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

#idObject

Returns the value of attribute id.



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

def id
  @id
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

#press_rateObject

Returns the value of attribute press_rate.



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

def press_rate
  @press_rate
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

#trailerObject

Returns the value of attribute trailer.



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

def trailer
  @trailer
end

Class Method Details

.find(search) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/allocine/movie.rb', line 6

def self.find(search)
  search = ActiveSupport::Multibyte.proxy_class.new(search.to_s).mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n,'').downcase.to_s.
    downcase.
    gsub(/[\W]/u, ' ').
    gsub(/\s[a-z]{1}\s/, ' ').
    strip.
    gsub(/-\z/u, '').
    gsub(' ', '+').
    gsub('2nd', '2').
    to_s
  data = Allocine::Web.new(MOVIE_SEARCH_URL % search).data.gsub(/\r|\n|\t|\s{2,}/,"")
  movies = Array.new
  while data =~ /<a href='\/film\/fichefilm_gen_cfilm=(\d+).html'><imgsrc='.*?'alt='(.*?)' \/><\/a>/i
    id, name = $1, $2
    data.gsub!("<a href='/film/fichefilm_gen_cfilm=#{id}.html'>", "")
    name.gsub!(/<(.+?)>/,'')
    movies << [id, name]
  end
  movies = searchGoogle(search, movies.collect{|m| m[0]}) + movies
  
  return movies
end

.lucky_find(search) ⇒ Object



38
39
40
41
# File 'lib/allocine/movie.rb', line 38

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

.searchGoogle(search, movies) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/allocine/movie.rb', line 29

def self.searchGoogle(search, movies)
  data = Allocine::Web.new("http://www.google.fr/search?hl=fr&q=site:allocine.fr+#{search}").data #.gsub(/\r|\n|\t/,"")
  matches = Array.new
  data.scan(/fichefilm_gen_cfilm=([0-9]*).html/) do |m| 
    matches << [m.first, "#{search} - Found by Google"] unless movies.include? m.first
  end
  return matches.uniq
end

Instance Method Details

#press_reviewsObject



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/allocine/movie.rb', line 43

def press_reviews
  data = Allocine::Web.new(MOVIE_PRESS_URL % self.id).data.gsub(/\r|\n|\t/,"")
  reviews = Array.new
  
  while data =~ /<div class="rubric"><div class="titlebar vmargin20t novmarginb"><a id="pressreview(.*?)" class="anchor"><\/a>(.*?)<\/p><\/div><div class="rubric">/i
    pressreviewid, content = $1, $2
    data.gsub!(/<a id="pressreview#{pressreviewid}" class="anchor">/, "")
    content =~ /<h2><a href="\/critique\/fichepresse_gen_cpresse=(.*?).html">(.*?)<\/a><\/h2><span><img.*?stareval n([0-9]{1}?)0 on4.*?\/><\/span><\/div><p class="lighten">(.*?)<\/p><p>(.*?)<\/p>/
    # 1 : ID, 2 : Journal, 3 : Note, 4 : Auteur, 5 : Critique
    reviews << [$1, $2, $3, $4, $5]  unless $2.nil? || $3.nil?
  end
  
  reviews
end