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