]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
*** (httputil) major rework, new caching implementation, unified request
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / search.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Google and Wikipedia search plugin for rbot
5 #
6 # Author:: Tom Gilbert (giblet) <tom@linuxbrit.co.uk>
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 #
9 # Copyright:: (C) 2002-2005 Tom Gilbert
10 # Copyright:: (C) 2006 Tom Gilbert, Giuseppe Bilotta
11 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
12
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
16 require 'uri'
17
18 Net::HTTP.version_1_2
19
20 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
21
22 class SearchPlugin < Plugin
23   BotConfig.register BotConfigIntegerValue.new('google.hits',
24     :default => 3,
25     :desc => "Number of hits to return from Google searches")
26   BotConfig.register BotConfigIntegerValue.new('google.first_par',
27     :default => 0,
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',
30     :default => 3,
31     :desc => "Number of hits to return from Wikipedia searches")
32   BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
33     :default => 1,
34     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
35
36   def help(plugin, topic="")
37     case topic
38     when "search", "google"
39       "#{topic} <string> => search google for <string>"
40     when "wp"
41       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
42     else
43       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
44     end
45   end
46
47   def google(m, params)
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
51     if params[:site]
52       site = "site:#{params[:site]}+"
53     else
54       site = ""
55     end
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] || ""
59
60     url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
61
62     hits = params[:hits] || @bot.config['google.hits']
63
64     begin
65       wml = @bot.httputil.get(url)
66     rescue => e
67       m.reply "error googling for #{what}"
68       return
69     end
70     results = wml.scan(GOOGLE_WAP_LINK)
71     if results.length == 0
72       m.reply "no results found for #{what}"
73       return
74     end
75     urls = Array.new
76     results = results[0...hits].map { |res|
77       n = res[0]
78       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
79       u = URI.unescape res[1]
80       urls.push(u)
81       "#{n}. #{Bold}#{t}#{Bold}: #{u}"
82     }.join(" | ")
83
84     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
85
86     first_pars = params[:firstpar] || @bot.config['google.first_par']
87
88     return unless first_pars > 0
89
90     Utils.get_first_pars urls, first_pars, :message => m
91
92   end
93
94   def wikipedia(m, params)
95     lang = params[:lang]
96     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
97     debug "Looking up things on #{site}"
98     params[:site] = 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)
103   end
104 end
105
106 plugin = SearchPlugin.new
107
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'
112