+# coding: utf-8\r
+\r
require 'net/http'\r
+require 'uri'\r
require 'cgi'\r
require 'thread'\r
require 'iconv'\r
\r
require "basemodule.rb"\r
\r
-# un mixin pour allocine\r
+# doc : http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html\r
+Net::HTTP.version_1_2\r
+\r
+# hack : http://arosien.blogspot.com/2007/06/increasing-rubys-netbufferedio-buffer.html\r
+module Net\r
+ class BufferedIO\r
+ def rbuf_fill\r
+ timeout(@read_timeout) { @rbuf << @io.sysread(32768) }\r
+ end\r
+ end\r
+end\r
+\r
+\r
+# un module d'importation pour le site "www.allocine.fr"\r
class Allocine < Basemodule\r
\r
- # Voir le parent.\r
+ def createConnexion\r
+ begin\r
+ @connexion = Net::HTTP::new('www.allocine.fr').start if @connexion == nil || !@connexion.started?\r
+ rescue \r
+ Puts "Impossible de se connecter à Allocine. Retry.."\r
+ retry\r
+ end\r
+ end\r
+ \r
+ def fermerConnexion \r
+ @connexion.finish if defined? @connexion && @connexion.started? # on admet qu'après un load on a plus besoin de la connexion\r
+ @connexion = nil\r
+ end\r
+ \r
+ # voir le parent.\r
def rechercherFilm(titre)\r
- Thread.current["connexion"] = Net::HTTP::new('www.allocine.fr') if Thread.current["connexion"].nil?\r
- connexion = Thread.current["connexion"]\r
+ createConnexion\r
\r
- donneesHtml = nil\r
begin\r
+ reponse = nil\r
begin\r
- reponse, donneesHtml = connexion.get("/recherche/?motcle=#{CGI::escape(Iconv.iconv("ISO-8859-1", "UTF-8", titre)[0])}")\r
+ reponse = @connexion.get("/recherche/?rub=1&motcle=#{CGI::escape(Iconv.iconv("ISO-8859-1", "UTF-8", titre)[0])}")\r
rescue Exception => e\r
p e\r
- puts "[!] Connexion lost, retry.."\r
+ puts "[!] Erreur lors de la recherche du titre #{titre}, retry.."\r
+ fermerConnexion\r
+ createConnexion\r
retry\r
end\r
+ \r
+ # convertit le code latin-1 en UTF8\r
+ html = Iconv.iconv("UTF-8", "ISO-8859-1", reponse.body)[0]\r
\r
- #convertit le code latin-1 en UTF8\r
- donneesHtml = Iconv.iconv("UTF-8", "ISO-8859-1", donneesHtml)[0]\r
- \r
- #si pas trouvé alors on enlève un mot à la fin\r
- if /.*?Pas de résultats.*?/ =~ donneesHtml || ! donneesHtml.include?("<h3><b>Films <h4>")\r
+ # si pas trouvé alors on enlève un mot à la fin\r
+ if /.*?Pas de résultats.*?/ =~ html || ! html.include?("<h3><b>Films <h4>")\r
/(.*?)[^ ]+?$/ =~ titre.strip\r
titre = $1\r
titre.strip!\r
\r
reponses = {}\r
unless titre.nil? or titre.empty?\r
- \r
- r = donneesHtml.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}/)\r
- \r
+ 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}/)\r
+\r
r.each{|f|\r
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]\r
}\r
end\r
\r
# Voir le parent.\r
- def load(id, film) \r
- Thread.current["connexion"] = Net::HTTP::new('www.allocine.fr') if Thread.current["connexion"].nil?\r
- connexion = Thread.current["connexion"]\r
+ def load(id, film) \r
+ createConnexion\r
+\r
+ html = nil\r
+ begin\r
+ html = @connexion.get("/film/fichefilm_gen_cfilm=#{id}.html").body \r
+ rescue Exception => e\r
+ p e\r
+ puts "[!] Erreur lors du chargement de #{film.fichiers[0]}, retry.."\r
+ fermerConnexion\r
+ createConnexion\r
+ retry\r
+ end\r
\r
- r, ficheHtml = connexion.get("/film/fichefilm_gen_cfilm=#{id}.html") \r
- \r
- #convertit le code latin-1 en UTF8\r
- ficheHtml = Iconv.iconv("UTF-8", "ISO-8859-1", ficheHtml)[0]\r
+ /<img src="(.*?)" border="0" alt="" class="affichette" \/>/ =~ html\r
+ unless $1.nil?\r
+ @threadImage = Thread::start($1){|imageUrl|\r
+ nbRetry = 0\r
+ begin\r
+ imageUrlParsed = URI.parse(imageUrl) \r
+ connexionImage = Net::HTTP::new(imageUrlParsed.host).start\r
+ image = connexionImage.get(imageUrlParsed.path).body\r
+ f = File::new($repBase + "/" + $repAffichette + film.id.to_s + ".jpg", 'w').binmode\r
+ f.write(image)\r
+ f.close\r
+ connexionImage.finish\r
+ rescue Exception => e\r
+ puts e.message\r
+ puts e.backtrace \r
+ puts "[!] Erreur lors du chargement de l'image '#{imageUrl}', retry.." \r
+ retry if (nbRetry += 1) < 5 \r
+ end\r
+ }\r
+ end\r
\r
+ #convertit le code latin-1 en UTF8 \r
+ html = Iconv.iconv("UTF-8", "ISO-8859-1", html)[0]\r
#url\r
film.url = "http://www.allocine.fr/film/fichefilm_gen_cfilm=#{id}.html"\r
\r
# Titre\r
- /<title>(.*?)<\/title>/ =~ ficheHtml\r
+ /<title>(.*?)<\/title>/ =~ html\r
film.titre = $1 unless $1.nil?\r
\r
puts "Movie found : #{film.titre} (#{film.fichiers[0]})"\r
\r
# Année\r
- /<h4>Année de production : (\d+)<\/h4>/ =~ ficheHtml\r
+ /<h4>Année de production : (\d+)<\/h4>/ =~ html\r
film.annee = $1 unless $1.nil?\r
\r
# Réalisateurs\r
- /<h4>Réalisé par(.*?)<\/h4>/ =~ ficheHtml\r
+ /<h4>Réalisé par(.*?)<\/h4>/ =~ html\r
$1.scan(/\s*<a class="link1" href=".*?">(.*?)<\/a>\s*/m){|a|\r
film.realisateurs << Personne::ajouter(a[0]) unless a[0].nil?\r
} unless $1.nil?\r
\r
# Acteurs\r
- /<h4>Avec(.*?)<\/h4>/ =~ ficheHtml\r
+ /<h4>Avec(.*?)<\/h4>/ =~ html\r
$1.scan(/\s*<a class="link1" href="\/personne\/fichepersonne_gen_cpersonne=\d+\.html">(.+?)<\/a>\s*/m){|a|\r
film.acteurs << Personne::ajouter(a[0]) unless a[0].nil? \r
} unless $1.nil? \r
\r
# Pays\r
- /<h4>Film (.*?)\. <\/h4>/ =~ ficheHtml\r
+ /<h4>Film (.*?)\. <\/h4>/ =~ html\r
$1.split(',').each{|pays|\r
film.pays << Pays::ajouter(pays) unless pays.nil?\r
} unless $1.nil? \r
\r
# Duree (capture des heures et des minutes séparement vue que c'est le bordel sur allocine\r
- /<h4>Durée :(?:.*?)(\d+)h/ =~ ficheHtml\r
+ /<h4>Durée :(?:.*?)(\d+)h/ =~ html\r
heure = $1.nil? ? 0 : $1.to_i\r
\r
- /<h4>Durée :(?:.*?)(\d+)min/ =~ ficheHtml\r
+ /<h4>Durée :(?:.*?)(\d+)min/ =~ html\r
min = $1.nil? ? 0 : $1.to_i\r
\r
film.duree = heure * 60 + min\r
\r
# Critiques presse et spectateur\r
- /Presse.*etoile_([012345]).*Spectateurs.*etoile_([012345])"/m =~ ficheHtml\r
+ /Presse.*etoile_([012345]).*Spectateurs.*etoile_([012345])"/m =~ html\r
film.critiquePresse = $1 unless $1.nil?\r
film.critiqueSpectateur = $2 unless $2.nil?\r
\r
# Genre\r
- /<h4>Genre : (.*?)<\/h4>/ =~ ficheHtml\r
+ /<h4>Genre : (.*?)<\/h4>/ =~ html\r
$1.scan(/<a href="\/film\/alaffiche_genre_gen_genre=.*?" class="link1">(.+?)<\/a>/m){|g|\r
film.genres << Genre::ajouter(g[0]) unless g[0].nil?\r
} unless $1.nil? \r
\r
# Synopsis \r
- /Synopsis.*?<h4>(.*?)<\/h4>/m =~ ficheHtml\r
+ /Synopsis.*?<h4>(.*?)<\/h4>/m =~ html\r
unless $1.nil?\r
film.synopsis = $1\r
film.synopsis.gsub!(/<br\s*\/>|<br\s*>/, "\n")\r
end\r
\r
# Budget \r
- /Budget<\/b> : (.+?) millions d'euros<\/h4>/ =~ ficheHtml\r
+ /Budget<\/b> : (.+?) millions d'euros<\/h4>/ =~ html\r
film.budget = $1 unless $1.nil?\r
+ \r
+ fermerConnexion\r
end \r
+ \r
+ def finish\r
+ @threadImage.join if defined? @threadImage\r
+ end\r
end
\ No newline at end of file