]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
search: Added a time command that gets the time from Google. !time london
[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 # TODO:: support localized uncyclopedias -- not easy because they have different names
16 #        for most languages
17
18 GOOGLE_SEARCH = "http://www.google.com/search?oe=UTF-8&q="
19 GOOGLE_WAP_SEARCH = "http://www.google.com/wml/search?hl=en&q="
20 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
21 GOOGLE_CALC_RESULT = %r{<img src=/images/calc_img\.gif(?: width=40 height=30 alt="")?>.*?<h2 class=r[^>]*><b>(.+?)</b>}
22 GOOGLE_COUNT_RESULT = %r{<font size=-1>Results <b>1<\/b> - <b>10<\/b> of about <b>(.*)<\/b> for}
23 GOOGLE_DEF_RESULT = %r{<p> (Web definitions for .*?)<br/>(.*?)<br/>(.*?)\s-\s+<a href}
24 GOOGLE_TIME_RESULT = %r{alt="Clock"></td><td valign=[^>]+>(.+?)<(br|/td)>}
25
26 class SearchPlugin < Plugin
27   Config.register Config::IntegerValue.new('google.hits',
28     :default => 3,
29     :desc => "Number of hits to return from Google searches")
30   Config.register Config::IntegerValue.new('google.first_par',
31     :default => 0,
32     :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
33   Config.register Config::IntegerValue.new('wikipedia.hits',
34     :default => 3,
35     :desc => "Number of hits to return from Wikipedia searches")
36   Config.register Config::IntegerValue.new('wikipedia.first_par',
37     :default => 1,
38     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
39
40   def help(plugin, topic="")
41     case topic
42     when "search", "google"
43       "#{topic} <string> => search google for <string>"
44     when "gcalc"
45       "gcalc <equation> => use the google calculator to find the answer to <equation>"
46     when "gdef"
47       "gdef <term(s)> => use the google define mechanism to find a definition of <term(s)>"
48     when "wp"
49       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
50     when "unpedia"
51       "unpedia <string> => search for <string> on Uncyclopedia"
52     else
53       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
54     end
55   end
56
57   def google(m, params)
58     what = params[:words].to_s
59     searchfor = CGI.escape what
60     # This method is also called by other methods to restrict searching to some sites
61     if params[:site]
62       site = "site:#{params[:site]}+"
63     else
64       site = ""
65     end
66     # It is also possible to choose a filter to remove constant parts from the titles
67     # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
68     filter = params[:filter] || ""
69
70     url = GOOGLE_WAP_SEARCH + site + searchfor
71
72     hits = params[:hits] || @bot.config['google.hits']
73
74     first_pars = params[:firstpar] || @bot.config['google.first_par']
75
76     single = (hits == 1 and first_pars == 1)
77
78     begin
79       wml = @bot.httputil.get(url)
80       raise unless wml
81     rescue => e
82       m.reply "error googling for #{what}"
83       return
84     end
85     results = wml.scan(GOOGLE_WAP_LINK)
86     if results.length == 0
87       m.reply "no results found for #{what}"
88       return
89     end
90     single ||= (results.length==1)
91     urls = Array.new
92     results = results[0...hits].map { |res|
93       n = res[0]
94       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
95       u = URI.unescape res[1]
96       urls.push(u)
97       single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
98     }.join(" | ")
99
100     if params[:lucky]
101       m.reply urls.first
102       return
103     end
104
105     # If we return a single, full result, change the output to a more compact representation
106     if single
107       m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
108       return
109     end
110
111     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
112
113     return unless first_pars > 0
114
115     Utils.get_first_pars urls, first_pars, :message => m
116
117   end
118
119   def lucky(m, params)
120     params.merge!(:lucky => true)
121     google(m, params)
122   end
123
124   def gcalc(m, params)
125     what = params[:words].to_s
126     searchfor = CGI.escape(what)
127
128     debug "Getting gcalc thing: #{searchfor.inspect}"
129     url = GOOGLE_SEARCH + searchfor
130
131     begin
132       html = @bot.httputil.get(url)
133     rescue => e
134       m.reply "error googlecalcing #{what}"
135       return
136     end
137
138     debug "#{html.size} bytes of html recieved"
139
140     results = html.scan(GOOGLE_CALC_RESULT)
141     debug "results: #{results.inspect}"
142
143     if results.length != 1
144       m.reply "couldn't calculate #{what}"
145       return
146     end
147
148     result = results[0][0].ircify_html
149     debug "replying with: #{result.inspect}"
150     m.reply "#{result}"
151   end
152
153   def gcount(m, params)
154     what = params[:words].to_s
155     searchfor = CGI.escape(what)
156
157     debug "Getting gcount thing: #{searchfor.inspect}"
158     url = GOOGLE_SEARCH + searchfor
159
160     begin
161       html = @bot.httputil.get(url)
162     rescue => e
163       m.reply "error googlecounting #{what}"
164       return
165     end
166
167     debug "#{html.size} bytes of html recieved"
168
169     results = html.scan(GOOGLE_COUNT_RESULT)
170     debug "results: #{results.inspect}"
171
172     if results.length != 1
173       m.reply "couldn't count #{what}"
174       return
175     end
176
177     result = results[0][0].ircify_html
178     debug "replying with: #{result.inspect}"
179     m.reply "total results: #{result}"
180
181   end
182
183   def gdef(m, params)
184     what = params[:words].to_s
185     searchfor = CGI.escape("define " + what)
186
187     debug "Getting gdef thing: #{searchfor.inspect}"
188     url = GOOGLE_WAP_SEARCH + searchfor
189
190     begin
191       html = @bot.httputil.get(url)
192     rescue => e
193       m.reply "error googledefining #{what}"
194       return
195     end
196
197     debug html
198     results = html.scan(GOOGLE_DEF_RESULT)
199     debug "results: #{results.inspect}"
200
201     if results.length != 1
202       m.reply "couldn't find a definition for #{what} on Google"
203       return
204     end
205
206     head = results[0][0].ircify_html
207     text = results[0][1].ircify_html
208     link = results[0][2]
209     m.reply "#{head} -- #{link}\n#{text}"
210   end
211
212   def wikipedia(m, params)
213     lang = params[:lang]
214     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
215     debug "Looking up things on #{site}"
216     params[:site] = site
217     params[:filter] = / - Wikipedia.*$/
218     params[:hits] = @bot.config['wikipedia.hits']
219     params[:firstpar] = @bot.config['wikipedia.first_par']
220     return google(m, params)
221   end
222
223   def unpedia(m, params)
224     site = "uncyclopedia.org"
225     debug "Looking up things on #{site}"
226     params[:site] = site
227     params[:filter] = / - Uncyclopedia.*$/
228     params[:hits] = @bot.config['wikipedia.hits']
229     params[:firstpar] = @bot.config['wikipedia.first_par']
230     return google(m, params)
231   end
232
233   def time(m, params)
234     where = params[:words].to_s
235     where.sub!(/^\s*in\s*/, '')
236     searchfor = CGI.escape("time in " + where)
237     url = GOOGLE_SEARCH + searchfor
238
239     begin
240       html = @bot.httputil.get(url)
241     rescue => e
242       m.reply "Error googletiming #{where}"
243       return
244     end
245
246     debug html
247     results = html.scan(GOOGLE_TIME_RESULT)
248     debug "results: #{results.inspect}"
249
250     if results.length != 1
251       m.reply "Couldn't find the time for #{where} on Google"
252       return
253     end
254
255     time = results[0][0].ircify_html
256     m.reply "#{time}"
257   end
258 end
259
260 plugin = SearchPlugin.new
261
262 plugin.map "search *words", :action => 'google', :threaded => true
263 plugin.map "google *words", :action => 'google', :threaded => true
264 plugin.map "lucky *words", :action => 'lucky', :threaded => true
265 plugin.map "gcount *words", :action => 'gcount', :threaded => true
266 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
267 plugin.map "gdef *words", :action => 'gdef', :threaded => true
268 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
269 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
270 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true
271 plugin.map "time *words", :action => 'time', :threaded => true
272