9 require "basemodule.rb"
11 # doc : http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html
14 # hack : http://arosien.blogspot.com/2007/06/increasing-rubys-netbufferedio-buffer.html
18 timeout(@read_timeout) { @rbuf << @io.sysread(32768) }
23 # un module d'importation pour le site "www.allocine.fr"
24 class Allocine
< Basemodule
28 @connexion = Net
::HTTP::new('www.allocine.fr').start
if @connexion == nil || !
@connexion.started
?
30 Puts
"[!] Impossible de se connecter à Allocine
. Retry
.."
36 @connexion.finish if defined? @connexion && @connexion.started? # on admet qu'après un load on a plus besoin de la connexion
41 def rechercherFilm(titre)
47 reponse = @connexion.get("/recherche/?rub
=1&motcle
=#{CGI::escape(Iconv.iconv("ISO-8859-1", "UTF-8", titre)[0])}")
50 puts "[!
] Erreur lors de la recherche du titre
#{titre}, retry.."
56 # convertit le code latin-1 en UTF8
57 html = Iconv.iconv("UTF-8
", "ISO-8859-1
", reponse.body)[0]
59 # si pas trouvé alors on enlève un mot à la fin
60 if /.*?Pas de résultats.*?/ =~ html || ! html
.include?("<h3><b>Films <h4>")
61 /(.*?)[^ ]+?$/ =~ titre
.strip
67 end while not titre
.nil? and not titre
.empty
?
70 unless titre
.nil? or titre
.empty
?
71 r
= html
.scan(/<a href="\/film\
/fichefilm_gen_cfilm=(\d+)\.html" class="link1">(.*?)<\/a
>(?:<\
/h4><h5 style="color: #666666"> (.*?)<\/h5
>){0,1}(?:<h4
><br \
/><\/h4
>){0,1}(?:<h4 style
="color: #666666"> de (.*?)<\
/h4>){0,1}(?:<h4 style="color: #666666"> avec (.*?)<\/h4
>){0,1}(?:<h4 style
="color: #666666"> 
;\
((.*?)\
)<\
/h4>){0,1}/)
74 reponses
[f
[1].virerBalisesHTML +
(f
[2] !
= nil ? " " + f
[2].virerBalisesHTML
: "") +
(f
[3] !
= nil ? " de " + f
[3].virerBalisesHTML
: "") +
(f
[4] !
= nil ? " avec " + f
[4].virerBalisesHTML
: "") +
(f
[5] !
= nil ? " (" + f
[5].virerBalisesHTML +
")" : "")] = f
[0]
82 loadPath("/film/fichefilm_gen_cfilm=#{id}.html")
85 def loadURL(url
, film
)
86 loadPath(URI
.parse(url
).path
, film
)
90 def loadPath(path
, film
)
95 html
= @connexion.get(path
).body
98 puts
"[!] Erreur lors du chargement de
#{film.fichiers[0]}, retry.."
104 puts "Pompage de
#{film.titre} (#{film.fichiers[0]}) ..."
106 #convertit le code latin-1 en UTF8
107 html = Iconv.iconv("UTF-8
", "ISO-8859-1
", html)[0]
110 /<img src="(.*?)" border="0" alt="" class="affichette
" \/>/ =~ html
112 @threadImage = Thread::start($1){|imageUrl|
115 imageUrlParsed = URI.parse(imageUrl)
116 connexionImage = Net::HTTP::new(imageUrlParsed.host).start
117 image = connexionImage.get(imageUrlParsed.path).body
118 f = File::new($repBase + "/" +
$repAffichette + film
.id
.to_s +
".jpg", 'w').binmode
121 connexionImage
.finish
122 rescue Exception
=> e
125 puts
"[!] Erreur lors du chargement de l
'image '#{imageUrl}', retry.."
126 retry if (nbRetry += 1) < 5
134 film
.url
= "http://www.allocine.fr" + path
139 /<title>(.*?)<\/title
>/ =~ html
140 film
.titre
= $1 unless $1.nil?
145 /<h4>Année de production : (\d+)<\/h4
>/ =~ html
146 film
.annee
= $1 unless $1.nil?
149 if take
?("realisateurs")
151 film
.realisateurs
.clear
152 /<h4>Réalisé par(.*?)<\/h4
>/ =~ html
153 $1.scan(/\s*<a class="link1" href=".*?">(.*?)<\/a
>\s
*/m
){|a
|
154 film
.realisateurs
<< Personne
::ajouter(a
[0]) unless a
[0].nil?
161 /<h4>Avec(.*?)<\/h4
>/ =~ html
162 $1.scan(/\s*<a class="link1" href="\/personne\
/fichepersonne_gen_cpersonne=\d+\.html">(.+?)<\/a
>\s
*/m
){|a
|
163 film
.acteurs
<< Personne
::ajouter(a
[0]) unless a
[0].nil?
170 /<h4>Film (.*?)\. <\/h4
>/ =~ html
171 $1.split(',').each
{|pays
|
172 film
.pays
<< Pays
::ajouter(pays
) unless pays
.nil?
177 # Duree (capture des heures et des minutes séparement vue que c'est le bordel sur allocine
178 /<h4>Durée :(?:.*?)(\d+)h/ =~ html
179 heure
= $1.nil? ? 0 : $1.to_i
180 /<h4>Durée :(?:.*?)(\d+)min/ =~ html
181 min
= $1.nil? ? 0 : $1.to_i
182 film
.duree
= heure
* 60 + min
185 if take
?("critiquenote")
186 # Critiques presse et spectateur
187 /Presse.*etoile_([012345]).*Spectateurs.*etoile_([012345])"/m
=~ html
188 film
.critiquePresse
= $1 unless $1.nil?
189 film
.critiqueSpectateur
= $2 unless $2.nil?
195 /<h4>Genre : (.*?)<\/h4
>/ =~ html
196 $1.scan(/<a href="\/film\
/alaffiche_genre_gen_genre=.*?" class="link1">(.+?)<\/a
>/m
){|g
|
197 film
.genres
<< Genre
::ajouter(g
[0]) unless g
[0].nil?
203 /Synopsis.*?<h4>(.*?)<\/h4
>/m
=~ html
206 film
.synopsis
.gsub!
(/<br\s*\/>|<br\s
*>/, "\n")
207 film
.synopsis
.virerBalisesHTML!
213 /Budget<\/b
> : (.+
?) millions d
'euros<\/h4>/ =~ html
214 film.budget = $1 unless $1.nil?
221 @threadImage.join if defined? @threadImage