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/wml/search?hl=en&q="
20 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
21 GOOGLE_CALC_RESULT = %r{<img src=/images/calc_img\.gif(?: width=40 height=30 alt="")?>.*?<h2 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{<p> (Web definitions for .*?)<br/>(.*?)<br/>(.*?)\s-\s+<a href}
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 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
51 "unpedia <string> => search for <string> on Uncyclopedia"
53 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
58 what = params[:words].to_s
59 searchfor = CGI.escape what
60 # This method is also called by other methods to restrict searching to some sites
62 site = "site:#{params[:site]}+"
66 # It is also possible to choose a filter to remove constant parts from the titles
67 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
68 filter = params[:filter] || ""
70 url = GOOGLE_WAP_SEARCH + site + searchfor
72 hits = params[:hits] || @bot.config['google.hits']
74 first_pars = params[:firstpar] || @bot.config['google.first_par']
76 single = (hits == 1 and first_pars == 1)
79 wml = @bot.httputil.get(url)
82 m.reply "error googling for #{what}"
85 results = wml.scan(GOOGLE_WAP_LINK)
86 if results.length == 0
87 m.reply "no results found for #{what}"
90 single ||= (results.length==1)
92 results = results[0...hits].map { |res|
94 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
95 u = URI.unescape res[1]
97 single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
105 # If we return a single, full result, change the output to a more compact representation
107 m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
111 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
113 return unless first_pars > 0
115 Utils.get_first_pars urls, first_pars, :message => m
120 params.merge!(:lucky => true)
125 what = params[:words].to_s
126 searchfor = CGI.escape(what)
128 debug "Getting gcalc thing: #{searchfor.inspect}"
129 url = GOOGLE_SEARCH + searchfor
132 html = @bot.httputil.get(url)
134 m.reply "error googlecalcing #{what}"
138 debug "#{html.size} bytes of html recieved"
140 results = html.scan(GOOGLE_CALC_RESULT)
141 debug "results: #{results.inspect}"
143 if results.length != 1
144 m.reply "couldn't calculate #{what}"
148 result = results[0][0].ircify_html
149 debug "replying with: #{result.inspect}"
153 def gcount(m, params)
154 what = params[:words].to_s
155 searchfor = CGI.escape(what)
157 debug "Getting gcount thing: #{searchfor.inspect}"
158 url = GOOGLE_SEARCH + searchfor
161 html = @bot.httputil.get(url)
163 m.reply "error googlecounting #{what}"
167 debug "#{html.size} bytes of html recieved"
169 results = html.scan(GOOGLE_COUNT_RESULT)
170 debug "results: #{results.inspect}"
172 if results.length != 1
173 m.reply "couldn't count #{what}"
177 result = results[0][0].ircify_html
178 debug "replying with: #{result.inspect}"
179 m.reply "total results: #{result}"
184 what = params[:words].to_s
185 searchfor = CGI.escape("define " + what)
187 debug "Getting gdef thing: #{searchfor.inspect}"
188 url = GOOGLE_WAP_SEARCH + searchfor
191 html = @bot.httputil.get(url)
193 m.reply "error googledefining #{what}"
198 results = html.scan(GOOGLE_DEF_RESULT)
199 debug "results: #{results.inspect}"
201 if results.length != 1
202 m.reply "couldn't find a definition for #{what} on Google"
206 head = results[0][0].ircify_html
207 text = results[0][1].ircify_html
209 m.reply "#{head} -- #{link}\n#{text}"
212 def wikipedia(m, params)
214 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
215 debug "Looking up things on #{site}"
217 params[:filter] = / - Wikipedia.*$/
218 params[:hits] = @bot.config['wikipedia.hits']
219 params[:firstpar] = @bot.config['wikipedia.first_par']
220 return google(m, params)
223 def unpedia(m, params)
224 site = "uncyclopedia.org"
225 debug "Looking up things on #{site}"
227 params[:filter] = / - Uncyclopedia.*$/
228 params[:hits] = @bot.config['wikipedia.hits']
229 params[:firstpar] = @bot.config['wikipedia.first_par']
230 return google(m, params)
234 where = params[:words].to_s
235 where.sub!(/^\s*in\s*/, '')
236 searchfor = CGI.escape("time in " + where)
237 url = GOOGLE_SEARCH + searchfor
240 html = @bot.httputil.get(url)
242 m.reply "Error googletiming #{where}"
247 results = html.scan(GOOGLE_TIME_RESULT)
248 debug "results: #{results.inspect}"
250 if results.length != 1
251 m.reply "Couldn't find the time for #{where} on Google"
255 time = results[0][0].ircify_html
260 plugin = SearchPlugin.new
262 plugin.map "search *words", :action => 'google', :threaded => true
263 plugin.map "google *words", :action => 'google', :threaded => true
264 plugin.map "lucky *words", :action => 'lucky', :threaded => true
265 plugin.map "gcount *words", :action => 'gcount', :threaded => true
266 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
267 plugin.map "gdef *words", :action => 'gdef', :threaded => true
268 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
269 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
270 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true
271 plugin.map "time *words", :action => 'time', :threaded => true