]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/imdb.rb
imdb plugin: prefix search string with 'name' or 'title' to only search for people...
[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 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2005 Arnaud Cornet
10 # Copyright:: (C) 2007 Giuseppe Bilotta
11 #
12 # License:: MIT license
13
14 require 'uri/common'
15
16 class Imdb
17   IMDB = "http://us.imdb.com"
18   TITLE_OR_NAME_MATCH = /<a href="(\/(?:title|name)\/(?:tt|nm)[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
19   TITLE_MATCH = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
20   NAME_MATCH = /<a href="(\/name\/nm[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
21   FINAL_ARTICLE_MATCH = /, ([A-Z]\S{0,2})$/
22
23   MATCHER = {
24     :title => TITLE_MATCH,
25     :name => NAME_MATCH,
26     :both => TITLE_OR_NAME_MATCH
27   }
28
29   def initialize(bot)
30     @bot = bot
31   end
32
33   def search(rawstr, rawopts={})
34     str = URI.escape(rawstr)
35     str << ";site=aka" if @bot.config['imdb.aka']
36     opts = rawopts.dup
37     opts[:type] = :both unless opts[:type]
38     return do_search(str, opts)
39   end
40
41   def do_search(str, opts={})
42     resp = nil
43     begin
44       resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
45                                         :max_redir => -1)
46     rescue Exception => e
47       error e.message
48       warning e.backtrace.join("\n")
49       return nil
50     end
51
52
53     matcher = MATCHER[opts[:type]]
54     debug matcher.inspect
55
56     if resp.code == "200"
57       m = []
58       m << matcher.match(resp.body) if @bot.config['imdb.popular']
59       if resp.body.match(/\(Exact Matches\)<\/b>/) and @bot.config['imdb.exact']
60         m << matcher.match($')
61       end
62       m.compact!
63       unless m.empty?
64         return m.map { |mm|
65           mm[1]
66         }.uniq
67       end
68     elsif resp.code == "302"
69       debug "automatic redirection"
70       new_loc = resp['location'].gsub(IMDB, "")
71       if new_loc.match(/\/find\?q=(.*)/)
72         return do_search($1, opts)
73       else
74         return [new_loc.gsub(/\?.*/, "")]
75       end
76     end
77     return nil
78   end
79
80   def info(rawstr, opts={})
81     debug opts.inspect
82     urls = search(rawstr, opts)
83     debug urls
84     if urls.nil_or_empty?
85       debug "IMDB: search returned NIL"
86       return nil
87     end
88     results = []
89     urls.each { |sr|
90       type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
91       case type
92       when :title
93         results << info_title(sr)
94       when :name
95         results << info_name(sr)
96       else
97         results << "#{sr}"
98       end
99     }
100     return results
101   end
102
103   def grab_info(info, body)
104     /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
105   end
106
107   def fix_article(org_tit)
108     title = org_tit.dup
109     if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
110       art = $1.dup
111       debug art.inspect
112       if art[-1,1].match(/[a-z]/)
113         art << " "
114       end
115       return art + title
116     end
117     return title
118   end
119
120   def info_title(sr)
121     resp = nil
122     begin
123       resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
124     rescue Exception => e
125       error e.message
126       warning e.backtrace.join("\n")
127       return nil
128     end
129
130     info = []
131
132     if resp.code == "200"
133       m = /<title>([^<]*)<\/title>/.match(resp.body)
134       return nil if !m
135       title_date = m[1]
136       pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:\/[IV]+)?)\)\s*(.+)?$/).first
137       pre_title.strip!
138       title = fix_article(pre_title)
139
140       dir = nil
141       data = grab_info(/Directors?/, resp.body)
142       if data
143         dir = data.scan(NAME_MATCH).map { |url, name|
144           name
145         }.join(', ')
146       end
147
148       country = nil
149       data = grab_info(/Country/, resp.body)
150       if data
151         country = data.ircify_html.gsub(' / ','/')
152       end
153
154       info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
155
156       ratings = "no votes"
157       m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
158       if m
159         ratings = "#{m[1]}/10 (#{m[2]} voters)"
160       end
161
162       genre = Array.new
163       resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
164         genre << gnr
165       end
166
167       plot = nil
168       data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
169       if data
170         plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
171       end
172
173       info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
174
175       return info
176     end
177     return nil
178   end
179
180   def info_name(sr)
181     resp = nil
182     begin
183       resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
184     rescue Exception => e
185       error e.message
186       warning e.backtrace.join("\n")
187       return nil
188     end
189
190     info = []
191
192     if resp.code == "200"
193       m = /<title>([^<]*)<\/title>/.match(resp.body)
194       return nil if !m
195       name = m[1]
196
197       info << "#{name} : http://us.imdb.com#{sr}"
198
199       birth = nil
200       data = grab_info("Date of Birth", resp.body)
201       if data
202         birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
203       end
204
205       death = nil
206       data = grab_info("Date of Death", resp.body)
207       if data
208         death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
209       end
210
211       info << [birth, death].compact.join('. ') if birth or death
212
213       movies = {}
214
215       filmorate = nil
216       begin
217         filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
218       rescue Exception
219       end
220
221       if filmorate
222         filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
223           what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
224           next unless what
225           movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
226             fix_article(tit)
227           }
228         }
229       end
230
231       preferred = ['Actor', 'Director']
232       if resp.body.match(/Jump to filmography as:&nbsp;(.*?)<\/div>/)
233         txt = $1
234         preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
235           pref.first
236         }
237       end
238
239       unless movies.empty?
240         all_keys = movies.keys.sort
241         debug all_keys.inspect
242         keys = []
243         preferred.each { |key|
244           keys << key if all_keys.include? key
245         }
246         keys = all_keys if keys.empty?
247         ar = []
248         keys.each { |key|
249           ar << key.dup
250           ar.last << ": " + movies[key].join('; ')
251         }
252         info << ar.join('. ')
253       end
254       return info
255
256     end
257     return nil
258   end
259 end
260
261 class ImdbPlugin < Plugin
262   BotConfig.register BotConfigBooleanValue.new('imdb.aka',
263     :default => true,
264     :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
265   BotConfig.register BotConfigBooleanValue.new('imdb.popular',
266     :default => true,
267     :desc => "Display info on popular IMDB entries matching the request closely")
268   BotConfig.register BotConfigBooleanValue.new('imdb.exact',
269     :default => true,
270     :desc => "Display info on IMDB entries matching the request exactly")
271   BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
272     :default => false,
273     :desc => "Try to detect an article placed at the end and move it in front of the title")
274
275   def help(plugin, topic="")
276     "imdb <string> => search http://www.imdb.org for <string>: prefix <string> with 'name' or 'title' if you only want to search for people or films respectively, e.g.: imdb name ed wood"
277   end
278
279   def imdb(m, params)
280     what = params[:what].to_s
281     type = params[:type].intern
282     i = Imdb.new(@bot)
283     info = i.info(what, :type => type)
284     if !info
285       m.reply "Nothing found for #{what}"
286       return nil
287     end
288     if info.length == 1
289       m.reply Utils.decode_html_entities info.first.join("\n")
290     else
291       m.reply info.map { |i|
292         Utils.decode_html_entities i.join(" | ")
293       }.join("\n")
294     end
295   end
296 end
297
298 plugin = ImdbPlugin.new
299 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }
300