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="")?>.*?<h[1-6] 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.match('<p align="center">').pre_match.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_WAP_SEARCH + searchfor
178 html = @bot.httputil.get(url)
180 m.reply "error googlecalcing #{what}"
184 debug "#{html.size} bytes of html recieved"
186 intro, result, junk = html.split(/\s*<br\/>\s*/, 3)
187 debug "result: #{result.inspect}"
189 unless result.include? '='
190 m.reply "couldn't calculate #{what}"
194 debug "replying with: #{result.inspect}"
195 m.reply result.ircify_html
198 def gcount(m, params)
199 what = params[:words].to_s
200 searchfor = CGI.escape(what)
202 debug "Getting gcount thing: #{searchfor.inspect}"
203 url = GOOGLE_SEARCH + searchfor
206 html = @bot.httputil.get(url)
208 m.reply "error googlecounting #{what}"
212 debug "#{html.size} bytes of html recieved"
214 results = html.scan(GOOGLE_COUNT_RESULT)
215 debug "results: #{results.inspect}"
217 if results.length != 1
218 m.reply "couldn't count #{what}"
222 result = results[0][0].ircify_html
223 debug "replying with: #{result.inspect}"
224 m.reply "total results: #{result}"
229 what = params[:words].to_s
230 searchfor = CGI.escape("define " + what)
232 debug "Getting gdef thing: #{searchfor.inspect}"
233 url = GOOGLE_WAP_SEARCH + searchfor
236 html = @bot.httputil.get(url)
238 m.reply "error googledefining #{what}"
243 results = html.scan(GOOGLE_DEF_RESULT)
244 debug "results: #{results.inspect}"
246 if results.length != 1
247 m.reply "couldn't find a definition for #{what} on Google"
251 gdef_link = "http://www.google.com" + CGI.unescapeHTML(results[0][0]) # could be used to extract all defs
252 head = results[0][1].ircify_html
253 text = results[0][2].ircify_html
255 m.reply "#{head} -- #{link}\n#{text}"
257 ### gdef_link could be used for something like
258 # html_defs = @bot.httputil.get(gdef_link)
259 # related_index = html_defs.index(/Related phrases:/, 0)
260 # defs_index = html_defs.index(/Definitions of <b>/, related_index)
262 # related = html_defs[related_index..defs_index]
263 # defs = html_defs[defs_index..-1]
265 # m.reply defs.gsub(' <br/>','<li>').ircify_html
268 def wikipedia(m, params)
270 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
271 debug "Looking up things on #{site}"
273 params[:filter] = / - Wikipedia.*$/
274 params[:hits] = @bot.config['wikipedia.hits']
275 params[:firstpar] = @bot.config['wikipedia.first_par']
276 return google(m, params)
279 def unpedia(m, params)
280 site = "uncyclopedia.org"
281 debug "Looking up things on #{site}"
283 params[:filter] = / - Uncyclopedia.*$/
284 params[:hits] = @bot.config['wikipedia.hits']
285 params[:firstpar] = @bot.config['wikipedia.first_par']
286 return google(m, params)
290 where = params[:words].to_s
291 where.sub!(/^\s*in\s*/, '')
292 searchfor = CGI.escape("time in " + where)
293 url = GOOGLE_SEARCH + searchfor
296 html = @bot.httputil.get(url)
298 m.reply "Error googletiming #{where}"
303 results = html.scan(GOOGLE_TIME_RESULT)
304 debug "results: #{results.inspect}"
306 if results.length != 1
307 m.reply "Couldn't find the time for #{where} on Google"
311 time = results[0][0].ircify_html
316 plugin = SearchPlugin.new
318 plugin.map "search *words", :action => 'google', :threaded => true
319 plugin.map "google *words", :action => 'google', :threaded => true
320 plugin.map "lucky *words", :action => 'lucky', :threaded => true
321 plugin.map "gcount *words", :action => 'gcount', :threaded => true
322 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
323 plugin.map "gdef *words", :action => 'gdef', :threaded => true
324 plugin.map "gtime *words", :action => 'gtime', :threaded => true
325 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
326 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
327 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true