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