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 CREDIT_NAME_MATCH = /#{NAME_MATCH}<\/td><td[^>]+> \.\.\. <\/td><td[^>]+>(.+?)<\/td>/
22 FINAL_ARTICLE_MATCH = /, ([A-Z]\S{0,2})$/
25 :title => TITLE_MATCH,
27 :both => TITLE_OR_NAME_MATCH
34 def search(rawstr, rawopts={})
35 str = CGI.escape(rawstr)
36 str << ";site=aka" if @bot.config['imdb.aka']
38 opts[:type] = :both unless opts[:type]
39 return do_search(str, opts)
42 def do_search(str, opts={})
45 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
49 warning e.backtrace.join("\n")
54 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, opts)
95 results << info_name(sr, opts)
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)
110 if title.match(/^"(.*)"$/)
111 return "\"#{fix_article($1)}\""
113 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
116 if art[-1,1].match(/[A-Za-z]/)
124 def info_title(sr, opts={})
127 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
128 rescue Exception => e
130 warning e.backtrace.join("\n")
136 if resp.code == "200"
137 m = /<title>([^<]*)<\/title>/.match(resp.body)
140 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:\/[IV]+)?)\)\s*(.+)?$/).first
142 title = fix_article(pre_title.ircify_html)
145 data = grab_info(/Directors?/, resp.body)
147 dir = data.scan(NAME_MATCH).map { |url, name|
153 data = grab_info(/Country/, resp.body)
155 country = data.ircify_html.gsub(' / ','/')
158 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
160 return info if opts[:title_only]
163 info << resp.body.scan(CREDIT_NAME_MATCH).map { |url, name, role|
164 "%s: %s" % [name, role]
170 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
172 ratings = "#{m[1]}/10 (#{m[2]} voters)"
176 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
181 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
183 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
186 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
193 def info_name(sr, opts={})
196 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
197 rescue Exception => e
199 warning e.backtrace.join("\n")
205 if resp.code == "200"
206 m = /<title>([^<]*)<\/title>/.match(resp.body)
210 info << "#{name} : http://us.imdb.com#{sr}"
212 return info if opts[:name_only]
214 if opts[:movies_by_year]
215 filmoyear = @bot.httputil.get(IMDB + sr + "filmoyear")
217 info << filmoyear.scan(/#{TITLE_MATCH} \((\d\d\d\d)\)[^\[\n]*((?:\s+\[[^\]]+\](?:\s+\([^\[<]+\))*)+)\s+</)
223 data = grab_info("Date of Birth", resp.body)
225 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
229 data = grab_info("Date of Death", resp.body)
231 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
234 info << [birth, death].compact.join('. ') if birth or death
240 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
245 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
246 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
248 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
249 fix_article(tit.ircify_html)
254 preferred = ['Actor', 'Director']
255 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
257 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
263 all_keys = movies.keys.sort
264 debug all_keys.inspect
266 preferred.each { |key|
267 keys << key if all_keys.include? key
269 keys = all_keys if keys.empty?
273 ar.last << ": " + movies[key].join('; ')
275 info << ar.join('. ')
283 def year_movies(urls, years_txt_org)
284 years_txt = years_txt_org.dup
285 years_txt.sub!(/^'/,'')
286 years_txt = "9#{years_txt}" if years_txt.match(/^\d\ds?$/)
287 years_txt = "1#{years_txt}" if years_txt.match(/^\d\d\ds?$/)
295 base.upto(base+9) { |year|
301 info = info_name(url, :movies_by_year => true)
305 name_url = info.first
309 # Sort by pre-title putting movies before TV series
314 (aclip == quot ? 1 : -1) <=> (bclip == quot ? 1 : -1)
315 }.each { |url, pre_title, year, pre_roles|
316 next unless years.include?(year)
317 title = fix_article(pre_title.ircify_html)
318 if title[0] == ?" and not @bot.config['imdb.tv_series_in_movies']
321 title << " (#{year})" unless years.length == 1
323 pre_roles.strip.scan(/\[([^\]]+)\]((?:\s+\([^\[]+\))+)?/) { |txt, comm|
324 if txt.match(/^(.*)\s+\.\.\.\.\s+(.*)$/)
325 role_array << "#{$1} (#{$2})"
329 role_array.last << " " + comm.ircify_html if comm
332 roles = role_array.join(', ')
333 movies << [roles, title].join(": ")
339 [name_url, movies.join(" | ")]
344 def name_in_movie(name_urls, movie_urls)
346 movie_urls.each { |movie|
347 title_info = info_title(movie, :title_only => true)
350 data = @bot.httputil.get(IMDB + movie + "fullcredits")
351 data.scan(CREDIT_NAME_MATCH).each { |url, name, role|
352 valid << [url, name.ircify_html, role.ircify_html] if name_urls.include?(url)
354 valid.each { |url, name, role|
355 info << "%s : %s was %s in %s" % [name, IMDB + url, role, title_info]
364 class ImdbPlugin < Plugin
365 BotConfig.register BotConfigBooleanValue.new('imdb.aka',
367 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
368 BotConfig.register BotConfigBooleanValue.new('imdb.popular',
370 :desc => "Display info on popular IMDB entries matching the request closely")
371 BotConfig.register BotConfigBooleanValue.new('imdb.exact',
373 :desc => "Display info on IMDB entries matching the request exactly")
374 BotConfig.register BotConfigBooleanValue.new('imdb.fix_article',
376 :desc => "Try to detect an article placed at the end and move it in front of the title")
377 BotConfig.register BotConfigBooleanValue.new('imdb.tv_series_in_movies',
379 :desc => "Whether searching movies by person/year should also return TV series")
381 def help(plugin, topic="")
382 "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"
392 # Find a person or movie on IMDB. A :type (name/title, default both) can be
393 # specified to limit the search to either.
397 movie = params[:movie].to_s
398 info = i.info(movie, :type => :title, :characters => true)
400 what = params[:what].to_s
401 type = params[:type].intern
402 info = i.info(what, :type => type)
404 m.reply "nothing found for #{what}"
409 m.reply Utils.decode_html_entities info.first.join("\n")
411 m.reply info.map { |si|
412 Utils.decode_html_entities si.join(" | ")
417 # Find the movies with a participation of :who in the year :year
418 # TODO: allow year to be either a year or a decade ('[in the] 1960s')
420 def movies(m, params)
421 who = params[:who].to_s
422 years = params[:years]
424 name_urls = i.search(who, :type => :name)
426 m.reply "nothing found about #{who}, sorry"
430 movie_urls = i.year_movies(name_urls, years)
431 debug movie_urls.inspect
432 debug movie_urls[0][1]
434 if movie_urls.length == 1 and movie_urls[0][1]
435 m.reply movie_urls.join("\n")
437 m.reply movie_urls.map { |si|
438 si[1] = "no movies in #{years}" unless si[1]
439 Utils.decode_html_entities si.join(" | ")
444 # Find the character played by :who in :movie
446 def character(m, params)
447 who = params[:who].to_s
448 movie = params[:movie].to_s
450 name_urls = i.search(who, :type => :name)
452 m.reply "nothing found about #{who}, sorry"
456 movie_urls = i.search(movie, :type => :title)
458 m.reply "nothing found about #{who}, sorry"
462 info = i.name_in_movie(name_urls, movie_urls)
464 m.reply "nothing found about #{who} in #{movie}, sorry"
466 m.reply info.join("\n")
470 # Report the characters in movie :movie
472 def characters(m, params)
473 movie = params[:movie].to_s
475 urls = i.search(movie, :type => :title)
477 m.reply "nothing found about #{movie}"
484 plugin = ImdbPlugin.new
486 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }
487 plugin.map "movies :prefix *who in [the] :years", :requirements => { :prefix => /with|by|from/, :years => /'?\d+s?/ }
488 plugin.map "character [played] by *who in *movie"
489 plugin.map "character of *who in *movie"
490 plugin.map "characters in *movie", :action => :imdb