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
15 IMDB = "http://us.imdb.com"
16 TITLE_OR_NAME_MATCH = /<a href="(\/(?:title|name)\/(?:tt|nm)[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
17 TITLE_MATCH = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
18 NAME_MATCH = /<a href="(\/name\/nm[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
19 CREDIT_NAME_MATCH = /#{NAME_MATCH}<\/td><td[^>]+> \.\.\. <\/td><td[^>]+>(.+?)<\/td>/
20 FINAL_ARTICLE_MATCH = /, ([A-Z]\S{0,2})$/
23 :title => TITLE_MATCH,
25 :both => TITLE_OR_NAME_MATCH
32 def search(rawstr, rawopts={})
33 str = CGI.escape(rawstr)
34 str << ";site=aka" if @bot.config['imdb.aka']
36 opts[:type] = :both unless opts[:type]
37 return do_search(str, opts)
40 def do_search(str, opts={})
43 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
47 warning e.backtrace.join("\n")
52 matcher = MATCHER[opts[:type]]
56 m << matcher.match(resp.body) if @bot.config['imdb.popular']
57 if resp.body.match(/\(Exact Matches\)<\/b>/) and @bot.config['imdb.exact']
58 m << matcher.match($')
66 elsif resp.code == "302"
67 debug "automatic redirection"
68 new_loc = resp['location'].gsub(IMDB, "")
69 if new_loc.match(/\/find\?q=(.*)/)
70 return do_search($1, opts)
72 return [new_loc.gsub(/\?.*/, "")]
78 def info(rawstr, opts={})
80 urls = search(rawstr, opts)
83 debug "IMDB: search returned NIL"
88 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
91 results << info_title(sr, opts)
93 results << info_name(sr, opts)
101 def grab_info(info, body)
102 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
105 def fix_article(org_tit)
108 if title.match(/^"(.*)"$/)
109 return "\"#{fix_article($1)}\""
111 if @bot.config['imdb.fix_article'] and title.gsub!(FINAL_ARTICLE_MATCH, '')
114 if art[-1,1].match(/[A-Za-z]/)
122 def info_title(sr, opts={})
125 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
126 rescue Exception => e
128 warning e.backtrace.join("\n")
134 if resp.code == "200"
135 m = /<title>([^<]*)<\/title>/.match(resp.body)
138 pre_title, date, extra = title_date.scan(/^(.*)\((\d\d\d\d(?:\/[IV]+)?)\)\s*(.+)?$/).first
140 title = fix_article(pre_title.ircify_html)
143 data = grab_info(/Directors?/, resp.body)
145 dir = data.scan(NAME_MATCH).map { |url, name|
151 data = grab_info(/Country/, resp.body)
153 country = data.ircify_html.gsub(' / ','/')
156 info << [title, "(#{country}, #{date})", extra, dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
158 return info if opts[:title_only]
161 info << resp.body.scan(CREDIT_NAME_MATCH).map { |url, name, role|
162 "%s: %s" % [name, role]
168 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
170 ratings = "#{m[1]}/10 (#{m[2]} voters)"
174 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
179 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
181 plot = "Plot: " + data.ircify_html.gsub(/\s+more$/,'')
184 info << ["Ratings: " << ratings, "Genre: " << genre.join('/') , plot].compact.join(". ")
191 def info_name(sr, opts={})
194 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
195 rescue Exception => e
197 warning e.backtrace.join("\n")
203 if resp.code == "200"
204 m = /<title>([^<]*)<\/title>/.match(resp.body)
208 info << "#{name} : http://us.imdb.com#{sr}"
210 return info if opts[:name_only]
212 if opts[:movies_by_year]
213 filmoyear = @bot.httputil.get(IMDB + sr + "filmoyear")
215 info << filmoyear.scan(/#{TITLE_MATCH} \((\d\d\d\d)\)[^\[\n]*((?:\s+\[[^\]]+\](?:\s+\([^\[<]+\))*)+)\s+</)
221 data = grab_info("Date of Birth", resp.body)
223 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
227 data = grab_info("Date of Death", resp.body)
229 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
232 info << [birth, death].compact.join('. ') if birth or death
238 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
243 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
244 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
246 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
247 fix_article(tit.ircify_html)
252 preferred = ['Actor', 'Director']
253 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
255 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
261 all_keys = movies.keys.sort
262 debug all_keys.inspect
264 preferred.each { |key|
265 keys << key if all_keys.include? key
267 keys = all_keys if keys.empty?
271 ar.last << ": " + movies[key].join('; ')
273 info << ar.join('. ')
281 def year_movies(urls, years_txt_org, role_req)
282 years_txt = years_txt_org.dup
283 years_txt.sub!(/^'/,'')
284 years_txt = "9#{years_txt}" if years_txt.match(/^\d\ds?$/)
285 years_txt = "1#{years_txt}" if years_txt.match(/^\d\d\ds?$/)
293 base.upto(base+9) { |year|
299 info = info_name(url, :movies_by_year => true)
303 name_url = info.first
307 # Sort by pre-title putting movies before TV series
312 (aclip == quot ? 1 : -1) <=> (bclip == quot ? 1 : -1)
313 }.each { |url, pre_title, year, pre_roles|
314 next unless years.include?(year)
315 title = fix_article(pre_title.ircify_html)
316 if title[0] == ?" and not @bot.config['imdb.tv_series_in_movies']
319 title << " (#{year})" unless years.length == 1
321 pre_roles.strip.scan(/\[([^\]]+)\]((?:\s+\([^\[]+\))+)?/) { |txt, comm|
324 if txt.match(/^(.*)\s+\.\.\.\.\s+(.*)$/)
326 extra = "(#{$2.ircify_html})"
330 next if role_req and not role.match(/^#{role_req}/i)
333 extra += comm.ircify_html if comm
335 role_array << [role, extra]
337 next if role_req and role_array.empty?
339 roles = role_array.map { |ar|
341 ar[1] # works for us both if it's nil and if it's something
346 roles = nil if roles.empty?
347 movies << [roles, title].compact.join(": ")
353 [name_url, movies.join(" | ")]
358 def name_in_movie(name_urls, movie_urls)
360 movie_urls.each { |movie|
361 title_info = info_title(movie, :title_only => true)
364 data = @bot.httputil.get(IMDB + movie + "fullcredits")
365 data.scan(CREDIT_NAME_MATCH).each { |url, name, role|
366 valid << [url, name.ircify_html, role.ircify_html] if name_urls.include?(url)
368 valid.each { |url, name, role|
369 info << "%s : %s was %s in %s" % [name, IMDB + url, role, title_info]
378 class ImdbPlugin < Plugin
379 Config.register Config::BooleanValue.new('imdb.aka',
381 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
382 Config.register Config::BooleanValue.new('imdb.popular',
384 :desc => "Display info on popular IMDB entries matching the request closely")
385 Config.register Config::BooleanValue.new('imdb.exact',
387 :desc => "Display info on IMDB entries matching the request exactly")
388 Config.register Config::BooleanValue.new('imdb.fix_article',
390 :desc => "Try to detect an article placed at the end and move it in front of the title")
391 Config.register Config::BooleanValue.new('imdb.tv_series_in_movies',
393 :desc => "Whether searching movies by person/year should also return TV series")
395 def help(plugin, topic="")
398 "movies by <who> in <years> [as <role>] => display the movies in the <years> where which <who> was <role>; <role> can be one of actor, actress, director or anything: if it's omitted, the role is defined by the prefix: \"movies by ...\" implies director, \"movies with ...\" implies actor or actress; the years can be specified as \"in the 60s\" or as \"in 1953\""
400 "character played by <who> in <movie> => show the character played by <who> in movie <movie>. characters in <movie> => show the actors and characters in movie <movie>"
402 "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. see also movies and characters"
413 # Find a person or movie on IMDB. A :type (name/title, default both) can be
414 # specified to limit the search to either.
418 movie = params[:movie].to_s
419 info = i.info(movie, :type => :title, :characters => true)
421 what = params[:what].to_s
422 type = params[:type].intern
423 info = i.info(what, :type => type)
425 m.reply "nothing found for #{what}"
430 m.reply Utils.decode_html_entities(info.first.join("\n"))
432 m.reply info.map { |si|
433 Utils.decode_html_entities si.join(" | ")
438 # Find the movies with a participation of :who in the year :year
439 # TODO: allow year to be either a year or a decade ('[in the] 1960s')
441 def movies(m, params)
442 who = params[:who].to_s
443 years = params[:years]
445 if role and role.downcase == 'anything'
448 case params[:prefix].intern
450 role = /actor|actress/i
456 name_urls = i.search(who, :type => :name)
458 m.reply "nothing found about #{who}, sorry"
462 movie_urls = i.year_movies(name_urls, years, role)
463 debug movie_urls.inspect
464 debug movie_urls[0][1]
466 if movie_urls.length == 1 and movie_urls[0][1]
467 m.reply movie_urls.join("\n")
469 m.reply movie_urls.map { |si|
470 si[1] = "no movies in #{years}" unless si[1]
471 Utils.decode_html_entities si.join(" | ")
476 # Find the character played by :who in :movie
478 def character(m, params)
479 who = params[:who].to_s
480 movie = params[:movie].to_s
482 name_urls = i.search(who, :type => :name)
484 m.reply "nothing found about #{who}, sorry"
488 movie_urls = i.search(movie, :type => :title)
490 m.reply "nothing found about #{who}, sorry"
494 info = i.name_in_movie(name_urls, movie_urls)
496 m.reply "nothing found about #{who} in #{movie}, sorry"
498 m.reply info.join("\n")
502 # Report the characters in movie :movie
504 def characters(m, params)
505 movie = params[:movie].to_s
507 urls = i.search(movie, :type => :title)
509 m.reply "nothing found about #{movie}"
516 plugin = ImdbPlugin.new
518 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }
519 plugin.map "movies :prefix *who in [the] :years [as :role]", :requirements => { :prefix => /with|by|from/, :years => /'?\d+s?/ }
520 plugin.map "character [played] by *who in *movie"
521 plugin.map "character of *who in *movie"
522 plugin.map "characters in *movie", :action => :imdb