Move code to get first par from a series of urls from search plugin to Utils, and...
[rbot] / data / rbot / plugins / search.rb
1 # vim: set sw=2 et:
2 #
3 # TODO: use lr=lang_<code> or whatever is most appropriate to let google know
4 # it shouldn't use the bot's location to find the preferred language
5 require 'uri'
6
7 Net::HTTP.version_1_2
8
9 GOOGLE_WAP_LINK = /<a accesskey="(\d)" href=".*?u=(.*?)">(.*?)<\/a>/im
10
11 class SearchPlugin < Plugin
12   BotConfig.register BotConfigIntegerValue.new('google.hits',
13     :default => 3,
14     :desc => "Number of hits to return from Google searches")
15   BotConfig.register BotConfigIntegerValue.new('google.first_par',
16     :default => 0,
17     :desc => "When set to n > 0, the bot will return the first paragraph from the first n search hits")
18   BotConfig.register BotConfigIntegerValue.new('wikipedia.hits',
19     :default => 3,
20     :desc => "Number of hits to return from Wikipedia searches")
21   BotConfig.register BotConfigIntegerValue.new('wikipedia.first_par',
22     :default => 1,
23     :desc => "When set to n > 0, the bot will return the first paragraph from the first n wikipedia search hits")
24
25   def help(plugin, topic="")
26     case topic
27     when "search", "google"
28       "#{topic} <string> => search google for <string>"
29     when "wp"
30       "wp [<code>] <string> => search for <string> on Wikipedia. You can select a national <code> to only search the national Wikipedia"
31     else
32       "search <string> (or: google <string>) => search google for <string> | wp <string> => search for <string> on Wikipedia"
33     end
34   end
35
36   def google(m, params)
37     what = params[:words].to_s
38     searchfor = URI.escape what
39     # This method is also called by other methods to restrict searching to some sites
40     if params[:site]
41       site = "site:#{params[:site]}+"
42     else
43       site = ""
44     end
45     # It is also possible to choose a filter to remove constant parts from the titles
46     # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
47     filter = params[:filter] || ""
48
49     url = "http://www.google.com/wml/search?q=#{site}#{searchfor}"
50
51     hits = params[:hits] || @bot.config['google.hits']
52
53     begin
54       wml = @bot.httputil.get_cached(url)
55     rescue => e
56       m.reply "error googling for #{what}"
57       return
58     end
59     results = wml.scan(GOOGLE_WAP_LINK)
60     if results.length == 0
61       m.reply "no results found for #{what}"
62       return
63     end
64     urls = Array.new
65     results = results[0...hits].map { |res|
66       n = res[0]
67       t = Utils.decode_html_entities res[2].gsub(filter, '').strip
68       u = URI.unescape res[1]
69       urls.push(u)
70       "#{n}. #{Bold}#{t}#{Bold}: #{u}"
71     }.join(" | ")
72
73     m.reply "Results for #{what}: #{results}", :split_at => /\s+\|\s+/
74
75     first_pars = params[:firstpar] || @bot.config['google.first_par']
76
77     Utils.get_first_pars urls, first_pars, :http_util => @bot.httputil, :message => m
78
79   end
80
81   def wikipedia(m, params)
82     lang = params[:lang]
83     site = "#{lang.nil? ? '' : lang + '.'}wikipedia.org"
84     debug "Looking up things on #{site}"
85     params[:site] = site
86     params[:filter] = / - Wikipedia.*$/
87     params[:hits] = @bot.config['wikipedia.hits']
88     params[:firstpar] = @bot.config['wikipedia.first_par']
89     return google(m, params)
90   end
91 end
92
93 plugin = SearchPlugin.new
94
95 plugin.map "search *words", :action => 'google'
96 plugin.map "google *words", :action => 'google'
97 plugin.map "wp :lang *words", :action => 'wikipedia', :requirements => { :lang => /^\w\w\w?$/ }
98 plugin.map "wp *words", :action => 'wikipedia'
99