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_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
19 GOOGLE_CALC_RESULT = %r{<p><table><tr><td><img src=/images/calc_img\.gif></td><td> </td><td nowrap><font size=\+1><b>(.+)</b></td></tr><tr><td>}
21 class SearchPlugin < Plugin
22 BotConfig.register BotConfigIntegerValue.new('google.hits',
24 :desc => "Number of hits to return from Google searches")
25 BotConfig.register BotConfigIntegerValue.new('google.first_par',
27 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
28 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
30 :desc => "Number of hits to return from Wikipedia searches")
31 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
33 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
35 def help(plugin, topic="")
37 when "search", "google"
38 "#{topic} <string> => search google for <string>"
40 "gcalc <equation> => use the google calculator to find the answer to <equation>"
42 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
44 "unpedia <string> => search for <string> on Uncyclopedia"
46 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
51 what = params[:words].to_s
52 searchfor = CGI.escape what
53 # This method is also called by other methods to restrict searching to some sites
55 site = "site:#{params[:site]}+"
59 # It is also possible to choose a filter to remove constant parts from the titles
60 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
61 filter = params[:filter] || ""
63 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
65 hits = params[:hits] || @bot.config['google.hits']
67 first_pars = params[:firstpar] || @bot.config['google.first_par']
69 single = (hits == 1 and first_pars == 1)
72 wml = @bot.httputil.get(url)
75 m.reply "error googling for #{what}"
78 results = wml.scan(GOOGLE_WAP_LINK)
79 if results.length == 0
80 m.reply "no results found for #{what}"
84 results = results[0...hits].map { |res|
86 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
87 u = URI.unescape res[1]
89 single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
92 # If we return a single, full result, change the output to a more compact representation
94 m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
98 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
100 return unless first_pars > 0
102 Utils.get_first_pars urls, first_pars, :message => m
107 what = params[:words].to_s
108 searchfor = CGI.escape(what)
110 debug "Getting gcalc thing: #{searchfor.inspect}"
111 url = "http://www.google.com/search?q=#{searchfor}"
114 html = @bot.httputil.get(url)
116 m.reply "error googlecalcing #{what}"
120 debug "#{html.size} bytes of html recieved"
122 results = html.scan(GOOGLE_CALC_RESULT)
123 debug "results: #{results.inspect}"
125 if results.length != 1
126 m.reply "couldn't calculate #{what}"
130 result = results[0][0].ircify_html
131 debug "replying with: #{result.inspect}"
135 def wikipedia(m, params)
137 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
138 debug "Looking up things on #{site}"
140 params[:filter] = / - Wikipedia.*$/
141 params[:hits] = @bot.config['wikipedia.hits']
142 params[:firstpar] = @bot.config['wikipedia.first_par']
143 return google(m, params)
146 def unpedia(m, params)
147 site = "uncyclopedia.org"
148 debug "Looking up things on #{site}"
150 params[:filter] = / - Uncyclopedia.*$/
151 params[:hits] = @bot.config['wikipedia.hits']
152 params[:firstpar] = @bot.config['wikipedia.first_par']
153 return google(m, params)
157 plugin = SearchPlugin.new
159 plugin.map "search *words", :action => 'google'
160 plugin.map "google *words", :action => 'google'
161 plugin.map "gcalc *words", :action => 'gcalc'
162 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
163 plugin.map "wp *words", :action => 'wikipedia'
164 plugin.map "unpedia *words", :action => 'unpedia'