]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
search plugin: add google define support
[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{<p><table><tr><td><img src=/images/calc_img\.gif(?: alt="")?></td><td>&nbsp;</td><td nowrap>(?:<h2 class=r>)?<font size=\+1><b>(.+)</b>(?:</h2>)?</td></tr><tr><td>}
22 GOOGLE_DEF_RESULT = %r{<p> (Web definitions for .*?)<br/>(.*?)<br/>(.*?)\s-\s+<a href}
23
24 class SearchPlugin < Plugin
25   BotConfig.register BotConfigIntegerValue.new('google.hits',
26     :default => 3,
27     :desc => "Number of hits to return from Google searches")
28   BotConfig.register BotConfigIntegerValue.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   BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
32     :default => 3,
33     :desc => "Number of hits to return from Wikipedia searches")
34   BotConfig.register BotConfigIntegerValue.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     urls = Array.new
89     results = results[0...hits].map { |res|
90       n = res[0]
91       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
92       u = URI.unescape res[1]
93       urls.push(u)
94       single ? u : "#{n}. #{Bold}#{t}#{Bold}: #{u}"
95     }.join(" | ")
96
97     # If we return a single, full result, change the output to a more compact representation
98     if single
99       m.reply "Result for %s: %s -- %s" % [what, results, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
100       return
101     end
102
103     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
104
105     return unless first_pars > 0
106
107     Utils.get_first_pars urls, first_pars, :message => m
108
109   end
110
111   def gcalc(m, params)
112     what = params[:words].to_s
113     searchfor = CGI.escape(what)
114     
115     debug "Getting gcalc thing: #{searchfor.inspect}"
116     url = GOOGLE_SEARCH + searchfor
117
118     begin
119       html = @bot.httputil.get(url)
120     rescue => e
121       m.reply "error googlecalcing #{what}"
122       return
123     end
124
125     debug "#{html.size} bytes of html recieved"
126     
127     results = html.scan(GOOGLE_CALC_RESULT)
128     debug "results: #{results.inspect}"
129     
130     if results.length != 1
131       m.reply "couldn't calculate #{what}"
132       return
133     end
134     
135     result = results[0][0].ircify_html
136     debug "replying with: #{result.inspect}"
137     m.reply "#{result}"
138   end
139
140   def gdef(m, params)
141     what = params[:words].to_s
142     searchfor = CGI.escape("define " + what)
143     
144     debug "Getting gdef thing: #{searchfor.inspect}"
145     url = GOOGLE_WAP_SEARCH + searchfor
146
147     begin
148       html = @bot.httputil.get(url)
149     rescue => e
150       m.reply "error googledefining #{what}"
151       return
152     end
153
154     debug html
155     results = html.scan(GOOGLE_DEF_RESULT)
156     debug "results: #{results.inspect}"
157     
158     if results.length != 1
159       m.reply "couldn't find a definition for #{what} on Google"
160       return
161     end
162     
163     head = results[0][0].ircify_html
164     text = results[0][1].ircify_html
165     link = results[0][2]
166     m.reply "#{head} -- #{link}\n#{text}"
167   end
168
169   def wikipedia(m, params)
170     lang = params[:lang]
171     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
172     debug "Looking up things on #{site}"
173     params[:site] = site
174     params[:filter] = / - Wikipedia.*$/
175     params[:hits] = @bot.config['wikipedia.hits']
176     params[:firstpar] = @bot.config['wikipedia.first_par']
177     return google(m, params)
178   end
179
180   def unpedia(m, params)
181     site = "uncyclopedia.org"
182     debug "Looking up things on #{site}"
183     params[:site] = site
184     params[:filter] = / - Uncyclopedia.*$/
185     params[:hits] = @bot.config['wikipedia.hits']
186     params[:firstpar] = @bot.config['wikipedia.first_par']
187     return google(m, params)
188   end
189 end
190
191 plugin = SearchPlugin.new
192
193 plugin.map "search *words", :action => 'google'
194 plugin.map "google *words", :action => 'google'
195 plugin.map "gcalc *words", :action => 'gcalc'
196 plugin.map "gdef *words", :action => 'gdef'
197 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
198 plugin.map "wp *words", :action => 'wikipedia'
199 plugin.map "unpedia *words", :action => 'unpedia'
200