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{<p><table><tr><td><img src=/images/calc_img\.gif(?: alt="")?></td><td> </td><td nowrap>(?:<h2 class=r>)?<font size=\+1><b>(.+)</b>(?:</h2>)?</td></tr><tr><td>}
22 GOOGLE_DEF_RESULT = %r{<p> (Web definitions for .*?)<br/>(.*?)<br/>(.*?)\s-\s+<a href}
24 class SearchPlugin < Plugin
25 Config.register Config::IntegerValue.new('google.hits',
27 :desc => "Number of hits to return from Google searches")
28 Config.register Config::IntegerValue.new('google.first_par',
30 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
31 Config.register Config::IntegerValue.new('wikipedia.hits',
33 :desc => "Number of hits to return from Wikipedia searches")
34 Config.register Config::IntegerValue.new('wikipedia.first_par',
36 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
38 def help(plugin, topic="")
40 when "search", "google"
41 "#{topic} <string> => search google for <string>"
43 "gcalc <equation> => use the google calculator to find the answer to <equation>"
45 "gdef <term(s)> => use the google define mechanism to find a definition of <term(s)>"
47 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
49 "unpedia <string> => search for <string> on Uncyclopedia"
51 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
56 what = params[:words].to_s
57 searchfor = CGI.escape what
58 # This method is also called by other methods to restrict searching to some sites
60 site = "site:#{params[:site]}+"
64 # It is also possible to choose a filter to remove constant parts from the titles
65 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
66 filter = params[:filter] || ""
68 url = GOOGLE_WAP_SEARCH + site + searchfor
70 hits = params[:hits] || @bot.config['google.hits']
72 first_pars = params[:firstpar] || @bot.config['google.first_par']
74 single = (hits == 1 and first_pars == 1)
77 wml = @bot.httputil.get(url)
80 m.reply "error googling for #{what}"
83 results = wml.scan(GOOGLE_WAP_LINK)
84 if results.length == 0
85 m.reply "no results found for #{what}"
88 single ||= (results.length==1)
90 results = results[0...hits].map { |res|
92 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
93 u = URI.unescape res[1]
95 single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
98 # If we return a single, full result, change the output to a more compact representation
100 m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
104 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
106 return unless first_pars > 0
108 Utils.get_first_pars urls, first_pars, :message => m
113 what = params[:words].to_s
114 searchfor = CGI.escape(what)
116 debug "Getting gcalc thing: #{searchfor.inspect}"
117 url = GOOGLE_SEARCH + searchfor
120 html = @bot.httputil.get(url)
122 m.reply "error googlecalcing #{what}"
126 debug "#{html.size} bytes of html recieved"
128 results = html.scan(GOOGLE_CALC_RESULT)
129 debug "results: #{results.inspect}"
131 if results.length != 1
132 m.reply "couldn't calculate #{what}"
136 result = results[0][0].ircify_html
137 debug "replying with: #{result.inspect}"
142 what = params[:words].to_s
143 searchfor = CGI.escape("define " + what)
145 debug "Getting gdef thing: #{searchfor.inspect}"
146 url = GOOGLE_WAP_SEARCH + searchfor
149 html = @bot.httputil.get(url)
151 m.reply "error googledefining #{what}"
156 results = html.scan(GOOGLE_DEF_RESULT)
157 debug "results: #{results.inspect}"
159 if results.length != 1
160 m.reply "couldn't find a definition for #{what} on Google"
164 head = results[0][0].ircify_html
165 text = results[0][1].ircify_html
167 m.reply "#{head} -- #{link}\n#{text}"
170 def wikipedia(m, params)
172 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
173 debug "Looking up things on #{site}"
175 params[:filter] = / - Wikipedia.*$/
176 params[:hits] = @bot.config['wikipedia.hits']
177 params[:firstpar] = @bot.config['wikipedia.first_par']
178 return google(m, params)
181 def unpedia(m, params)
182 site = "uncyclopedia.org"
183 debug "Looking up things on #{site}"
185 params[:filter] = / - Uncyclopedia.*$/
186 params[:hits] = @bot.config['wikipedia.hits']
187 params[:firstpar] = @bot.config['wikipedia.first_par']
188 return google(m, params)
192 plugin = SearchPlugin.new
194 plugin.map "search *words", :action => 'google'
195 plugin.map "google *words", :action => 'google'
196 plugin.map "gcalc *words", :action => 'gcalc'
197 plugin.map "gdef *words", :action => 'gdef'
198 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
199 plugin.map "wp *words", :action => 'wikipedia'
200 plugin.map "unpedia *words", :action => 'unpedia'