ADD Pompage des images ! :)
[pompage.git] / src / modules / allocine.rb
index e4173bb..d5da08c 100644 (file)
@@ -1,4 +1,5 @@
 require 'net/http'\r
+require 'uri'\r
 require 'cgi'\r
 require 'thread'\r
 require 'iconv'\r
@@ -7,16 +8,15 @@ require "basemodule.rb"
 \r
 # un mixin pour allocine\r
 class Allocine  < Basemodule\r
-   \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
+      @connexion = Net::HTTP::new('www.allocine.fr') if @connexion.nil?\r
       \r
       donneesHtml = nil\r
       begin\r
          begin\r
-            reponse, donneesHtml = connexion.get("/recherche/?rub=1&motcle=#{CGI::escape(Iconv.iconv("ISO-8859-1", "UTF-8", titre)[0])}")\r
+            reponse, donneesHtml = @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
@@ -38,7 +38,6 @@ class Allocine  < Basemodule
       \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">&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
@@ -50,11 +49,27 @@ class Allocine  < Basemodule
    \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
-         \r
-      r, ficheHtml = connexion.get("/film/fichefilm_gen_cfilm=#{id}.html") \r
+      @connexion = Net::HTTP::new('www.allocine.fr') if @connexion.nil?\r
+\r
+      r, ficheHtml = @connexion.get("/film/fichefilm_gen_cfilm=#{id}.html") \r
       \r
+      /<img src="(.*?)" border="0" alt="" class="affichette" \/>/ =~ ficheHtml\r
+      unless $1.nil?\r
+         @threadImage = Thread::new($1){|imageUrl|\r
+            begin\r
+               imageUrl = URI.parse(imageUrl)               \r
+               connexionImage = Net::HTTP::new(imageUrl.host)\r
+               r, image = connexionImage.get(imageUrl.path)\r
+               f = File::new($repBase + "/" + $repAffichette + film.id.to_s + ".jpg", 'w').binmode\r
+               f.write(image)\r
+               f.close\r
+            rescue Exception => e\r
+               puts e.message\r
+               puts e.backtrace            \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
@@ -121,4 +136,8 @@ class Allocine  < Basemodule
       /Budget<\/b> : (.+?) millions d'euros<\/h4>/ =~ ficheHtml\r
       film.budget = $1 unless $1.nil?\r
    end   \r
+   \r
+   def finish\r
+      @threadImage.join if defined? @threadImage\r
+   end\r
 end
\ No newline at end of file