5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
7 class SearchPlugin < Plugin
8 def help(plugin, topic="")
11 "search <string> => search google for <string>"
13 "google <string> => search google for <string>"
15 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
17 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
22 what = params[:words].to_s
23 searchfor = URI.escape what
24 # This method is also called by other methods to restrict searching to some sites
26 site = "site:#{params[:site]}+"
30 # It is also possible to choose a filter to remove constant parts from the titles
31 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
32 filter = params[:filter] || ""
34 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
38 wml = @bot.httputil.get(url)
40 m.reply "error googling for #{what}"
43 results = wml.scan(GOOGLE_WAP_LINK)
44 if results.length == 0
45 m.reply "no results found for #{what}"
48 results = results[0...3].map { |res|
50 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
51 u = URI.unescape res[1]
52 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
55 m.reply "Results for #{what}: #{results}"
58 def wikipedia(m, params)
60 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
62 params[:filter] = / - Wikipedia.*$/
63 return google(m, params)
67 plugin = SearchPlugin.new
69 plugin.map "search *words", :action => 'google'
70 plugin.map "google *words", :action => 'google'
71 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
72 plugin.map "wp *words", :action => 'wikipedia'