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]]
57 m << matcher.match(resp.body) if @bot.config['imdb.popular']
58 if resp.body.match(/\(Exact Matches\)<\/b>/) and @bot.config['imdb.exact']
59 m << matcher.match($')
67 elsif resp.code == "302"
68 debug "automatic redirection"
69 new_loc = resp['location'].gsub(IMDB, "")
70 if new_loc.match(/\/find\?q=(.*)/)
71 return do_search($1, opts)
73 return [new_loc.gsub(/\?.*/, "")]
79 def info(rawstr, opts={})
81 urls = search(rawstr, opts)
84 debug "IMDB: search returned NIL"
89 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
92 results << info_title(sr)
94 results << info_name(sr)
102 def grab_info(info, body)
103 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
106 def fix_article(org_tit)
109 if title.match(/^"(.*)"$/)
110 return "\"#{fix_article($1)}\""
112 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
115 if art[-1,1].match(/[A-Za-z]/)
123 def info_title(sr, opts={})
126 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
127 rescue Exception => e
129 warning e.backtrace.join("\n")
135 if resp.code == "200"
136 m = /<title>([^<]*)<\/title>/.match(resp.body)
139 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:\/[IV]+)?)\)\s*(.+)?$/).first
141 title = fix_article(pre_title.ircify_html)
144 data = grab_info(/Directors?/, resp.body)
146 dir = data.scan(NAME_MATCH).map { |url, name|
152 data = grab_info(/Country/, resp.body)
154 country = data.ircify_html.gsub(' / ','/')
157 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
159 return info if opts[:title_only]
162 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
164 ratings = "#{m[1]}/10 (#{m[2]} voters)"
168 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
173 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
175 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
178 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
185 def info_name(sr, opts={})
188 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
189 rescue Exception => e
191 warning e.backtrace.join("\n")
197 if resp.code == "200"
198 m = /<title>([^<]*)<\/title>/.match(resp.body)
202 info << "#{name} : http://us.imdb.com#{sr}"
204 return info if opts[:name_only]
206 if year = opts[:movies_in_year]
207 filmoyear = @bot.httputil.get(IMDB + sr + "filmoyear")
209 info << filmoyear.scan(/#{TITLE_MATCH} \(#{year}\)[^\[\n]*((?:\s+\[[^\]]+\](?:\s+\([^\[<]+\))*)+)\s+</)
215 data = grab_info("Date of Birth", resp.body)
217 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
221 data = grab_info("Date of Death", resp.body)
223 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
226 info << [birth, death].compact.join('. ') if birth or death
232 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
237 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
238 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
240 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
241 fix_article(tit.ircify_html)
246 preferred = ['Actor', 'Director']
247 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
249 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
255 all_keys = movies.keys.sort
256 debug all_keys.inspect
258 preferred.each { |key|
259 keys << key if all_keys.include? key
261 keys = all_keys if keys.empty?
265 ar.last << ": " + movies[key].join('; ')
267 info << ar.join('. ')
275 def year_movies(urls, year)
277 info = info_name(url, :movies_in_year => year)
281 name_url = info.first
285 # Sort by pre-title putting movies before TV series
290 (aclip == quot ? 1 : -1) <=> (bclip == quot ? 1 : -1)
291 }.each { |url, pre_title, pre_roles|
292 title = fix_article(pre_title.ircify_html)
293 if title[0] == ?" and not @bot.config['imdb.tv_series_in_movies']
297 pre_roles.strip.scan(/\[([^\]]+)\]((?:\s+\([^\[]+\))+)?/) { |txt, comm|
298 if txt.match(/^(.*)\s+\.\.\.\.\s+(.*)$/)
299 role_array << "#{$1} (#{$2})"
303 role_array.last << " " + comm.ircify_html if comm
306 roles = role_array.join(', ')
307 movies << [roles, title].join(": ")
313 [name_url, movies.join(" | ")]
318 def name_in_movie(name_urls, movie_urls)
320 movie_urls.each { |movie|
321 title_info = info_title(movie, :title_only => true)
324 data = @bot.httputil.get(IMDB + movie + "fullcredits")
325 data.scan(/#{NAME_MATCH}<\/td><td[^>]+> \.\.\. <\/td><td[^>]+>(.+?)<\/td>/).each { |url, name, role|
326 valid << [url, name.ircify_html, role.ircify_html] if name_urls.include?(url)
328 valid.each { |url, name, role|
329 info << "%s : %s was %s in %s" % [name, IMDB + url, role, title_info]
338 class ImdbPlugin < Plugin
339 BotConfig.register BotConfigBooleanValue.new('imdb.aka',
341 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
342 BotConfig.register BotConfigBooleanValue.new('imdb.popular',
344 :desc => "Display info on popular IMDB entries matching the request closely")
345 BotConfig.register BotConfigBooleanValue.new('imdb.exact',
347 :desc => "Display info on IMDB entries matching the request exactly")
348 BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
350 :desc => "Try to detect an article placed at the end and move it in front of the title")
351 BotConfig.register BotConfigBooleanValue.new('imdb.tv_series_in_movies',
353 :desc => "Whether searching movies by person/year should also return TV series")
355 def help(plugin, topic="")
356 "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"
367 what = params[:what].to_s
368 type = params[:type].intern
369 info = i.info(what, :type => type)
371 m.reply "Nothing found for #{what}"
375 m.reply Utils.decode_html_entities info.first.join("\n")
377 m.reply info.map { |si|
378 Utils.decode_html_entities si.join(" | ")
383 def movies(m, params)
384 who = params[:who].to_s
387 name_urls = i.search(who, :type => :name)
389 m.reply "nothing found about #{who}, sorry"
393 movie_urls = i.year_movies(name_urls, year)
394 debug movie_urls.inspect
395 debug movie_urls[0][1]
397 if movie_urls.length == 1 and movie_urls[0][1]
398 m.reply movie_urls.join("\n")
400 m.reply movie_urls.map { |si|
401 si[1] = "no movies in #{year}" unless si[1]
402 Utils.decode_html_entities si.join(" | ")
407 def character(m, params)
408 who = params[:who].to_s
409 movie = params[:movie].to_s
411 name_urls = i.search(who, :type => :name)
413 m.reply "nothing found about #{who}, sorry"
417 movie_urls = i.search(movie, :type => :title)
419 m.reply "nothing found about #{who}, sorry"
423 info = i.name_in_movie(name_urls, movie_urls)
425 m.reply "nothing found about #{who} in #{movie}, sorry"
427 m.reply info.join("\n")
433 plugin = ImdbPlugin.new
435 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }
436 plugin.map "movies :prefix *who in :year", :requirements => { :prefix => /with|by|from/, :year => /\d+/ }
437 plugin.map "character [played] by *who in *movie"
438 plugin.map "character of *who in *movie"