]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb-epipatch
Some new feature(s)!
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / search.rb-epipatch
1 require 'uri'
2
3 Net::HTTP.version_1_2
4
5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
6 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>}
7
8 class SearchPlugin < Plugin
9   def help(plugin, topic="")
10     case topic
11     when "search"
12       "search <string> => search google for <string>"
13     when "google"
14       "google <string> => search google for <string>"
15     when "gcalc"
16       "gcalc <equation> => use the google calculator to find the answer to <equation>"
17     when "wp"
18       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
19     else
20       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia | gcalc <equation> => use google calculator to calculate <equation>"
21     end
22   end
23
24   def google(m, params)
25     what = params[:words].to_s
26     searchfor = URI.escape what
27     # This method is also called by other methods to restrict searching to some sites
28     if params[:site]
29       site = "site:#{params[:site]}+"
30     else
31       site = ""
32     end
33     # It is also possible to choose a filter to remove constant parts from the titles
34     # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
35     filter = params[:filter] || ""
36
37     url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
38
39     begin
40       wml = @bot.httputil.get_cached(url)
41     rescue => e
42       m.reply "error googling for #{what}"
43       return
44     end
45     results = wml.scan(GOOGLE_WAP_LINK)
46     if results.length == 0
47       m.reply "no results found for #{what}"
48       return
49     end
50     results = results[0...3].map { |res|
51       n = res[0]
52       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
53       u = URI.unescape res[1]
54       "#{n}. #{Bold}#{t}#{Bold}: #{u}"
55     }.join(" | ")
56
57     m.reply "Results for #{what}: #{results}"
58   end
59
60   def gcalc(m, params)
61     what = params[:words].to_s
62     searchfor = URI.escape(what).sub('+','%2B')
63     
64     puts "Getting gcalc thing: #{searchfor.inspect}"
65     url = "http://www.google.com/search?q=#{searchfor}"
66
67     begin
68       html = @bot.httputil.get_cached(url)
69     rescue => e
70       m.reply "error googlecalcing #{what}"
71       return
72     end
73
74     puts "#{html.size} bytes of html recieved"
75     
76     results = html.scan(GOOGLE_CALC_RESULT)
77     puts "results: #{results.inspect}"
78     
79     if results.length != 1
80       m.reply "couldn't calculate #{what}"
81       #puts html
82       return
83     end
84     
85     result = results[0][0]
86     result.gsub!("<font size=-2> </font>", ",")
87     puts "replying with: #{result.inspect}"
88     m.reply "#{result}"
89   end
90
91   def wikipedia(m, params)
92     lang = params[:lang]
93     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
94     params[:site] = site
95     params[:filter] = / - Wikipedia.*$/
96     return google(m, params)
97   end
98   
99 end
100
101 plugin = SearchPlugin.new
102
103 plugin.map "search *words", :action => 'google'
104 plugin.map "google *words", :action => 'google'
105 plugin.map "gcalc *words", :action => 'gcalc'
106 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
107 plugin.map "wp *words", :action => 'wikipedia'
108