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($')
54 elsif resp.code == "302"
55 debug "automatic redirection"
56 new_loc = resp['location'].gsub(IMDB, "")
57 if new_loc.match(/\/find\?q=(.*)/)
60 return [new_loc.gsub(/\?.*/, "")]
70 debug "IMDB: search returned NIL"
75 type = sr.match(/^\/([^\/]+)\//)[1].downcase.intern rescue nil
78 results << info_title(sr)
80 results << info_name(sr)
88 def grab_info(info, body)
89 /<div class="info">\s+<h5>#{info}:<\/h5>\s+(.*?)<\/div>/mi.match(body)[1] rescue nil
95 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
98 warning e.backtrace.join("\n")
104 if resp.code == "200"
105 m = /<title>([^<]*)<\/title>/.match(resp.body)
108 title, date = title_date.scan(/^(.*)\((\d\d\d\d(?:[IV]+)?)\)$/).first
112 data = grab_info(/Directors?/, resp.body)
114 dir = data.scan(NAME_MATCH).map { |url, name|
120 data = grab_info(/Country/, resp.body)
122 country = data.ircify_html
125 info << [title, "(#{country}, #{date})", dir ? "[#{dir}]" : nil, ": http://us.imdb.com#{sr}"].compact.join(" ")
127 m = /<b>([0-9.]+)\/10<\/b>\n?\r?\s+<small>\(<a href="ratings">([0-9,]+) votes?<\/a>\)<\/small>/.match(resp.body)
133 resp.body.scan(/<a href="\/Sections\/Genres\/[^\/]+\/">([^<]+)<\/a>/) do |gnr|
137 info << "Ratings: #{score}/10 (#{votes} voters). Genre: #{genre.join('/')}"
140 data = grab_info(/Plot (?:Outline|Summary)/, resp.body)
142 plot = "Plot: #{data.ircify_html.gsub(/\s+more$/,'')}"
154 resp = @bot.httputil.get_response(IMDB + sr, :max_redir => -1)
155 rescue Exception => e
157 warning e.backtrace.join("\n")
163 if resp.code == "200"
164 m = /<title>([^<]*)<\/title>/.match(resp.body)
168 info << "#{name} : http://us.imdb.com#{sr}"
171 data = grab_info("Date of Birth", resp.body)
173 birth = "Birth: #{data.ircify_html.gsub(/\s+more$/,'')}"
177 data = grab_info("Date of Death", resp.body)
179 death = "Death: #{data.ircify_html.gsub(/\s+more$/,'')}"
182 info << [birth, death].compact.join('. ')
188 filmorate = @bot.httputil.get(IMDB + sr + "filmorate")
193 filmorate.scan(/<div class="filmo">.*?<a href="\/title.*?<\/div>/m) { |str|
194 what = str.match(/<a name="[^"]+">([^<]+)<\/a>/)[1] rescue nil
196 next unless ['Actor', 'Director'].include?(what)
197 movies[what] = str.scan(TITLE_MATCH)[0..2].map { |url, tit|
205 movies.keys.sort.each { |key|
207 ar.last << ": " + movies[key].join(', ')
209 info << "Top Movies:: " + ar.join('. ')
218 class ImdbPlugin < Plugin
219 def help(plugin, topic="")
220 "imdb <string> => search http://www.imdb.org for <string>"
224 what = params[:what].to_s
228 m.reply "Nothing found for #{what}"
232 m.reply Utils.decode_html_entities info.first.join("\n")
234 m.reply info.map { |i|
235 Utils.decode_html_entities i.join(" | ")
241 plugin = ImdbPlugin.new
242 plugin.map "imdb *what"