]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/imdb.rb
* (rss) bypass http cache when handling 'show' command
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / imdb.rb
index 759e380c091d43a2d8496af53c420046f4ab97ae..cfadd3c8568ad4bad79feeed639e39b3606825fd 100644 (file)
@@ -1,8 +1,16 @@
-# IMDB plugin for RubyBot
-# (c) 2005 Arnaud Cornet <arnaud.cornet@gmail.com>
-# Licensed under MIT License.
+#-- vim:sw=2:et
+#++
+#
+# :title: IMDB plugin for rbot
+#
+# Author:: Arnaud Cornet <arnaud.cornet@gmail.com>
+# Copyright:: (C) 2005 Arnaud Cornet
+# License:: MIT license
+#
+# Notes by Giuseppe Bilotta:
+# TODO return more than one match (configurable)
+# TODO why do we use CGI.unescapeHTML? shall we rely on the rbot methods?
 
-require 'net/http'
 require 'cgi'
 require 'uri/common'
 
@@ -13,18 +21,18 @@ class Imdb
 
   def search(rawstr)
     str = URI.escape(rawstr)
-    @http = @bot.httputil.get_proxy(URI.parse("http://us.imdb.com/find?q=#{str}"))
-    @http.start
+    resp = nil
     begin
-    resp, data = @http.get("/find?q=#{str}", "User-Agent" => "Mozilla/5.0")
-    rescue Net::ProtoRetriableError => detail
-      head = detail.data
-      if head.code == "301" or head.code == "302"
-            return head['location'].gsub(/http:\/\/us.imdb.com/, "").gsub(/\?.*/, "")
-        end
+      resp = @bot.httputil.get_response("http://us.imdb.com/find?q=#{str}",
+                                        :max_redir => -1)
+    rescue Exception => e
+      error e.message
+      warning e.backtrace.join("\n")
+      return nil
     end
+
     if resp.code == "200"
-      m = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(:?[^>]*)>([^<]*)<\/a>/.match(resp.body)
+      m = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/.match(resp.body)
       if m
         url = m[1]
         title = m[2]
@@ -39,17 +47,25 @@ class Imdb
   def info(rawstr)
     sr = search(rawstr)
     if !sr
-      puts "IMDB : search returned NIL"
+      debug "IMDB: search returned NIL"
+      return nil
+    end
+    resp = nil
+    begin
+      resp = @bot.httputil.get_response('http://us.imdb.com' + sr,
+                                        :max_redir => -1)
+    rescue Exception => e
+      error e.message
+      warning e.backtrace.join("\n")
       return nil
     end
-    resp, data = @http.get(sr, "User-Agent" =>
-      "Mozilla/5.0 (compatible; Konqueror/3.1; Linux)")
+
     if resp.code == "200"
       m = /<title>([^<]*)<\/title>/.match(resp.body)
       return nil if !m
       title = CGI.unescapeHTML(m[1])
 
-      m = /<b>([0-9.]+)\/10<\/b> \(([0-9,]+) votes?\)/.match(resp.body)
+      m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
       return nil if !m
       score = m[1]
       votes = m[2]
@@ -70,16 +86,12 @@ class ImdbPlugin < Plugin
     "imdb <string> => search http://www.imdb.org for <string>"
   end
 
-  def privmsg(m)
-    unless(m.params && m.params.length > 0)
-      m.reply "incorrect usage: " + help(m.plugin)
-      return
-    end
-
+  def imdb(m, params)
+    what = params[:what].to_s
     i = Imdb.new(@bot)
-    info = i.info(m.params)
+    info = i.info(what)
     if !info
-      m.reply "Nothing found for #{m.params}"
+      m.reply "Nothing found for #{what}"
       return nil
     end
     m.reply "#{info[1]} : #{info[0]}"
@@ -88,5 +100,5 @@ class ImdbPlugin < Plugin
 end
 
 plugin = ImdbPlugin.new
-plugin.register("imdb")
+plugin.map "imdb *what"