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) }
24 # un module d'importation pour le site "www.allocine.fr"
25 class Allocine
< Basemodule
29 @connexion = Net
::HTTP::new('www.allocine.fr').start
if @connexion == nil || !
@connexion.started
?
31 Puts
"Impossible de se connecter à Allocine. Retry.."
37 @connexion.finish
if defined? @connexion && @connexion.started
? # on admet qu'après un load on a plus besoin de la connexion
42 def rechercherFilm(titre
)
48 reponse
= @connexion.get("/recherche/?rub=1&motcle=#{CGI::escape(Iconv.iconv("ISO-8859-1", "UTF-8", titre)[0])}")
51 puts
"[!] Erreur lors de la recherche du titre
#{titre}, retry.."
57 # convertit le code latin-1 en UTF8
58 html = Iconv.iconv("UTF-8
", "ISO-8859-1
", reponse.body)[0]
60 # si pas trouvé alors on enlève un mot à la fin
61 if /.*?Pas de résultats.*?/ =~ html || ! html
.include?("<h3><b>Films <h4>")
62 /(.*?)[^ ]+?$/ =~ titre
.strip
68 end while not titre
.nil? and not titre
.empty
?
71 unless titre
.nil? or titre
.empty
?
72 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}/)
75 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]
87 html
= @connexion.get("/film/fichefilm_gen_cfilm=#{id}.html").body
90 puts
"[!] Erreur lors du chargement de
#{film.fichiers[0]}, retry.."
96 /<img src="(.*?)" border="0" alt="" class="affichette
" \/>/ =~ html
98 @threadImage = Thread::start($1){|imageUrl|
101 imageUrlParsed = URI.parse(imageUrl)
102 connexionImage = Net::HTTP::new(imageUrlParsed.host).start
103 image = connexionImage.get(imageUrlParsed.path).body
104 f = File::new($repBase + "/" +
$repAffichette + film
.id
.to_s +
".jpg", 'w').binmode
107 connexionImage
.finish
108 rescue Exception
=> e
111 puts
"[!] Erreur lors du chargement de l
'image '#{imageUrl}', retry.."
112 retry if (nbRetry += 1) < 5
117 #convertit le code latin-1 en UTF8
118 html
= Iconv
.iconv("UTF-8", "ISO-8859-1", html
)[0]
120 film
.url
= "http://www.allocine.fr/film/fichefilm_gen_cfilm=#{id}.html"
123 /<title>(.*?)<\/title
>/ =~ html
124 film
.titre
= $1 unless $1.nil?
126 puts
"Movie found : #{film.titre} (#{film.fichiers[0]})"
129 /<h4>Année de production : (\d+)<\/h4
>/ =~ html
130 film
.annee
= $1 unless $1.nil?
133 /<h4>Réalisé par(.*?)<\/h4
>/ =~ html
134 $1.scan(/\s*<a class="link1" href=".*?">(.*?)<\/a
>\s
*/m
){|a
|
135 film
.realisateurs
<< Personne
::ajouter(a
[0]) unless a
[0].nil?
139 /<h4>Avec(.*?)<\/h4
>/ =~ html
140 $1.scan(/\s*<a class="link1" href="\/personne\
/fichepersonne_gen_cpersonne=\d+\.html">(.+?)<\/a
>\s
*/m
){|a
|
141 film
.acteurs
<< Personne
::ajouter(a
[0]) unless a
[0].nil?
145 /<h4>Film (.*?)\. <\/h4
>/ =~ html
146 $1.split(',').each
{|pays
|
147 film
.pays
<< Pays
::ajouter(pays
) unless pays
.nil?
150 # Duree (capture des heures et des minutes séparement vue que c'est le bordel sur allocine
151 /<h4>Durée :(?:.*?)(\d+)h/ =~ html
152 heure
= $1.nil? ? 0 : $1.to_i
154 /<h4>Durée :(?:.*?)(\d+)min/ =~ html
155 min
= $1.nil? ? 0 : $1.to_i
157 film
.duree
= heure
* 60 + min
159 # Critiques presse et spectateur
160 /Presse.*etoile_([012345]).*Spectateurs.*etoile_([012345])"/m
=~ html
161 film
.critiquePresse
= $1 unless $1.nil?
162 film
.critiqueSpectateur
= $2 unless $2.nil?
165 /<h4>Genre : (.*?)<\/h4
>/ =~ html
166 $1.scan(/<a href="\/film\
/alaffiche_genre_gen_genre=.*?" class="link1">(.+?)<\/a
>/m
){|g
|
167 film
.genres
<< Genre
::ajouter(g
[0]) unless g
[0].nil?
171 /Synopsis.*?<h4>(.*?)<\/h4
>/m
=~ html
174 film
.synopsis
.gsub!
(/<br\s*\/>|<br\s
*>/, "\n")
175 film
.synopsis
.virerBalisesHTML!
179 /Budget<\/b
> : (.+
?) millions d
'euros<\/h4>/ =~ html
180 film.budget = $1 unless $1.nil?
186 @threadImage.join if defined? @threadImage