]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/imdb.rb
imdb plugin: update to the new site layout, use rbot headers
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / imdb.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: IMDB plugin for rbot
5 #
6 # Author:: Arnaud Cornet <arnaud.cornet@gmail.com>
7 # Copyright:: (C) 2005 Arnaud Cornet
8 # License:: MIT license
9 #
10 # Notes by Giuseppe Bilotta:
11 # TODO return more than one match (configurable)
12 # TODO why do we use CGI.unescapeHTML? shall we rely on the rbot methods?
13
14 require 'cgi'
15 require 'uri/common'
16
17 class Imdb
18   def initialize(bot)
19     @bot = bot
20   end
21
22   def search(rawstr)
23     str = URI.escape(rawstr)
24     @http = @bot.httputil.get_proxy(URI.parse("http://us.imdb.com/find?q=#{str}"))
25     @http.start
26     begin
27     resp, data = @http.get("/find?q=#{str}", @bot.httputil.headers)
28     rescue Net::ProtoRetriableError => detail
29       head = detail.data
30       if head.code == "301" or head.code == "302"
31             return head['location'].gsub(/http:\/\/us.imdb.com/, "").gsub(/\?.*/, "")
32         end
33     end
34     if resp.code == "200"
35       m = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/.match(resp.body)
36       if m
37         url = m[1]
38         title = m[2]
39         return url
40       end
41     elsif resp.code == "302"
42       return resp['location'].gsub(/http:\/\/us.imdb.com/, "").gsub(/\?.*/, "")
43     end
44     return nil
45   end
46
47   def info(rawstr)
48     sr = search(rawstr)
49     if !sr
50       debug "IMDB: search returned NIL"
51       return nil
52     end
53     resp, data = @http.get(sr, @bot.httputil.headers)
54     if resp.code == "200"
55       m = /<title>([^<]*)<\/title>/.match(resp.body)
56       return nil if !m
57       title = CGI.unescapeHTML(m[1])
58
59       m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
60       return nil if !m
61       score = m[1]
62       votes = m[2]
63
64       genre = Array.new
65       resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
66         genre << gnr
67       end
68       return ["http://us.imdb.com" + sr, title, score, votes,
69         genre]
70     end
71     return nil
72   end
73 end
74
75 class ImdbPlugin < Plugin
76   def help(plugin, topic="")
77     "imdb <string> => search http://www.imdb.org for <string>"
78   end
79
80   def imdb(m, params)
81     what = params[:what].to_s
82     i = Imdb.new(@bot)
83     info = i.info(what)
84     if !info
85       m.reply "Nothing found for #{what}"
86       return nil
87     end
88     m.reply "#{info[1]} : #{info[0]}"
89     m.reply "Ratings: #{info[2]}/10 (#{info[3]} voters). Genre: #{info[4].join('/')}"
90   end
91 end
92
93 plugin = ImdbPlugin.new
94 plugin.map "imdb *what"
95