5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
7 class SearchPlugin < Plugin
8 BotConfig.register BotConfigIntegerValue.new('google.hits',
10 :desc => "Number of hits to return from Google searches")
11 BotConfig.register BotConfigIntegerValue.new('google.first_par',
13 :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
14 BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
16 :desc => "Number of hits to return from Wikipedia searches")
17 BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
19 :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
21 def help(plugin, topic="")
23 when "search", "google"
24 "#{topic} <string> => search google for <string>"
26 "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
28 "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
33 what = params[:words].to_s
34 searchfor = URI.escape what
35 # This method is also called by other methods to restrict searching to some sites
37 site = "site:#{params[:site]}+"
41 # It is also possible to choose a filter to remove constant parts from the titles
42 # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
43 filter = params[:filter] || ""
45 url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
47 hits = params[:hits] || @bot.config['google.hits']
50 wml = @bot.httputil.get_cached(url)
52 m.reply "error googling for #{what}"
55 results = wml.scan(GOOGLE_WAP_LINK)
56 if results.length == 0
57 m.reply "no results found for #{what}"
61 results = results[0...hits].map { |res|
63 t = Utils.decode_html_entities res[2].gsub(filter, '').strip
64 u = URI.unescape res[1]
66 "#{n}. #{Bold}#{t}#{Bold}: #{u}"
69 m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
71 first_pars = params[:firstpar] || @bot.config['google.first_par']
74 while first_pars > 0 and urls.length > 0
75 url.replace(urls.shift)
78 # FIXME what happens if some big file is returned? We should share
79 # code with the url plugin to only retrieve partial file content!
80 xml = @bot.httputil.get_cached(url)
82 debug "Unable to retrieve #{url}"
85 # We get the first par after the first main heading, if possible
86 header_found = xml.match(/<h1(?:\s+[^>]*)?>(.*?)<\/h1>/im)
89 debug "Found header: #{header_found[1].inspect}"
92 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
93 break unless candidate
94 txt.replace candidate.ircify_html
97 # If we haven't found a first par yet, try to get it from the whole
102 candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
103 break unless candidate
104 txt.replace candidate.ircify_html
108 # Nothing yet, try title
110 debug "No first par found\n#{xml}"
111 # FIXME only do this if the 'url' plugin is loaded
112 txt.replace @bot.plugins['url'].get_title_from_html(xml)
115 m.reply "[#{idx}] #{txt}", :overlong => :truncate
120 def wikipedia(m, params)
122 site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
123 debug "Looking up things on #{site}"
125 params[:filter] = / - Wikipedia.*$/
126 params[:hits] = @bot.config['wikipedia.hits']
127 params[:firstpar] = @bot.config['wikipedia.first_par']
128 return google(m, params)
132 plugin = SearchPlugin.new
134 plugin.map "search *words", :action => 'google'
135 plugin.map "google *words", :action => 'google'
136 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
137 plugin.map "wp *words", :action => 'wikipedia'