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>/
27 str = URI.escape(rawstr) << ";site=aka"
34 resp = @bot.httputil.get_response(IMDB + "/find?q=#{str}",
38 warning e.backtrace.join("\n")
44 m << TITLE_OR_NAME_MATCH.match(resp.body)
45 if resp.body.match(/\(Exact Matches\)<\/b>/)
46 m << TITLE_OR_NAME_MATCH.match($')
56 elsif resp.code == "302"
57 debug "automatic redirection"
58 new_loc = resp['location'].gsub(IMDB, "")
59 if new_loc.match(/\/find\?q=(.*)/)
62 return [new_loc.gsub(/\?.*/, "")]
72 debug "IMDB: search returned NIL"
77 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
80 results << info_title(sr)
82 results << info_name(sr)
90 def grab_info(info, body)
91 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
97 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
100 warning e.backtrace.join("\n")
104 if resp.code == "200"
105 m = /<title>([^<]*)<\/title>/.match(resp.body)
107 title = Utils.decode_html_entities(m[1])
109 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
115 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
117 plot = "Plot: #{data.ircify_html.gsub(/\s+more$/,'')}"
121 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
124 info = "#{title} : http://us.imdb.com#{sr}\n"
125 info << "Ratings: #{score}/10 (#{votes} voters). Genre: #{genre.join('/')}\n"
135 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
136 rescue Exception => e
138 warning e.backtrace.join("\n")
142 if resp.code == "200"
143 m = /<title>([^<]*)<\/title>/.match(resp.body)
145 name = Utils.decode_html_entities(m[1])
148 data = grab_info("Date of Birth", resp.body)
150 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
154 data = grab_info("Date of Death", resp.body)
156 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
163 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
168 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
169 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
171 next unless ['Actor', 'Director'].include?(what)
172 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
173 Utils.decode_html_entities(tit)
179 info = "#{name} : http://us.imdb.com#{sr}\n"
180 info << [birth, death].compact.join('. ') << "\n"
182 info << "Top Movies:: "
184 movies.keys.sort.each { |key|
186 ar.last << ": " + movies[key].join(', ')
188 info << ar.join('. ')
197 class ImdbPlugin < Plugin
198 def help(plugin, topic="")
199 "imdb <string> => search http://www.imdb.org for <string>"
203 what = params[:what].to_s
207 m.reply "Nothing found for #{what}"
213 m.reply info.map { |i|
220 plugin = ImdbPlugin.new
221 plugin.map "imdb *what"