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
22 class SearchPlugin < Plugin
23 BotConfig.register BotConfigIntegerValue.new('google.hits',
25 :desc => "Number of hits to return from Google searches")
26 BotConfig.register BotConfigIntegerValue.new('google.first_par',
28 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
29 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
31 :desc => "Number of hits to return from Wikipedia searches")
32 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
34 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
36 def help(plugin, topic="")
38 when "search", "google"
39 "#{topic} <string> => search google for <string>"
41 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
43 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
48 what = params[:words].to_s
49 searchfor = URI.escape what
50 # This method is also called by other methods to restrict searching to some sites
52 site = "site:#{params[:site]}+"
56 # It is also possible to choose a filter to remove constant parts from the titles
57 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
58 filter = params[:filter] || ""
60 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
62 hits = params[:hits] || @bot.config['google.hits']
65 wml = @bot.httputil.get_cached(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, :http_util => @bot.httputil, :message => m
94 def wikipedia(m, params)
96 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
97 debug "Looking up things on #{site}"
99 params[:filter] = / - Wikipedia.*$/
100 params[:hits] = @bot.config['wikipedia.hits']
101 params[:firstpar] = @bot.config['wikipedia.first_par']
102 return google(m, params)
106 plugin = SearchPlugin.new
108 plugin.map "search *words", :action => 'google'
109 plugin.map "google *words", :action => 'google'
110 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
111 plugin.map "wp *words", :action => 'wikipedia'