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, opts[:nourl] ? nil : ": http://www.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.ircify_html]
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)
209 info.last << " : http://www.imdb.com#{sr}" unless opts[:nourl]
211 return info if opts[:name_only]
213 if opts[:movies_by_year]
214 filmoyear = @bot.httputil.get(IMDB + sr + "filmoyear")
216 info << filmoyear.scan(/#{TITLE_MATCH} \((\d\d\d\d)\)[^\[\n]*((?:\s+\[[^\]]+\](?:\s+\([^\[<]+\))*)+)\s+</)
222 data = grab_info("Date of Birth", resp.body)
224 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
228 data = grab_info("Date of Death", resp.body)
230 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
233 info << [birth, death].compact.join('. ') if birth or death
239 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
244 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
245 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
247 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
248 fix_article(tit.ircify_html)
253 preferred = ['Actor', 'Director']
254 if resp.body.match(/Jump to filmography as: (.*?)<\/div>/)
256 preferred = txt.scan(/<a[^>]+>([^<]+)<\/a>/)[0..2].map { |pref|
262 all_keys = movies.keys.sort
263 debug all_keys.inspect
265 preferred.each { |key|
266 keys << key if all_keys.include? key
268 keys = all_keys if keys.empty?
272 ar.last << ": " + movies[key].join('; ')
274 info << ar.join('. ')
282 def year_movies(urls, years_txt_org, role_req)
283 years_txt = years_txt_org.dup
284 years_txt.sub!(/^'/,'')
285 years_txt = "9#{years_txt}" if years_txt.match(/^\d\ds?$/)
286 years_txt = "1#{years_txt}" if years_txt.match(/^\d\d\ds?$/)
294 base.upto(base+9) { |year|
300 info = info_name(url, :movies_by_year => true)
304 name_url = info.first
308 # Sort by pre-title putting movies before TV series
313 (aclip == quot ? 1 : -1) <=> (bclip == quot ? 1 : -1)
314 }.each { |url, pre_title, year, pre_roles|
315 next unless years.include?(year)
316 title = fix_article(pre_title.ircify_html)
317 if title[0] == ?" and not @bot.config['imdb.tv_series_in_movies']
320 title << " (#{year})" unless years.length == 1
322 pre_roles.strip.scan(/\[([^\]]+)\]((?:\s+\([^\[]+\))+)?/) { |txt, comm|
325 if txt.match(/^(.*)\s+\.\.\.\.\s+(.*)$/)
327 extra = "(#{$2.ircify_html})"
331 next if role_req and not role.match(/^#{role_req}/i)
334 extra += comm.ircify_html if comm
336 role_array << [role, extra]
338 next if role_req and role_array.empty?
340 roles = role_array.map { |ar|
342 ar[1] # works for us both if it's nil and if it's something
347 roles = nil if roles.empty?
348 movies << [roles, title].compact.join(": ")
354 [name_url, movies.join(" | ")]
359 def name_in_movie(name_urls, movie_urls)
361 movie_urls.each { |movie|
362 title_info = info_title(movie, :title_only => true)
365 data = @bot.httputil.get(IMDB + movie + "fullcredits")
366 data.scan(CREDIT_NAME_MATCH).each { |url, name, role|
367 valid << [url, name.ircify_html, role.ircify_html] if name_urls.include?(url)
369 valid.each { |url, name, role|
370 info << "%s : %s was %s in %s" % [name, IMDB + url, role, title_info]
379 class ImdbPlugin < Plugin
380 Config.register Config::BooleanValue.new('imdb.aka',
382 :desc => "Look for IMDB matches also in translated titles and other 'also known as' information")
383 Config.register Config::BooleanValue.new('imdb.popular',
385 :desc => "Display info on popular IMDB entries matching the request closely")
386 Config.register Config::BooleanValue.new('imdb.exact',
388 :desc => "Display info on IMDB entries matching the request exactly")
389 Config.register Config::BooleanValue.new('imdb.fix_article',
391 :desc => "Try to detect an article placed at the end and move it in front of the title")
392 Config.register Config::BooleanValue.new('imdb.tv_series_in_movies',
394 :desc => "Whether searching movies by person/year should also return TV series")
396 def help(plugin, topic="")
399 "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\""
401 "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>"
403 "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"
409 TITLE_URL = %r{^http://(?:[^.]+\.)?imdb.com(/title/tt\d+/)}
410 NAME_URL = %r{^http://(?:[^.]+\.)?imdb.com(/name/nm\d+/)}
412 loc = Utils.check_location(s, TITLE_URL)
414 sr = loc.first.match(TITLE_URL)[1]
415 extra = $2 # nothign for the time being, could be fullcredits or whatever
416 res = i.info_title(sr, :nourl => true, :characters => (extra == 'fullcredits'))
419 return {:title => res.first, :content => res.last}
424 loc = Utils.check_location(s, NAME_URL)
426 sr = loc.first.match(NAME_URL)[1]
427 extra = $2 # nothing for the time being, could be filmoyear or whatever
428 res = i.info_name(sr, :nourl => true, :movies_by_year => (extra == 'filmoyear'))
432 return {:title => name, :content => res.join(". ")}
443 @bot.register_filter(:imdb, :htmlinfo) { |s| imdb_filter(s) }
446 # Find a person or movie on IMDB. A :type (name/title, default both) can be
447 # specified to limit the search to either.
451 movie = params[:movie].to_s
452 info = i.info(movie, :type => :title, :characters => true)
454 what = params[:what].to_s
455 type = params[:type].intern
456 info = i.info(what, :type => type)
458 m.reply "nothing found for #{what}"
463 m.reply Utils.decode_html_entities(info.first.join("\n"))
465 m.reply info.map { |si|
466 Utils.decode_html_entities si.join(" | ")
471 # Find the movies with a participation of :who in the year :year
472 # TODO: allow year to be either a year or a decade ('[in the] 1960s')
474 def movies(m, params)
475 who = params[:who].to_s
476 years = params[:years]
478 if role and role.downcase == 'anything'
481 case params[:prefix].intern
483 role = /actor|actress/i
489 name_urls = i.search(who, :type => :name)
491 m.reply "nothing found about #{who}, sorry"
495 movie_urls = i.year_movies(name_urls, years, role)
496 debug movie_urls.inspect
497 debug movie_urls[0][1]
499 if movie_urls.length == 1 and movie_urls[0][1]
500 m.reply movie_urls.join("\n")
502 m.reply movie_urls.map { |si|
503 si[1] = "no movies in #{years}" unless si[1]
504 Utils.decode_html_entities si.join(" | ")
509 # Find the character played by :who in :movie
511 def character(m, params)
512 who = params[:who].to_s
513 movie = params[:movie].to_s
515 name_urls = i.search(who, :type => :name)
517 m.reply "nothing found about #{who}, sorry"
521 movie_urls = i.search(movie, :type => :title)
523 m.reply "nothing found about #{who}, sorry"
527 info = i.name_in_movie(name_urls, movie_urls)
529 m.reply "nothing found about #{who} in #{movie}, sorry"
531 m.reply info.join("\n")
535 # Report the characters in movie :movie
537 def characters(m, params)
538 movie = params[:movie].to_s
540 urls = i.search(movie, :type => :title)
542 m.reply "nothing found about #{movie}"
549 plugin = ImdbPlugin.new
551 plugin.map "imdb [:type] *what", :requirements => { :type => /name|title/ }, :defaults => { :type => 'both' }
552 plugin.map "movies :prefix *who in [the] :years [as :role]", :requirements => { :prefix => /with|by|from/, :years => /'?\d+s?/ }
553 plugin.map "character [played] by *who in *movie"
554 plugin.map "character of *who in *movie"
555 plugin.map "characters in *movie", :action => :imdb