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 searchfor = CGI.escape what
63 # This method is also called by other methods to restrict searching to some sites
65 site = "site:#{params[:site]}+"
69 # It is also possible to choose a filter to remove constant parts from the titles
70 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
71 filter = params[:filter] || ""
73 url = GOOGLE_WAP_SEARCH + site + searchfor
75 hits = params[:hits] || @bot.config['google.hits']
76 hits = 1 if params[:lucky]
78 first_pars = params[:firstpar] || @bot.config['google.first_par']
80 single = params[:lucky] || (hits == 1 and first_pars == 1)
83 wml = @bot.httputil.get(url)
86 m.reply "error googling for #{what}"
89 results = wml.scan(GOOGLE_WAP_LINK)
90 if results.length == 0
91 m.reply "no results found for #{what}"
94 single ||= (results.length==1)
97 results = results[0...hits].map { |res|
99 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
100 u = URI.unescape(res[0] || res[1])
102 "%{n}%{b}%{t}%{b}%{sep}%{u}" % {
103 :n => (single ? "" : "#{n}. "),
104 :sep => (single ? " -- " : ": "),
105 :b => Bold, :t => t, :u => u
110 m.reply results.first
114 # If we return a single, full result, change the output to a more compact representation
116 m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
120 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
122 return unless first_pars > 0
124 Utils.get_first_pars urls, first_pars, :message => m
129 params.merge!(:lucky => true)
134 what = params[:words].to_s
135 searchfor = CGI.escape(what)
137 debug "Getting gcalc thing: #{searchfor.inspect}"
138 url = GOOGLE_SEARCH + searchfor
141 html = @bot.httputil.get(url)
143 m.reply "error googlecalcing #{what}"
147 debug "#{html.size} bytes of html recieved"
149 results = html.scan(GOOGLE_CALC_RESULT)
150 debug "results: #{results.inspect}"
152 if results.length != 1
153 m.reply "couldn't calculate #{what}"
157 result = results[0][0].ircify_html
158 debug "replying with: #{result.inspect}"
162 def gcount(m, params)
163 what = params[:words].to_s
164 searchfor = CGI.escape(what)
166 debug "Getting gcount thing: #{searchfor.inspect}"
167 url = GOOGLE_SEARCH + searchfor
170 html = @bot.httputil.get(url)
172 m.reply "error googlecounting #{what}"
176 debug "#{html.size} bytes of html recieved"
178 results = html.scan(GOOGLE_COUNT_RESULT)
179 debug "results: #{results.inspect}"
181 if results.length != 1
182 m.reply "couldn't count #{what}"
186 result = results[0][0].ircify_html
187 debug "replying with: #{result.inspect}"
188 m.reply "total results: #{result}"
193 what = params[:words].to_s
194 searchfor = CGI.escape("define " + what)
196 debug "Getting gdef thing: #{searchfor.inspect}"
197 url = GOOGLE_WAP_SEARCH + searchfor
200 html = @bot.httputil.get(url)
202 m.reply "error googledefining #{what}"
207 results = html.scan(GOOGLE_DEF_RESULT)
208 debug "results: #{results.inspect}"
210 if results.length != 1
211 m.reply "couldn't find a definition for #{what} on Google"
215 gdef_link = "http://www.google.com" + CGI.unescapeHTML(results[0][0]) # could be used to extract all defs
216 head = results[0][1].ircify_html
217 text = results[0][2].ircify_html
219 m.reply "#{head} -- #{link}\n#{text}"
221 ### gdef_link could be used for something like
222 # html_defs = @bot.httputil.get(gdef_link)
223 # related_index = html_defs.index(/Related phrases:/, 0)
224 # defs_index = html_defs.index(/Definitions of <b>/, related_index)
226 # related = html_defs[related_index..defs_index]
227 # defs = html_defs[defs_index..-1]
229 # m.reply defs.gsub(' <br/>','<li>').ircify_html
232 def wikipedia(m, params)
234 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
235 debug "Looking up things on #{site}"
237 params[:filter] = / - Wikipedia.*$/
238 params[:hits] = @bot.config['wikipedia.hits']
239 params[:firstpar] = @bot.config['wikipedia.first_par']
240 return google(m, params)
243 def unpedia(m, params)
244 site = "uncyclopedia.org"
245 debug "Looking up things on #{site}"
247 params[:filter] = / - Uncyclopedia.*$/
248 params[:hits] = @bot.config['wikipedia.hits']
249 params[:firstpar] = @bot.config['wikipedia.first_par']
250 return google(m, params)
254 where = params[:words].to_s
255 where.sub!(/^\s*in\s*/, '')
256 searchfor = CGI.escape("time in " + where)
257 url = GOOGLE_SEARCH + searchfor
260 html = @bot.httputil.get(url)
262 m.reply "Error googletiming #{where}"
267 results = html.scan(GOOGLE_TIME_RESULT)
268 debug "results: #{results.inspect}"
270 if results.length != 1
271 m.reply "Couldn't find the time for #{where} on Google"
275 time = results[0][0].ircify_html
280 plugin = SearchPlugin.new
282 plugin.map "search *words", :action => 'google', :threaded => true
283 plugin.map "google *words", :action => 'google', :threaded => true
284 plugin.map "lucky *words", :action => 'lucky', :threaded => true
285 plugin.map "gcount *words", :action => 'gcount', :threaded => true
286 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
287 plugin.map "gdef *words", :action => 'gdef', :threaded => true
288 plugin.map "gtime *words", :action => 'gtime', :threaded => true
289 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
290 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
291 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true