]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
Move extensions to standard classes into a specific extends.rb util module
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / search.rb
1 require 'uri'
2
3 Net::HTTP.version_1_2
4
5 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
6
7 class SearchPlugin < Plugin
8   BotConfig.register BotConfigIntegerValue.new('google.hits',
9     :default => 3,
10     :desc => "Number of hits to return from Google searches")
11   BotConfig.register BotConfigIntegerValue.new('google.first_par',
12     :default => 0,
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',
15     :default => 3,
16     :desc => "Number of hits to return from Wikipedia searches")
17   BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
18     :default => 1,
19     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
20
21   def help(plugin, topic="")
22     case topic
23     when "search", "google"
24       "#{topic} <string> => search google for <string>"
25     when "wp"
26       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
27     else
28       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
29     end
30   end
31
32   def google(m, params)
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
36     if params[:site]
37       site = "site:#{params[:site]}+"
38     else
39       site = ""
40     end
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] || ""
44
45     url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
46
47     hits = params[:hits] || @bot.config['google.hits']
48
49     begin
50       wml = @bot.httputil.get_cached(url)
51     rescue => e
52       m.reply "error googling for #{what}"
53       return
54     end
55     results = wml.scan(GOOGLE_WAP_LINK)
56     if results.length == 0
57       m.reply "no results found for #{what}"
58       return
59     end
60     urls = Array.new
61     results = results[0...hits].map { |res|
62       n = res[0]
63       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
64       u = URI.unescape res[1]
65       urls.push(u)
66       "#{n}. #{Bold}#{t}#{Bold}: #{u}"
67     }.join(" | ")
68
69     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
70
71     first_pars = params[:firstpar] || @bot.config['google.first_par']
72
73     idx = 0
74     while first_pars > 0 and urls.length > 0
75       url.replace(urls.shift)
76       idx += 1
77
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)
81       if xml.nil?
82         debug "Unable to retrieve #{url}"
83         next
84       end
85       # We get the first par after the first main heading, if possible
86       header_found = xml.match(/<h1(?:\s+[^>]*)?>(.*?)<\/h1>/im)
87       txt = String.new
88       if header_found
89         debug "Found header: #{header_found[1].inspect}"
90         while txt.empty? 
91           header_found = $'
92           candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
93           break unless candidate
94           txt.replace candidate.ircify_html
95         end
96       end
97       # If we haven't found a first par yet, try to get it from the whole
98       # document
99       if txt.empty?
100         header_found = xml
101         while txt.empty? 
102           candidate = header_found[/<p(?:\s+[^>]*)?>.*?<\/p>/im]
103           break unless candidate
104           txt.replace candidate.ircify_html
105           header_found = $'
106         end
107       end
108       # Nothing yet, try title
109       if txt.empty?
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)
113         next if txt.empty?
114       end
115       m.reply "[#{idx}] #{txt}", :overlong => :truncate
116       first_pars -=1
117     end
118   end
119
120   def wikipedia(m, params)
121     lang = params[:lang]
122     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
123     debug "Looking up things on #{site}"
124     params[:site] = 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)
129   end
130 end
131
132 plugin = SearchPlugin.new
133
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'
138