]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
search: add command 'lucky'
[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 params[:lucky]
100       m.reply urls.first
101       return
102     end
103
104     # If we return a single, full result, change the output to a more compact representation
105     if single
106       m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
107       return
108     end
109
110     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
111
112     return unless first_pars > 0
113
114     Utils.get_first_pars urls, first_pars, :message => m
115
116   end
117
118   def lucky(m, params)
119     params.merge!(:lucky => true)
120     google(m, params)
121   end
122
123   def gcalc(m, params)
124     what = params[:words].to_s
125     searchfor = CGI.escape(what)
126
127     debug "Getting gcalc thing: #{searchfor.inspect}"
128     url = GOOGLE_SEARCH + searchfor
129
130     begin
131       html = @bot.httputil.get(url)
132     rescue => e
133       m.reply "error googlecalcing #{what}"
134       return
135     end
136
137     debug "#{html.size} bytes of html recieved"
138
139     results = html.scan(GOOGLE_CALC_RESULT)
140     debug "results: #{results.inspect}"
141
142     if results.length != 1
143       m.reply "couldn't calculate #{what}"
144       return
145     end
146
147     result = results[0][0].ircify_html
148     debug "replying with: #{result.inspect}"
149     m.reply "#{result}"
150   end
151
152   def gcount(m, params)
153     what = params[:words].to_s
154     searchfor = CGI.escape(what)
155
156     debug "Getting gcount thing: #{searchfor.inspect}"
157     url = GOOGLE_SEARCH + searchfor
158
159     begin
160       html = @bot.httputil.get(url)
161     rescue => e
162       m.reply "error googlecounting #{what}"
163       return
164     end
165
166     debug "#{html.size} bytes of html recieved"
167
168     results = html.scan(GOOGLE_COUNT_RESULT)
169     debug "results: #{results.inspect}"
170
171     if results.length != 1
172       m.reply "couldn't count #{what}"
173       return
174     end
175
176     result = results[0][0].ircify_html
177     debug "replying with: #{result.inspect}"
178     m.reply "total results: #{result}"
179
180   end
181
182   def gdef(m, params)
183     what = params[:words].to_s
184     searchfor = CGI.escape("define " + what)
185
186     debug "Getting gdef thing: #{searchfor.inspect}"
187     url = GOOGLE_WAP_SEARCH + searchfor
188
189     begin
190       html = @bot.httputil.get(url)
191     rescue => e
192       m.reply "error googledefining #{what}"
193       return
194     end
195
196     debug html
197     results = html.scan(GOOGLE_DEF_RESULT)
198     debug "results: #{results.inspect}"
199
200     if results.length != 1
201       m.reply "couldn't find a definition for #{what} on Google"
202       return
203     end
204
205     head = results[0][0].ircify_html
206     text = results[0][1].ircify_html
207     link = results[0][2]
208     m.reply "#{head} -- #{link}\n#{text}"
209   end
210
211   def wikipedia(m, params)
212     lang = params[:lang]
213     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
214     debug "Looking up things on #{site}"
215     params[:site] = site
216     params[:filter] = / - Wikipedia.*$/
217     params[:hits] = @bot.config['wikipedia.hits']
218     params[:firstpar] = @bot.config['wikipedia.first_par']
219     return google(m, params)
220   end
221
222   def unpedia(m, params)
223     site = "uncyclopedia.org"
224     debug "Looking up things on #{site}"
225     params[:site] = site
226     params[:filter] = / - Uncyclopedia.*$/
227     params[:hits] = @bot.config['wikipedia.hits']
228     params[:firstpar] = @bot.config['wikipedia.first_par']
229     return google(m, params)
230   end
231 end
232
233 plugin = SearchPlugin.new
234
235 plugin.map "search *words", :action => 'google', :threaded => true
236 plugin.map "google *words", :action => 'google', :threaded => true
237 plugin.map "lucky *words", :action => 'lucky', :threaded => true
238 plugin.map "gcount *words", :action => 'gcount', :threaded => true
239 plugin.map "gcalc *words", :action => 'gcalc', :threaded => true
240 plugin.map "gdef *words", :action => 'gdef', :threaded => true
241 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }, :threaded => true
242 plugin.map "wp *words", :action => 'wikipedia', :threaded => true
243 plugin.map "unpedia *words", :action => 'unpedia', :threaded => true
244