git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / src / modules / allocine.rb
index 893f7ed..01764f8 100644 (file)
@@ -1,3 +1,5 @@
+# coding: utf-8\r
+\r
 require 'net/http'\r
 require 'uri'\r
 require 'cgi'\r
@@ -6,32 +8,52 @@ require 'iconv'
 \r
 require "basemodule.rb"\r
 \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
    def createConnexion\r
-      @connexion = Net::HTTP::new('www.allocine.fr') if @connexion.nil?      \r
+      @connexion = Net::HTTP::new('www.allocine.fr').start if @connexion == nil || !@connexion.started?\r
    end\r
    \r
-   # Voir le parent.\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
       createConnexion\r
 \r
-      donneesHtml = nil\r
       begin\r
+         reponse = nil\r
          begin\r
-            reponse, donneesHtml = @connexion.get("/recherche/?rub=1&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
@@ -42,8 +64,8 @@ class Allocine  < Basemodule
       \r
       reponses = {}\r
       unless titre.nil? or titre.empty?\r
-         r = donneesHtml.scan(/<a href="\/film\/fichefilm_gen_cfilm=(\d+)\.html" class="link1">(.*?)<\/a>(?:<\/h4><h5 style="color: #666666">&nbsp;(.*?)<\/h5>){0,1}(?:<h4><br \/><\/h4>){0,1}(?:<h4 style="color: #666666"> de (.*?)<\/h4>){0,1}(?:<h4 style="color: #666666">&nbsp;avec (.*?)<\/h4>){0,1}(?:<h4 style="color: #666666">&nbsp;\((.*?)\)<\/h4>){0,1}/)\r
-         \r
+         r = html.scan(/<a href="\/film\/fichefilm_gen_cfilm=(\d+)\.html" class="link1">(.*?)<\/a>(?:<\/h4><h5 style="color: #666666">&nbsp;(.*?)<\/h5>){0,1}(?:<h4><br \/><\/h4>){0,1}(?:<h4 style="color: #666666"> de (.*?)<\/h4>){0,1}(?:<h4 style="color: #666666">&nbsp;avec (.*?)<\/h4>){0,1}(?:<h4 style="color: #666666">&nbsp;\((.*?)\)<\/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
@@ -52,84 +74,95 @@ class Allocine  < Basemodule
    end\r
    \r
    # Voir le parent.\r
-   def load(id, film)      \r
+   def load(id, film)   \r
       createConnexion\r
 \r
-      r, ficheHtml = @connexion.get("/film/fichefilm_gen_cfilm=#{id}.html") \r
-      \r
-      /<img src="(.*?)" border="0" alt="" class="affichette" \/>/ =~ ficheHtml\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
+      /<img src="(.*?)" border="0" alt="" class="affichette" \/>/ =~ html\r
       unless $1.nil?\r
-         @threadImage = Thread::new($1){|imageUrl|\r
+         @threadImage = Thread::start($1){|imageUrl|\r
             begin\r
                imageUrl = URI.parse(imageUrl)               \r
-               connexionImage = Net::HTTP::new(imageUrl.host)\r
-               r, image = connexionImage.get(imageUrl.path)\r
+               connexionImage = Net::HTTP::new(imageUrl.host).start\r
+               image = connexionImage.get(imageUrl.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 e.backtrace    \r
+               puts "[!] Erreur lors du chargement de l'image #{imageUrl}, retry.."        \r
+               retry\r
             end\r
          }\r
       end\r
 \r
-      #convertit le code latin-1 en UTF8\r
-      ficheHtml = Iconv.iconv("UTF-8", "ISO-8859-1", ficheHtml)[0]\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 (.*?)\.&nbsp;<\/h4>/ =~ ficheHtml\r
+      /<h4>Film (.*?)\.&nbsp;<\/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
@@ -137,8 +170,10 @@ class Allocine  < Basemodule
       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