]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/imdb.rb
*** (httputil) major rework, new caching implementation, unified request
[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     resp = nil
25     begin
26       resp = @bot.httputil.get_response("http://us.imdb.com/find?q=#{str}",
27                                         :max_redir => -1)
28     rescue Exception => e
29       error e.message
30       warning e.backtrace.join("\n")
31       return nil
32     end
33
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 = nil
54     begin
55       resp = @bot.httputil.get_response('http://us.imdb.com' + sr,
56                                         :max_redir => -1)
57     rescue Exception => e
58       error e.message
59       warning e.backtrace.join("\n")
60       return nil
61     end
62
63     if resp.code == "200"
64       m = /<title>([^<]*)<\/title>/.match(resp.body)
65       return nil if !m
66       title = CGI.unescapeHTML(m[1])
67
68       m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
69       return nil if !m
70       score = m[1]
71       votes = m[2]
72
73       genre = Array.new
74       resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
75         genre << gnr
76       end
77       return ["http://us.imdb.com" + sr, title, score, votes,
78         genre]
79     end
80     return nil
81   end
82 end
83
84 class ImdbPlugin < Plugin
85   def help(plugin, topic="")
86     "imdb <string> => search http://www.imdb.org for <string>"
87   end
88
89   def imdb(m, params)
90     what = params[:what].to_s
91     i = Imdb.new(@bot)
92     info = i.info(what)
93     if !info
94       m.reply "Nothing found for #{what}"
95       return nil
96     end
97     m.reply "#{info[1]} : #{info[0]}"
98     m.reply "Ratings: #{info[2]}/10 (#{info[3]} voters). Genre: #{info[4].join('/')}"
99   end
100 end
101
102 plugin = ImdbPlugin.new
103 plugin.map "imdb *what"
104