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> </td><td nowrap><font size=\+1><b>(.+)</b></td></tr><tr><td>}
8 class SearchPlugin < Plugin
9 def help(plugin, topic="")
12 "search <string> => search google for <string>"
14 "google <string> => search google for <string>"
16 "gcalc <equation> => use the google calculator to find the answer to <equation>"
18 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
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>"
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
29 site = "site:#{params[:site]}+"
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] || ""
37 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
40 wml = @bot.httputil.get_cached(url)
42 m.reply "error googling for #{what}"
45 results = wml.scan(GOOGLE_WAP_LINK)
46 if results.length == 0
47 m.reply "no results found for #{what}"
50 results = results[0...3].map { |res|
52 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
53 u = URI.unescape res[1]
54 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
57 m.reply "Results for #{what}: #{results}"
61 what = params[:words].to_s
62 searchfor = URI.escape(what).sub('+','%2B')
64 puts "Getting gcalc thing: #{searchfor.inspect}"
65 url = "http://www.google.com/search?q=#{searchfor}"
68 html = @bot.httputil.get_cached(url)
70 m.reply "error googlecalcing #{what}"
74 puts "#{html.size} bytes of html recieved"
76 results = html.scan(GOOGLE_CALC_RESULT)
77 puts "results: #{results.inspect}"
79 if results.length != 1
80 m.reply "couldn't calculate #{what}"
85 result = results[0][0]
86 result.gsub!("<font size=-2> </font>", ",")
87 puts "replying with: #{result.inspect}"
91 def wikipedia(m, params)
93 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
95 params[:filter] = / - Wikipedia.*$/
96 return google(m, params)
101 plugin = SearchPlugin.new
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'