]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
search: gtime instead of time
[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 "gtime"
49       "gtime <location> => use the google clock to find the current time at <location>"
50     when "wp"
51       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
52     when "unpedia"
53       "unpedia <string> => search for <string> on Uncyclopedia"
54     else
55       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | unpedia <string> => search for <string> on Uncyclopedia"
56     end
57   end
58
59   def google(m, params)
60     what = params[:words].to_s
61     searchfor = CGI.escape what
62     # This method is also called by other methods to restrict searching to some sites
63     if params[:site]
64       site = "site:#{params[:site]}+"
65     else
66       site = ""
67     end
68     # It is also possible to choose a filter to remove constant parts from the titles
69     # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
70     filter = params[:filter] || ""
71
72     url = GOOGLE_WAP_SEARCH + site + searchfor
73
74     hits = params[:hits] || @bot.config['google.hits']
75
76     first_pars = params[:firstpar] || @bot.config['google.first_par']
77
78     single = (hits == 1 and first_pars == 1)
79
80     begin
81       wml = @bot.httputil.get(url)
82       raise unless wml
83     rescue => e
84       m.reply "error googling for #{what}"
85       return
86     end
87     results = wml.scan(GOOGLE_WAP_LINK)
88     if results.length == 0
89       m.reply "no results found for #{what}"
90       return
91     end
92     single ||= (results.length==1)
93     urls = Array.new
94     results = results[0...hits].map { |res|
95       n = res[0]
96       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
97       u = URI.unescape res[1]
98       urls.push(u)
99       single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
100     }.join(" | ")
101
102     if params[:lucky]
103       m.reply urls.first
104       return
105     end
106
107     # If we return a single, full result, change the output to a more compact representation
108     if single
109       m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
110       return
111     end
112
113     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
114
115     return unless first_pars > 0
116
117     Utils.get_first_pars urls, first_pars, :message => m
118
119   end
120
121   def lucky(m, params)
122     params.merge!(:lucky => true)
123     google(m, params)
124   end
125
126   def gcalc(m, params)
127     what = params[:words].to_s
128     searchfor = CGI.escape(what)
129
130     debug "Getting gcalc thing: #{searchfor.inspect}"
131     url = GOOGLE_SEARCH + searchfor
132
133     begin
134       html = @bot.httputil.get(url)
135     rescue => e
136       m.reply "error googlecalcing #{what}"
137       return
138     end
139
140     debug "#{html.size} bytes of html recieved"
141
142     results = html.scan(GOOGLE_CALC_RESULT)
143     debug "results: #{results.inspect}"
144
145     if results.length != 1
146       m.reply "couldn't calculate #{what}"
147       return
148     end
149
150     result = results[0][0].ircify_html
151     debug "replying with: #{result.inspect}"
152     m.reply "#{result}"
153   end
154
155   def gcount(m, params)
156     what = params[:words].to_s
157     searchfor = CGI.escape(what)
158
159     debug "Getting gcount thing: #{searchfor.inspect}"
160     url = GOOGLE_SEARCH + searchfor
161
162     begin
163       html = @bot.httputil.get(url)
164     rescue => e
165       m.reply "error googlecounting #{what}"
166       return
167     end
168
169     debug "#{html.size} bytes of html recieved"
170
171     results = html.scan(GOOGLE_COUNT_RESULT)
172     debug "results: #{results.inspect}"
173
174     if results.length != 1
175       m.reply "couldn't count #{what}"
176       return
177     end
178
179     result = results[0][0].ircify_html
180     debug "replying with: #{result.inspect}"
181     m.reply "total results: #{result}"
182
183   end
184
185   def gdef(m, params)
186     what = params[:words].to_s
187     searchfor = CGI.escape("define " + what)
188
189     debug "Getting gdef thing: #{searchfor.inspect}"
190     url = GOOGLE_WAP_SEARCH + searchfor
191
192     begin
193       html = @bot.httputil.get(url)
194     rescue => e
195       m.reply "error googledefining #{what}"
196       return
197     end
198
199     debug html
200     results = html.scan(GOOGLE_DEF_RESULT)
201     debug "results: #{results.inspect}"
202
203     if results.length != 1
204       m.reply "couldn't find a definition for #{what} on Google"
205       return
206     end
207
208     head = results[0][0].ircify_html
209     text = results[0][1].ircify_html
210     link = results[0][2]
211     m.reply "#{head} -- #{link}\n#{text}"
212   end
213
214   def wikipedia(m, params)
215     lang = params[:lang]
216     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
217     debug "Looking up things on #{site}"
218     params[:site] = site
219     params[:filter] = / - Wikipedia.*$/
220     params[:hits] = @bot.config['wikipedia.hits']
221     params[:firstpar] = @bot.config['wikipedia.first_par']
222     return google(m, params)
223   end
224
225   def unpedia(m, params)
226     site = "uncyclopedia.org"
227     debug "Looking up things on #{site}"
228     params[:site] = site
229     params[:filter] = / - Uncyclopedia.*$/
230     params[:hits] = @bot.config['wikipedia.hits']
231     params[:firstpar] = @bot.config['wikipedia.first_par']
232     return google(m, params)
233   end
234
235   def gtime(m, params)
236     where = params[:words].to_s
237     where.sub!(/^\s*in\s*/, '')
238     searchfor = CGI.escape("time in " + where)
239     url = GOOGLE_SEARCH + searchfor
240
241     begin
242       html = @bot.httputil.get(url)
243     rescue => e
244       m.reply "Error googletiming #{where}"
245       return
246     end
247
248     debug html
249     results = html.scan(GOOGLE_TIME_RESULT)
250     debug "results: #{results.inspect}"
251
252     if results.length != 1
253       m.reply "Couldn't find the time for #{where} on Google"
254       return
255     end
256
257     time = results[0][0].ircify_html
258     m.reply "#{time}"
259   end
260 end
261
262 plugin = SearchPlugin.new
263
264 plugin.map "search *words", :action => 'google', :threaded => true
265 plugin.map "google *words", :action => 'google', :threaded => true
266 plugin.map "lucky *words", :action => 'lucky', :threaded => true
267 plugin.map "gcount *words", :action => 'gcount', :threaded => true
268 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
269 plugin.map "gdef *words", :action => 'gdef', :threaded => true
270 plugin.map "gtime *words", :action => 'gtime', :threaded => true
271 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
272 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
273 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true
274