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)
29 str << ";site=aka" if @bot.config['imdb.aka']
36 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
40 warning e.backtrace.join("\n")
46 m << TITLE_OR_NAME_MATCH.match(resp.body) if @bot.config['imdb.popular']
47 if resp.body.match(/\(Exact Matches\)<\/b>/) and @bot.config['imdb.exact']
48 m << TITLE_OR_NAME_MATCH.match($')
56 elsif resp.code == "302"
57 debug "automatic redirection"
58 new_loc = resp['location'].gsub(IMDB, "")
59 if new_loc.match(/\/find\?q=(.*)/)
62 return [new_loc.gsub(/\?.*/, "")]
72 debug "IMDB: search returned NIL"
77 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
80 results << info_title(sr)
82 results << info_name(sr)
90 def grab_info(info, body)
91 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
94 def fix_article(org_tit)
96 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
99 if art[-1,1].match(/[a-z]/)
110 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
111 rescue Exception => e
113 warning e.backtrace.join("\n")
119 if resp.code == "200"
120 m = /<title>([^<]*)<\/title>/.match(resp.body)
123 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:[IV]+)?)\)\s*(.+)?$/).first
125 title = fix_article(pre_title)
128 data = grab_info(/Directors?/, resp.body)
130 dir = data.scan(NAME_MATCH).map { |url, name|
136 data = grab_info(/Country/, resp.body)
138 country = data.ircify_html
141 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
144 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
146 ratings = "#{m[1]}/10 (#{m[2]} voters)"
150 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
155 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
157 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
160 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
170 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
171 rescue Exception => e
173 warning e.backtrace.join("\n")
179 if resp.code == "200"
180 m = /<title>([^<]*)<\/title>/.match(resp.body)
184 info << "#{name} : http://us.imdb.com#{sr}"
187 data = grab_info("Date of Birth", resp.body)
189 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
193 data = grab_info("Date of Death", resp.body)
195 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
198 info << [birth, death].compact.join('. ') if birth or death
204 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
209 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
210 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
212 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
218 preferred = ['Actor', 'Director']
219 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
221 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
227 all_keys = movies.keys.sort
228 debug all_keys.inspect
230 preferred.each { |key|
231 keys << key if all_keys.include? key
233 keys = all_keys if keys.empty?
237 ar.last << ": " + movies[key].join('; ')
239 info << ar.join('. ')
248 class ImdbPlugin < Plugin
249 BotConfig.register BotConfigBooleanValue.new('imdb.aka',
251 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
252 BotConfig.register BotConfigBooleanValue.new('imdb.popular',
254 :desc => "Display info on popular IMDB entries matching the request closely")
255 BotConfig.register BotConfigBooleanValue.new('imdb.exact',
257 :desc => "Display info on IMDB entries matching the request exactly")
258 BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
260 :desc => "Try to detect an article placed at the end and move it in front of the title")
262 def help(plugin, topic="")
263 "imdb <string> => search http://www.imdb.org for <string>"
267 what = params[:what].to_s
271 m.reply "Nothing found for #{what}"
275 m.reply Utils.decode_html_entities info.first.join("\n")
277 m.reply info.map { |i|
278 Utils.decode_html_entities i.join(" | ")
284 plugin = ImdbPlugin.new
285 plugin.map "imdb *what"