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