]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
search plugin: gcalc command thanks to epitron
[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
16 require 'uri'
17
18 Net::HTTP.version_1_2
19
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></td><td>&nbsp;</td><td nowrap><font size=\+1><b>(.+)</b></td></tr><tr><td>}
22
23 class SearchPlugin < Plugin
24   BotConfig.register BotConfigIntegerValue.new('google.hits',
25     :default => 3,
26     :desc => "Number of hits to return from Google searches")
27   BotConfig.register BotConfigIntegerValue.new('google.first_par',
28     :default => 0,
29     :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
30   BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
31     :default => 3,
32     :desc => "Number of hits to return from Wikipedia searches")
33   BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
34     :default => 1,
35     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
36
37   def help(plugin, topic="")
38     case topic
39     when "search", "google"
40       "#{topic} <string> => search google for <string>"
41     when "gcalc"
42       "gcalc <equation> => use the google calculator to find the answer to <equation>"
43     when "wp"
44       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
45     else
46       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
47     end
48   end
49
50   def google(m, params)
51     what = params[:words].to_s
52     searchfor = URI.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     begin
68       wml = @bot.httputil.get(url)
69       raise unless wml
70     rescue => e
71       m.reply "error googling for #{what}"
72       return
73     end
74     results = wml.scan(GOOGLE_WAP_LINK)
75     if results.length == 0
76       m.reply "no results found for #{what}"
77       return
78     end
79     urls = Array.new
80     results = results[0...hits].map { |res|
81       n = res[0]
82       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
83       u = URI.unescape res[1]
84       urls.push(u)
85       "#{n}. #{Bold}#{t}#{Bold}: #{u}"
86     }.join(" | ")
87
88     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
89
90     first_pars = params[:firstpar] || @bot.config['google.first_par']
91
92     return unless first_pars > 0
93
94     Utils.get_first_pars urls, first_pars, :message => m
95
96   end
97
98   def gcalc(m, params)
99     what = params[:words].to_s
100     searchfor = URI.escape(what).sub('+','%2B')
101     
102     debug "Getting gcalc thing: #{searchfor.inspect}"
103     url = "http://www.google.com/search?q=#{searchfor}"
104
105     begin
106       html = @bot.httputil.get(url)
107     rescue => e
108       m.reply "error googlecalcing #{what}"
109       return
110     end
111
112     debug "#{html.size} bytes of html recieved"
113     
114     results = html.scan(GOOGLE_CALC_RESULT)
115     debug "results: #{results.inspect}"
116     
117     if results.length != 1
118       m.reply "couldn't calculate #{what}"
119       return
120     end
121     
122     result = results[0][0].ircify_html
123     debug "replying with: #{result.inspect}"
124     m.reply "#{result}"
125   end
126
127   def wikipedia(m, params)
128     lang = params[:lang]
129     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
130     debug "Looking up things on #{site}"
131     params[:site] = site
132     params[:filter] = / - Wikipedia.*$/
133     params[:hits] = @bot.config['wikipedia.hits']
134     params[:firstpar] = @bot.config['wikipedia.first_par']
135     return google(m, params)
136   end
137 end
138
139 plugin = SearchPlugin.new
140
141 plugin.map "search *words", :action => 'google'
142 plugin.map "google *words", :action => 'google'
143 plugin.map "gcalc *words", :action => 'gcalc'
144 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
145 plugin.map "wp *words", :action => 'wikipedia'
146