4 # :title: Google and Wikipedia search plugin for rbot
6 # Author:: Tom Gilbert (giblet) <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
9 # Copyright:: (C) 2002-2005 Tom Gilbert
10 # Copyright:: (C) 2006 Tom Gilbert, Giuseppe Bilotta
11 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
13 # TODO:: use lr=lang_<code> or whatever is most appropriate to let google know
14 # it shouldn't use the bot's location to find the preferred language
15 # TODO:: support localized uncyclopedias -- not easy because they have different names
18 GOOGLE_SEARCH = "http://www.google.com/search?oe=UTF-8&q="
19 GOOGLE_WAP_SEARCH = "http://www.google.com/m/search?hl=en&q="
20 # GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
21 GOOGLE_WAP_LINK = /<a href="(?:.*?u=(.*?)|(http:\/\/.*?))">(.*?)<\/a>/im
22 GOOGLE_CALC_RESULT = %r{<img src=/images/calc_img\.gif(?: width=40 height=30 alt="")?>.*?<h2 class=r[^>]*><b>(.+?)</b>}
23 GOOGLE_COUNT_RESULT = %r{<font size=-1>Results <b>1<\/b> - <b>10<\/b> of about <b>(.*)<\/b> for}
24 GOOGLE_DEF_RESULT = %r{<a href="([^"]*)"[^>]*>(Web definitions for .*?)<br/>(.*?)<br/>(.*?)\s-\s+<a href}
25 GOOGLE_TIME_RESULT = %r{alt="Clock"></td><td valign=[^>]+>(.+?)<(br|/td)>}
27 class SearchPlugin < Plugin
28 Config.register Config::IntegerValue.new('google.hits',
30 :desc => "Number of hits to return from Google searches")
31 Config.register Config::IntegerValue.new('google.first_par',
33 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
34 Config.register Config::IntegerValue.new('wikipedia.hits',
36 :desc => "Number of hits to return from Wikipedia searches")
37 Config.register Config::IntegerValue.new('wikipedia.first_par',
39 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
41 def help(plugin, topic="")
43 when "search", "google"
44 "#{topic} <string> => search google for <string>"
46 "gcalc <equation> => use the google calculator to find the answer to <equation>"
48 "gdef <term(s)> => use the google define mechanism to find a definition of <term(s)>"
50 "gtime <location> => use the google clock to find the current time at <location>"
52 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
54 "unpedia <string> => search for <string> on Uncyclopedia"
56 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
61 what = params[:words].to_s
62 if what.match(/^define:/)
63 return google_define(m, what, params)
66 searchfor = CGI.escape what
67 # This method is also called by other methods to restrict searching to some sites
69 site = "site:#{params[:site]}+"
73 # It is also possible to choose a filter to remove constant parts from the titles
74 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
75 filter = params[:filter] || ""
77 url = GOOGLE_WAP_SEARCH + site + searchfor
79 hits = params[:hits] || @bot.config['google.hits']
80 hits = 1 if params[:lucky]
82 first_pars = params[:firstpar] || @bot.config['google.first_par']
84 single = params[:lucky] || (hits == 1 and first_pars == 1)
87 wml = @bot.httputil.get(url)
90 m.reply "error googling for #{what}"
93 results = wml.scan(GOOGLE_WAP_LINK)
95 if results.length == 0
96 m.reply "no results found for #{what}"
100 single ||= (results.length==1)
104 results = results[0...hits].map { |res|
106 t = res[2].ircify_html(:img => "[%{src} %{alt} %{dimensions}]").strip
107 u = URI.unescape(res[0] || res[1])
109 "%{n}%{b}%{t}%{b}%{sep}%{u}" % {
110 :n => (single ? "" : "#{n}. "),
111 :sep => (single ? " -- " : ": "),
112 :b => Bold, :t => t, :u => u
117 m.reply results.first
121 result_string = results.join(" | ")
123 # If we return a single, full result, change the output to a more compact representation
125 m.reply "Result for %s: %s -- %s" % [what, result_string, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
129 m.reply "Results for #{what}: #{result_string}", :split_at => /\s+\|\s+/
131 return unless first_pars > 0
133 Utils.get_first_pars urls, first_pars, :message => m
137 def google_define(m, what, params)
139 wml = @bot.httputil.get(GOOGLE_SEARCH + CGI.escape(what))
142 m.reply "error googling for #{what}"
147 related_index = wml.index(/Related phrases:/, 0)
148 raise unless related_index
149 defs_index = wml.index(/Definitions of <b>/, related_index)
150 raise unless defs_index
151 defs_end = wml.index(/<input/, defs_index)
152 raise unless defs_end
154 m.reply "no results found for #{what}"
158 related = wml[related_index...defs_index]
159 defs = wml[defs_index...defs_end]
161 m.reply defs.ircify_html(:a_href => Underline), :split_at => (Underline + ' ')
166 params.merge!(:lucky => true)
171 what = params[:words].to_s
172 searchfor = CGI.escape(what)
174 debug "Getting gcalc thing: #{searchfor.inspect}"
175 url = GOOGLE_SEARCH + searchfor
178 html = @bot.httputil.get(url)
180 m.reply "error googlecalcing #{what}"
184 debug "#{html.size} bytes of html recieved"
186 results = html.scan(GOOGLE_CALC_RESULT)
187 debug "results: #{results.inspect}"
189 if results.length != 1
190 m.reply "couldn't calculate #{what}"
194 result = results[0][0].ircify_html
195 debug "replying with: #{result.inspect}"
199 def gcount(m, params)
200 what = params[:words].to_s
201 searchfor = CGI.escape(what)
203 debug "Getting gcount thing: #{searchfor.inspect}"
204 url = GOOGLE_SEARCH + searchfor
207 html = @bot.httputil.get(url)
209 m.reply "error googlecounting #{what}"
213 debug "#{html.size} bytes of html recieved"
215 results = html.scan(GOOGLE_COUNT_RESULT)
216 debug "results: #{results.inspect}"
218 if results.length != 1
219 m.reply "couldn't count #{what}"
223 result = results[0][0].ircify_html
224 debug "replying with: #{result.inspect}"
225 m.reply "total results: #{result}"
230 what = params[:words].to_s
231 searchfor = CGI.escape("define " + what)
233 debug "Getting gdef thing: #{searchfor.inspect}"
234 url = GOOGLE_WAP_SEARCH + searchfor
237 html = @bot.httputil.get(url)
239 m.reply "error googledefining #{what}"
244 results = html.scan(GOOGLE_DEF_RESULT)
245 debug "results: #{results.inspect}"
247 if results.length != 1
248 m.reply "couldn't find a definition for #{what} on Google"
252 gdef_link = "http://www.google.com" + CGI.unescapeHTML(results[0][0]) # could be used to extract all defs
253 head = results[0][1].ircify_html
254 text = results[0][2].ircify_html
256 m.reply "#{head} -- #{link}\n#{text}"
258 ### gdef_link could be used for something like
259 # html_defs = @bot.httputil.get(gdef_link)
260 # related_index = html_defs.index(/Related phrases:/, 0)
261 # defs_index = html_defs.index(/Definitions of <b>/, related_index)
263 # related = html_defs[related_index..defs_index]
264 # defs = html_defs[defs_index..-1]
266 # m.reply defs.gsub(' <br/>','<li>').ircify_html
269 def wikipedia(m, params)
271 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
272 debug "Looking up things on #{site}"
274 params[:filter] = / - Wikipedia.*$/
275 params[:hits] = @bot.config['wikipedia.hits']
276 params[:firstpar] = @bot.config['wikipedia.first_par']
277 return google(m, params)
280 def unpedia(m, params)
281 site = "uncyclopedia.org"
282 debug "Looking up things on #{site}"
284 params[:filter] = / - Uncyclopedia.*$/
285 params[:hits] = @bot.config['wikipedia.hits']
286 params[:firstpar] = @bot.config['wikipedia.first_par']
287 return google(m, params)
291 where = params[:words].to_s
292 where.sub!(/^\s*in\s*/, '')
293 searchfor = CGI.escape("time in " + where)
294 url = GOOGLE_SEARCH + searchfor
297 html = @bot.httputil.get(url)
299 m.reply "Error googletiming #{where}"
304 results = html.scan(GOOGLE_TIME_RESULT)
305 debug "results: #{results.inspect}"
307 if results.length != 1
308 m.reply "Couldn't find the time for #{where} on Google"
312 time = results[0][0].ircify_html
317 plugin = SearchPlugin.new
319 plugin.map "search *words", :action => 'google', :threaded => true
320 plugin.map "google *words", :action => 'google', :threaded => true
321 plugin.map "lucky *words", :action => 'lucky', :threaded => true
322 plugin.map "gcount *words", :action => 'gcount', :threaded => true
323 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
324 plugin.map "gdef *words", :action => 'gdef', :threaded => true
325 plugin.map "gtime *words", :action => 'gtime', :threaded => true
326 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
327 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
328 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true