aboutsummaryrefslogtreecommitdiff
blob: 485d7a3c677f894bfa6f5814e58c7ce60103df92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#-- vim:sw=2:et:ft=ruby

GOOGLE_WAP_SEARCH = "http://www.google.com/m/search?hl=en&q="
GOOGLE_WAP_LINK = /<a href="(?:.*?u=(.*?)|(http:\/\/.*?))">(.*?)<\/a>/im

class GentooSearchPlugin < Plugin
  def listen(m)
    #return if m.address?
    # if the channel is #gentoo, you MUST use "g? $QUERY" to cut down on spam.
    return if m.target.to_s == "#gentoo" && m.message !~ /^g\? (.+)$/
    # Otherwise, you can use "g? $QUERY" or "? $QUERY" to search.
    return if m.message !~ /^g?\? (.+)$/i
    search = $1
    #m.reply "doing search for #{search}"
    params = {}
    params[:words] = search
    return gentoo_search(m, params)
  end

  def gentoo_search(m, params)
    params[:site] = 'gentoo.org'
    return google(m, params)
  end
  
  def google(m, params)
    what = params[:words].to_s
    searchfor = CGI.escape what
    # This method is also called by other methods to restrict searching to some sites
    if params[:site]
      site = "site:#{params[:site]}+"
    else
      site = ""
    end
    # It is also possible to choose a filter to remove constant parts from the titles
    # e.g.: "Wikipedia, the free encyclopedia" when doing Wikipedia searches
    filter = params[:filter] || ""

    url = GOOGLE_WAP_SEARCH + site + searchfor

    hits = params[:hits] || @bot.config['google.hits']
    hits = 1 if params[:lucky]

    first_pars = params[:firstpar] || @bot.config['google.first_par']

    single = params[:lucky] || (hits == 1 and first_pars == 1)

    begin
      wml = @bot.httputil.get(url)
      raise unless wml
    rescue => e
      m.reply "error googling for #{what}"
      return
    end
    results = wml.scan(GOOGLE_WAP_LINK)

    if results.length == 0
      m.reply "no results found for #{what}"
      return
    end

    single ||= (results.length==1)
    urls = Array.new
    n = 0
    results = results[0...hits].map { |res|
      n += 1
      t = res[2].ircify_html(:img => "[%{src} %{alt} %{dimensions}]").strip
      u = URI.unescape(res[0] || res[1])
      urls.push(u)
      "%{n}%{b}%{t}%{b}%{sep}%{u}" % {
        :n => (single ? "" : "#{n}. "),
        :sep => (single ? " -- " : ": "),
        :b => Bold, :t => t, :u => u
      }
    }

     if params[:lucky]
       m.reply results.first
       return
     end

    result_string = results.join(" | ")

    # If we return a single, full result, change the output to a more compact representation
    if single
      m.reply "Result for %s: %s -- %s" % [what, result_string, Utils.get_first_pars(urls, first_pars)], :overlong => :truncate
      return
    end

    m.reply "Results for #{what}: #{result_string}", :split_at => /\s+\|\s+/

    return unless first_pars > 0

    Utils.get_first_pars urls, first_pars, :message => m

  end
end

plugin = GentooSearchPlugin.new

#plugin.map "? *words", :action => 'gentoo_search', :threaded => true