]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/search.rb
Move code to find and ircify first par from search plugin to utils
[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       par = Utils.ircify_first_html_par(xml)
86       if par.empty?
87         debug "No first par found\n#{xml}"
88         # FIXME only do this if the 'url' plugin is loaded
89         par = @bot.plugins['url'].get_title_from_html(xml)
90         next if par.empty?
91       end
92       m.reply "[#{idx}] #{par}", :overlong => :truncate
93       first_pars -=1
94     end
95   end
96
97   def wikipedia(m, params)
98     lang = params[:lang]
99     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
100     debug "Looking up things on #{site}"
101     params[:site] = site
102     params[:filter] = / - Wikipedia.*$/
103     params[:hits] = @bot.config['wikipedia.hits']
104     params[:firstpar] = @bot.config['wikipedia.first_par']
105     return google(m, params)
106   end
107 end
108
109 plugin = SearchPlugin.new
110
111 plugin.map "search *words", :action => 'google'
112 plugin.map "google *words", :action => 'google'
113 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
114 plugin.map "wp *words", :action => 'wikipedia'
115