]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
plugins: remove excess requires and Net::HTTP.version setups
[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 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>&nbsp;</td><td nowrap><font size=\+1><b>(.+)</b></td></tr><tr><td>}
18
19 class SearchPlugin < Plugin
20   BotConfig.register BotConfigIntegerValue.new('google.hits',
21     :default => 3,
22     :desc => "Number of hits to return from Google searches")
23   BotConfig.register BotConfigIntegerValue.new('google.first_par',
24     :default => 0,
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',
27     :default => 3,
28     :desc => "Number of hits to return from Wikipedia searches")
29   BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
30     :default => 1,
31     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
32
33   def help(plugin, topic="")
34     case topic
35     when "search", "google"
36       "#{topic} <string> => search google for <string>"
37     when "gcalc"
38       "gcalc <equation> => use the google calculator to find the answer to <equation>"
39     when "wp"
40       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
41     else
42       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
43     end
44   end
45
46   def google(m, params)
47     what = params[:words].to_s
48     searchfor = URI.escape what
49     # This method is also called by other methods to restrict searching to some sites
50     if params[:site]
51       site = "site:#{params[:site]}+"
52     else
53       site = ""
54     end
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] || ""
58
59     url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
60
61     hits = params[:hits] || @bot.config['google.hits']
62
63     begin
64       wml = @bot.httputil.get(url)
65       raise unless wml
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 gcalc(m, params)
95     what = params[:words].to_s
96     searchfor = URI.escape(what).sub('+','%2B')
97     
98     debug "Getting gcalc thing: #{searchfor.inspect}"
99     url = "http://www.google.com/search?q=#{searchfor}"
100
101     begin
102       html = @bot.httputil.get(url)
103     rescue => e
104       m.reply "error googlecalcing #{what}"
105       return
106     end
107
108     debug "#{html.size} bytes of html recieved"
109     
110     results = html.scan(GOOGLE_CALC_RESULT)
111     debug "results: #{results.inspect}"
112     
113     if results.length != 1
114       m.reply "couldn't calculate #{what}"
115       return
116     end
117     
118     result = results[0][0].ircify_html
119     debug "replying with: #{result.inspect}"
120     m.reply "#{result}"
121   end
122
123   def wikipedia(m, params)
124     lang = params[:lang]
125     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
126     debug "Looking up things on #{site}"
127     params[:site] = 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)
132   end
133 end
134
135 plugin = SearchPlugin.new
136
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'
142