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="")?></td><td> </td><td nowrap(?: dir=ltr)?>(?:<h2 class=r>)?<font size=\+1><b>(.+)</b>(?:</h2>)?</td>}
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}
25 class SearchPlugin < Plugin
26 Config.register Config::IntegerValue.new('google.hits',
28 :desc => "Number of hits to return from Google searches")
29 Config.register Config::IntegerValue.new('google.first_par',
31 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
32 Config.register Config::IntegerValue.new('wikipedia.hits',
34 :desc => "Number of hits to return from Wikipedia searches")
35 Config.register Config::IntegerValue.new('wikipedia.first_par',
37 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
39 def help(plugin, topic="")
41 when "search", "google"
42 "#{topic} <string> => search google for <string>"
44 "gcalc <equation> => use the google calculator to find the answer to <equation>"
46 "gdef <term(s)> => use the google define mechanism to find a definition of <term(s)>"
48 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
50 "unpedia <string> => search for <string> on Uncyclopedia"
52 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
57 what = params[:words].to_s
58 searchfor = CGI.escape what
59 # This method is also called by other methods to restrict searching to some sites
61 site = "site:#{params[:site]}+"
65 # It is also possible to choose a filter to remove constant parts from the titles
66 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
67 filter = params[:filter] || ""
69 url = GOOGLE_WAP_SEARCH + site + searchfor
71 hits = params[:hits] || @bot.config['google.hits']
73 first_pars = params[:firstpar] || @bot.config['google.first_par']
75 single = (hits == 1 and first_pars == 1)
78 wml = @bot.httputil.get(url)
81 m.reply "error googling for #{what}"
84 results = wml.scan(GOOGLE_WAP_LINK)
85 if results.length == 0
86 m.reply "no results found for #{what}"
89 single ||= (results.length==1)
91 results = results[0...hits].map { |res|
93 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
94 u = URI.unescape res[1]
96 single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
99 # If we return a single, full result, change the output to a more compact representation
101 m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
105 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
107 return unless first_pars > 0
109 Utils.get_first_pars urls, first_pars, :message => m
114 what = params[:words].to_s
115 searchfor = CGI.escape(what)
117 debug "Getting gcalc thing: #{searchfor.inspect}"
118 url = GOOGLE_SEARCH + searchfor
121 html = @bot.httputil.get(url)
123 m.reply "error googlecalcing #{what}"
127 debug "#{html.size} bytes of html recieved"
129 results = html.scan(GOOGLE_CALC_RESULT)
130 debug "results: #{results.inspect}"
132 if results.length != 1
133 m.reply "couldn't calculate #{what}"
137 result = results[0][0].ircify_html
138 debug "replying with: #{result.inspect}"
142 def gcount(m, params)
143 what = params[:words].to_s
144 searchfor = CGI.escape(what)
146 debug "Getting gcount thing: #{searchfor.inspect}"
147 url = GOOGLE_SEARCH + searchfor
150 html = @bot.httputil.get(url)
152 m.reply "error googlecounting #{what}"
156 debug "#{html.size} bytes of html recieved"
158 results = html.scan(GOOGLE_COUNT_RESULT)
159 debug "results: #{results.inspect}"
161 if results.length != 1
162 m.reply "couldn't count #{what}"
166 result = results[0][0].ircify_html
167 debug "replying with: #{result.inspect}"
168 m.reply "total results: #{result}"
173 what = params[:words].to_s
174 searchfor = CGI.escape("define " + what)
176 debug "Getting gdef thing: #{searchfor.inspect}"
177 url = GOOGLE_WAP_SEARCH + searchfor
180 html = @bot.httputil.get(url)
182 m.reply "error googledefining #{what}"
187 results = html.scan(GOOGLE_DEF_RESULT)
188 debug "results: #{results.inspect}"
190 if results.length != 1
191 m.reply "couldn't find a definition for #{what} on Google"
195 head = results[0][0].ircify_html
196 text = results[0][1].ircify_html
198 m.reply "#{head} -- #{link}\n#{text}"
201 def wikipedia(m, params)
203 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
204 debug "Looking up things on #{site}"
206 params[:filter] = / - Wikipedia.*$/
207 params[:hits] = @bot.config['wikipedia.hits']
208 params[:firstpar] = @bot.config['wikipedia.first_par']
209 return google(m, params)
212 def unpedia(m, params)
213 site = "uncyclopedia.org"
214 debug "Looking up things on #{site}"
216 params[:filter] = / - Uncyclopedia.*$/
217 params[:hits] = @bot.config['wikipedia.hits']
218 params[:firstpar] = @bot.config['wikipedia.first_par']
219 return google(m, params)
223 plugin = SearchPlugin.new
225 plugin.map "search *words", :action => 'google', :threaded => true
226 plugin.map "google *words", :action => 'google', :threaded => true
227 plugin.map "gcount *words", :action => 'gcount', :threaded => true
228 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
229 plugin.map "gdef *words", :action => 'gdef', :threaded => true
230 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
231 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
232 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true