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})$/
28 str = URI.escape(rawstr) << ";site=aka"
35 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
39 warning e.backtrace.join("\n")
45 m << TITLE_OR_NAME_MATCH.match(resp.body) if @bot.config['imdb.popular']
46 if resp.body.match(/\(Exact Matches\)<\/b>/) and @bot.config['imdb.exact']
47 m << TITLE_OR_NAME_MATCH.match($')
55 elsif resp.code == "302"
56 debug "automatic redirection"
57 new_loc = resp['location'].gsub(IMDB, "")
58 if new_loc.match(/\/find\?q=(.*)/)
61 return [new_loc.gsub(/\?.*/, "")]
71 debug "IMDB: search returned NIL"
76 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
79 results << info_title(sr)
81 results << info_name(sr)
89 def grab_info(info, body)
90 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
93 def fix_article(org_tit)
95 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
98 if art[-1,1].match(/[a-z]/)
109 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
110 rescue Exception => e
112 warning e.backtrace.join("\n")
118 if resp.code == "200"
119 m = /<title>([^<]*)<\/title>/.match(resp.body)
122 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:[IV]+)?)\)\s*(.+)?$/).first
124 title = fix_article(pre_title)
127 data = grab_info(/Directors?/, resp.body)
129 dir = data.scan(NAME_MATCH).map { |url, name|
135 data = grab_info(/Country/, resp.body)
137 country = data.ircify_html
140 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
143 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
145 ratings = "#{m[1]}/10 (#{m[2]} voters)"
149 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
154 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
156 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
159 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
169 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
170 rescue Exception => e
172 warning e.backtrace.join("\n")
178 if resp.code == "200"
179 m = /<title>([^<]*)<\/title>/.match(resp.body)
183 info << "#{name} : http://us.imdb.com#{sr}"
186 data = grab_info("Date of Birth", resp.body)
188 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
192 data = grab_info("Date of Death", resp.body)
194 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
197 info << [birth, death].compact.join('. ') if birth or death
203 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
208 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
209 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
211 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
217 preferred = ['Actor', 'Director']
218 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
220 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
226 all_keys = movies.keys.sort
227 debug all_keys.inspect
229 preferred.each { |key|
230 keys << key if all_keys.include? key
232 keys = all_keys if keys.empty?
236 ar.last << ": " + movies[key].join('; ')
238 info << ar.join('. ')
247 class ImdbPlugin < Plugin
248 BotConfig.register BotConfigBooleanValue.new('imdb.popular',
250 :desc => "Display info on popular IMDB entries matching the request closely")
251 BotConfig.register BotConfigBooleanValue.new('imdb.exact',
253 :desc => "Display info on IMDB entries matching the request exactly")
254 BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
256 :desc => "Try to detect an article placed at the end and move it in front of the title")
258 def help(plugin, topic="")
259 "imdb <string> => search http://www.imdb.org for <string>"
263 what = params[:what].to_s
267 m.reply "Nothing found for #{what}"
271 m.reply Utils.decode_html_entities info.first.join("\n")
273 m.reply info.map { |i|
274 Utils.decode_html_entities i.join(" | ")
280 plugin = ImdbPlugin.new
281 plugin.map "imdb *what"