4 # :title: IMDB plugin for rbot
6 # Author:: Arnaud Cornet <arnaud.cornet@gmail.com>
7 # Copyright:: (C) 2005 Arnaud Cornet
8 # License:: MIT license
10 # Notes by Giuseppe Bilotta:
11 # TODO return more than one match (configurable)
16 IMDB = "http://us.imdb.com"
17 TITLE_OR_NAME_MATCH = /<a href="(\/(?:title|name)\/(?:tt|nm)[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
18 TITLE_MATCH = /<a href="(\/title\/tt[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
19 NAME_MATCH = /<a href="(\/name\/nm[0-9]+\/?)[^"]*"(?:[^>]*)>([^<]*)<\/a>/
26 str = URI.escape(rawstr) << ";site=aka"
33 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
37 warning e.backtrace.join("\n")
42 m = TITLE_OR_NAME_MATCH.match(resp.body)
47 elsif resp.code == "302"
48 new_loc = resp['location'].gsub(IMDB, "")
49 if new_loc.match(/\/find\?q=(.*)/)
52 return new_loc.gsub(/\?.*/, "")
61 debug "IMDB: search returned NIL"
64 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
75 def grab_info(info, body)
76 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
82 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
85 warning e.backtrace.join("\n")
90 m = /<title>([^<]*)<\/title>/.match(resp.body)
92 title = Utils.decode_html_entities(m[1])
94 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
100 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
102 plot = "Plot: #{data.ircify_html.gsub(/\s+more$/,'')}"
106 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
109 info = "#{title} : http://us.imdb.com#{sr}\n"
110 info << "Ratings: #{score}/10 (#{votes} voters). Genre: #{genre.join('/')}\n"
120 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
121 rescue Exception => e
123 warning e.backtrace.join("\n")
127 if resp.code == "200"
128 m = /<title>([^<]*)<\/title>/.match(resp.body)
130 name = Utils.decode_html_entities(m[1])
133 data = grab_info("Date of Birth", resp.body)
135 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
139 data = grab_info("Date of Death", resp.body)
141 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
148 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
153 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
154 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
156 next unless ['Actor', 'Director'].include?(what)
157 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
158 Utils.decode_html_entities(tit)
164 info = "#{name} : http://us.imdb.com#{sr}\n"
165 info << [birth, death].compact.join('. ') << "\n"
167 info << "Top Movies:: "
169 movies.keys.sort.each { |key|
171 ar.last << ": " + movies[key].join(', ')
173 info << ar.join('. ')
182 class ImdbPlugin < Plugin
183 def help(plugin, topic="")
184 "imdb <string> => search http://www.imdb.org for <string>"
188 what = params[:what].to_s
192 m.reply "Nothing found for #{what}"
199 plugin = ImdbPlugin.new
200 plugin.map "imdb *what"