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
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></td><td> </td><td nowrap><font size=\+1><b>(.+)</b></td></tr><tr><td>}
23 class SearchPlugin < Plugin
24 BotConfig.register BotConfigIntegerValue.new('google.hits',
26 :desc => "Number of hits to return from Google searches")
27 BotConfig.register BotConfigIntegerValue.new('google.first_par',
29 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
30 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
32 :desc => "Number of hits to return from Wikipedia searches")
33 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
35 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
37 def help(plugin, topic="")
39 when "search", "google"
40 "#{topic} <string> => search google for <string>"
42 "gcalc <equation> => use the google calculator to find the answer to <equation>"
44 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
46 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
51 what = params[:words].to_s
52 searchfor = URI.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']
68 wml = @bot.httputil.get(url)
71 m.reply "error googling for #{what}"
74 results = wml.scan(GOOGLE_WAP_LINK)
75 if results.length == 0
76 m.reply "no results found for #{what}"
80 results = results[0...hits].map { |res|
82 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
83 u = URI.unescape res[1]
85 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
88 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
90 first_pars = params[:firstpar] || @bot.config['google.first_par']
92 return unless first_pars > 0
94 Utils.get_first_pars urls, first_pars, :message => m
99 what = params[:words].to_s
100 searchfor = URI.escape(what).sub('+','%2B')
102 debug "Getting gcalc thing: #{searchfor.inspect}"
103 url = "http://www.google.com/search?q=#{searchfor}"
106 html = @bot.httputil.get(url)
108 m.reply "error googlecalcing #{what}"
112 debug "#{html.size} bytes of html recieved"
114 results = html.scan(GOOGLE_CALC_RESULT)
115 debug "results: #{results.inspect}"
117 if results.length != 1
118 m.reply "couldn't calculate #{what}"
122 result = results[0][0].ircify_html
123 debug "replying with: #{result.inspect}"
127 def wikipedia(m, params)
129 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
130 debug "Looking up things on #{site}"
132 params[:filter] = / - Wikipedia.*$/
133 params[:hits] = @bot.config['wikipedia.hits']
134 params[:firstpar] = @bot.config['wikipedia.first_par']
135 return google(m, params)
139 plugin = SearchPlugin.new
141 plugin.map "search *words", :action => 'google'
142 plugin.map "google *words", :action => 'google'
143 plugin.map "gcalc *words", :action => 'gcalc'
144 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
145 plugin.map "wp *words", :action => 'wikipedia'