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 = /result">(?:<div[^>]*>)?<a href="([^"]+)"[^>]*>(.*?)<\/a>/im
21 GOOGLE_CALC_RESULT = %r{<img src=/images/calc_img\.gif(?: width=40 height=30 alt="")?>.*?<h[1-6] class=r[^>]*><b>(.+?)</b>}
22 GOOGLE_COUNT_RESULT = %r{<font size=-1>Results <b>1<\/b> - <b>10<\/b> of about <b>(.*)<\/b> for}
23 GOOGLE_DEF_RESULT = %r{onebox_result">\s*(.*?)\s*<br/>\s*(.*?)<table}
24 GOOGLE_TIME_RESULT = %r{alt="Clock"></td><td valign=[^>]+>(.+?)<(br|/td)>}
26 class SearchPlugin < Plugin
27 Config.register Config::IntegerValue.new('google.hits',
29 :desc => "Number of hits to return from Google searches")
30 Config.register Config::IntegerValue.new('google.first_par',
32 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
33 Config.register Config::IntegerValue.new('wikipedia.hits',
35 :desc => "Number of hits to return from Wikipedia searches")
36 Config.register Config::IntegerValue.new('wikipedia.first_par',
38 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
40 def help(plugin, topic="")
42 when "search", "google"
43 "#{topic} <string> => search google for <string>"
45 "gcalc <equation> => use the google calculator to find the answer to <equation>"
47 "gdef <term(s)> => use the google define mechanism to find a definition of <term(s)>"
49 "gtime <location> => use the google clock to find the current time at <location>"
51 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
53 "unpedia <string> => search for <string> on Uncyclopedia"
55 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
60 what = params[:words].to_s
61 if what.match(/^define:/)
62 return google_define(m, what, params)
65 searchfor = CGI.escape what
66 # This method is also called by other methods to restrict searching to some sites
68 site = "site:#{params[:site]}+"
72 # It is also possible to choose a filter to remove constant parts from the titles
73 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
74 filter = params[:filter] || ""
76 url = GOOGLE_WAP_SEARCH + site + searchfor
78 hits = params[:hits] || @bot.config['google.hits']
79 hits = 1 if params[:lucky]
81 first_pars = params[:firstpar] || @bot.config['google.first_par']
83 single = params[:lucky] || (hits == 1 and first_pars == 1)
86 wml = @bot.httputil.get(url)
89 m.reply "error googling for #{what}"
92 results = wml.scan(GOOGLE_WAP_LINK)
94 if results.length == 0
95 m.reply "no results found for #{what}"
99 single ||= (results.length==1)
106 results.each do |res|
107 t = res[1].ircify_html(:img => "[%{src} %{alt} %{dimensions}]").strip
109 if u.sub!(%r{^http://www.google.com/aclk\?},'')
110 u = CGI::parse(u)['adurl'].first
111 debug "skipping ad for #{u}"
113 elsif u.sub!(%r{^http://www.google.com/gwt/x\?},'')
114 u = CGI::parse(u)['u'].first
115 elsif u.sub!(%r{^/url\?},'')
116 u = CGI::parse(u)['q'].first
119 pretty.push("%{n}%{b}%{t}%{b}%{sep}%{u}" % {
120 :n => (single ? "" : "#{urls.length}. "),
121 :sep => (single ? " -- " : ": "),
122 :b => Bold, :t => t, :u => u
124 break if urls.length == hits
127 m.reply "failed to understand what google found for #{what}"
139 result_string = pretty.join(" | ")
141 # If we return a single, full result, change the output to a more compact representation
143 m.reply "Result for %s: %s -- %s" % [what, result_string, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
147 m.reply "Results for #{what}: #{result_string}", :split_at => /\s+\|\s+/
149 return unless first_pars > 0
151 Utils.get_first_pars urls, first_pars, :message => m
155 def google_define(m, what, params)
157 wml = @bot.httputil.get(GOOGLE_SEARCH + CGI.escape(what))
160 m.reply "error googling for #{what}"
165 related_index = wml.index(/Related phrases:/, 0)
166 raise unless related_index
167 defs_index = wml.index(/Definitions of <b>/, related_index)
168 raise unless defs_index
169 defs_end = wml.index(/<input/, defs_index)
170 raise unless defs_end
172 m.reply "no results found for #{what}"
176 related = wml[related_index...defs_index]
177 defs = wml[defs_index...defs_end]
179 m.reply defs.ircify_html(:a_href => Underline), :split_at => (Underline + ' ')
184 params.merge!(:lucky => true)
189 what = params[:words].to_s
190 searchfor = CGI.escape(what)
192 debug "Getting gcalc thing: #{searchfor.inspect}"
193 url = GOOGLE_WAP_SEARCH + searchfor
196 html = @bot.httputil.get(url)
198 m.reply "error googlecalcing #{what}"
202 debug "#{html.size} bytes of html recieved"
205 candidates = html.match(/font-weight:bold">(.*?)<\/(?:span|div)>/)
206 debug "candidates: #{candidates.inspect}"
209 m.reply "couldn't calculate #{what}"
212 result = candidates[1]
214 debug "replying with: #{result.inspect}"
215 m.reply result.ircify_html
218 def gcount(m, params)
219 what = params[:words].to_s
220 searchfor = CGI.escape(what)
222 debug "Getting gcount thing: #{searchfor.inspect}"
223 url = GOOGLE_SEARCH + searchfor
226 html = @bot.httputil.get(url)
228 m.reply "error googlecounting #{what}"
232 debug "#{html.size} bytes of html recieved"
234 results = html.scan(GOOGLE_COUNT_RESULT)
235 debug "results: #{results.inspect}"
237 if results.length != 1
238 m.reply "couldn't count #{what}"
242 result = results[0][0].ircify_html
243 debug "replying with: #{result.inspect}"
244 m.reply "total results: #{result}"
249 what = params[:words].to_s
250 searchfor = CGI.escape("define " + what)
252 debug "Getting gdef thing: #{searchfor.inspect}"
253 url = GOOGLE_WAP_SEARCH + searchfor
256 html = @bot.httputil.get(url)
258 m.reply "error googledefining #{what}"
263 results = html.scan(GOOGLE_DEF_RESULT)
264 debug "results: #{results.inspect}"
266 if results.length != 1
267 m.reply "couldn't find a definition for #{what} on Google"
271 head = results[0][0].ircify_html
272 text = results[0][1].ircify_html
273 m.reply "#{head} -- #{text}"
276 def wikipedia(m, params)
278 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
279 debug "Looking up things on #{site}"
281 params[:filter] = / - Wikipedia.*$/
282 params[:hits] = @bot.config['wikipedia.hits']
283 params[:firstpar] = @bot.config['wikipedia.first_par']
284 return google(m, params)
287 def unpedia(m, params)
288 site = "uncyclopedia.org"
289 debug "Looking up things on #{site}"
291 params[:filter] = / - Uncyclopedia.*$/
292 params[:hits] = @bot.config['wikipedia.hits']
293 params[:firstpar] = @bot.config['wikipedia.first_par']
294 return google(m, params)
298 where = params[:words].to_s
299 where.sub!(/^\s*in\s*/, '')
300 searchfor = CGI.escape("time in " + where)
301 url = GOOGLE_SEARCH + searchfor
304 html = @bot.httputil.get(url)
306 m.reply "Error googletiming #{where}"
311 results = html.scan(GOOGLE_TIME_RESULT)
312 debug "results: #{results.inspect}"
314 if results.length != 1
315 m.reply "Couldn't find the time for #{where} on Google"
319 time = results[0][0].ircify_html
324 plugin = SearchPlugin.new
326 plugin.map "search *words", :action => 'google', :threaded => true
327 plugin.map "google *words", :action => 'google', :threaded => true
328 plugin.map "lucky *words", :action => 'lucky', :threaded => true
329 plugin.map "gcount *words", :action => 'gcount', :threaded => true
330 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
331 plugin.map "gdef *words", :action => 'gdef', :threaded => true
332 plugin.map "gtime *words", :action => 'gtime', :threaded => true
333 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
334 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
335 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true