4 # :title: IMDB plugin for rbot
6 # Author:: Arnaud Cornet <arnaud.cornet@gmail.com>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2005 Arnaud Cornet
10 # Copyright:: (C) 2007 Giuseppe Bilotta
12 # License:: MIT license
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})$/
24 :title => TITLE_MATCH,
26 :both => TITLE_OR_NAME_MATCH
33 def search(rawstr, rawopts={})
34 str = URI.escape(rawstr)
35 str << ";site=aka" if @bot.config['imdb.aka']
37 opts[:type] = :both unless opts[:type]
38 return do_search(str, opts)
41 def do_search(str, opts={})
44 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
48 warning e.backtrace.join("\n")
53 matcher = MATCHER[opts[:type]]
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($')
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)
74 return [new_loc.gsub(/\?.*/, "")]
80 def info(rawstr, opts={})
82 urls = search(rawstr, opts)
85 debug "IMDB: search returned NIL"
90 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
93 results << info_title(sr)
95 results << info_name(sr)
103 def grab_info(info, body)
104 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
107 def fix_article(org_tit)
109 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
112 if art[-1,1].match(/[a-z]/)
123 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
124 rescue Exception => e
126 warning e.backtrace.join("\n")
132 if resp.code == "200"
133 m = /<title>([^<]*)<\/title>/.match(resp.body)
136 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:\/[IV]+)?)\)\s*(.+)?$/).first
138 title = fix_article(pre_title)
141 data = grab_info(/Directors?/, resp.body)
143 dir = data.scan(NAME_MATCH).map { |url, name|
149 data = grab_info(/Country/, resp.body)
151 country = data.ircify_html.gsub(' / ','/')
154 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
157 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
159 ratings = "#{m[1]}/10 (#{m[2]} voters)"
163 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
168 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
170 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
173 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
183 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
184 rescue Exception => e
186 warning e.backtrace.join("\n")
192 if resp.code == "200"
193 m = /<title>([^<]*)<\/title>/.match(resp.body)
197 info << "#{name} : http://us.imdb.com#{sr}"
200 data = grab_info("Date of Birth", resp.body)
202 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
206 data = grab_info("Date of Death", resp.body)
208 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
211 info << [birth, death].compact.join('. ') if birth or death
217 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
222 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
223 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
225 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
231 preferred = ['Actor', 'Director']
232 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
234 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
240 all_keys = movies.keys.sort
241 debug all_keys.inspect
243 preferred.each { |key|
244 keys << key if all_keys.include? key
246 keys = all_keys if keys.empty?
250 ar.last << ": " + movies[key].join('; ')
252 info << ar.join('. ')
261 class ImdbPlugin < Plugin
262 BotConfig.register BotConfigBooleanValue.new('imdb.aka',
264 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
265 BotConfig.register BotConfigBooleanValue.new('imdb.popular',
267 :desc => "Display info on popular IMDB entries matching the request closely")
268 BotConfig.register BotConfigBooleanValue.new('imdb.exact',
270 :desc => "Display info on IMDB entries matching the request exactly")
271 BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
273 :desc => "Try to detect an article placed at the end and move it in front of the title")
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"
280 what = params[:what].to_s
281 type = params[:type].intern
283 info = i.info(what, :type => type)
285 m.reply "Nothing found for #{what}"
289 m.reply Utils.decode_html_entities info.first.join("\n")
291 m.reply info.map { |i|
292 Utils.decode_html_entities i.join(" | ")
298 plugin = ImdbPlugin.new
299 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }