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
16 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
17 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>}
19 class SearchPlugin < Plugin
20 BotConfig.register BotConfigIntegerValue.new('google.hits',
22 :desc => "Number of hits to return from Google searches")
23 BotConfig.register BotConfigIntegerValue.new('google.first_par',
25 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
26 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
28 :desc => "Number of hits to return from Wikipedia searches")
29 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
31 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
33 def help(plugin, topic="")
35 when "search", "google"
36 "#{topic} <string> => search google for <string>"
38 "gcalc <equation> => use the google calculator to find the answer to <equation>"
40 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
42 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
47 what = params[:words].to_s
48 searchfor = CGI.escape what
49 # This method is also called by other methods to restrict searching to some sites
51 site = "site:#{params[:site]}+"
55 # It is also possible to choose a filter to remove constant parts from the titles
56 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
57 filter = params[:filter] || ""
59 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
61 hits = params[:hits] || @bot.config['google.hits']
64 wml = @bot.httputil.get(url)
67 m.reply "error googling for #{what}"
70 results = wml.scan(GOOGLE_WAP_LINK)
71 if results.length == 0
72 m.reply "no results found for #{what}"
76 results = results[0...hits].map { |res|
78 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
79 u = URI.unescape res[1]
81 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
84 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
86 first_pars = params[:firstpar] || @bot.config['google.first_par']
88 return unless first_pars > 0
90 Utils.get_first_pars urls, first_pars, :message => m
95 what = params[:words].to_s
96 searchfor = CGI.escape(what)
98 debug "Getting gcalc thing: #{searchfor.inspect}"
99 url = "http://www.google.com/search?q=#{searchfor}"
102 html = @bot.httputil.get(url)
104 m.reply "error googlecalcing #{what}"
108 debug "#{html.size} bytes of html recieved"
110 results = html.scan(GOOGLE_CALC_RESULT)
111 debug "results: #{results.inspect}"
113 if results.length != 1
114 m.reply "couldn't calculate #{what}"
118 result = results[0][0].ircify_html
119 debug "replying with: #{result.inspect}"
123 def wikipedia(m, params)
125 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
126 debug "Looking up things on #{site}"
128 params[:filter] = / - Wikipedia.*$/
129 params[:hits] = @bot.config['wikipedia.hits']
130 params[:firstpar] = @bot.config['wikipedia.first_par']
131 return google(m, params)
135 plugin = SearchPlugin.new
137 plugin.map "search *words", :action => 'google'
138 plugin.map "google *words", :action => 'google'
139 plugin.map "gcalc *words", :action => 'gcalc'
140 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
141 plugin.map "wp *words", :action => 'wikipedia'