]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/urban.rb
6d01c625d0dd3515f9e42876a2ebe8e07fde05b9
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
1 class UrbanPlugin < Plugin
2   URBAN = 'http://www.urbandictionary.com/define.php?term='
3
4   def help( plugin, topic="")
5     "urban [word] [n]: give the [n]th definition of [word] from urbandictionary.com. urbanday: give the word-of-the-day at urban"
6   end
7
8   def format_definition(total, num, word, desc, ex)
9     "#{Bold}#{word} (#{num}/#{total})#{Bold}: " +
10     desc.ircify_html(:limit => 300) + " " +
11     "<i>#{ex}</i>".ircify_html(:limit => 100)
12   end
13
14   def get_def(m, word, n = nil)
15     n = n ? n.to_i : 1
16     p = (n-1)/7 + 1
17     u = URBAN + URI.escape(word)
18     u += '&page=' + p.to_s if p > 1
19     s = @bot.httputil.get(u)
20     return m.reply("Couldn't get the urban dictionary definition for #{word}") if s.nil?
21
22     notfound = s.match %r{<i>.*?</i> isn't defined}
23
24     numpages = if s[%r{<ul class="pagination[^"]+">.*?</ul>}m]
25       debug 'foo->'
26       debug $&
27       $&.scan(/>(\d+)</).collect {|x| x[0].to_i}.max || 1
28     else 1 end
29
30     rv = Array.new
31     num = 1
32     s.scan(%r{<a class="word"[^>]* href="\/define[^>]*>([^<]+)</a>.*?<div class='meaning'>(.+?)</div>.*?<div class='example'>(.+?)</div>}m) do |wrd, desc, ex|
33       rv << [num, wrd.strip, desc.strip, ex.strip]
34       num += 1
35     end
36
37     maxnum = rv.collect {|x| x[0]}.max || 0
38     return m.reply("#{Bold}#{word}#{Bold} not found") if rv.empty?
39
40     if notfound
41       suggestions = rv.map { |str| Underline + str[1] + Underline }.uniq.join ', '
42       m.reply "#{Bold}#{word}#{Bold} not found. maybe you mean #{suggestions}?"
43       return
44     end
45
46     answer = rv.find { |a| a[0] == n }
47     answer ||= (n > maxnum ? rv.last : rv.first)
48     m.reply format_definition((p == numpages ? maxnum : "#{(numpages-1)*7 + 1}+"), *answer)
49   end
50
51   def urban(m, params)
52     words = params[:words].to_s
53     if words.empty?
54       resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
55                                :max_redir => -1,
56                                :cache => false)
57       return m.reply("Couldn't get a random urban dictionary word") if resp.nil?
58       if resp.code == "302" && (loc = resp['location'])
59         words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
60       end
61     end
62     get_def(m, words, params[:n])
63   end
64
65   def uotd(m, params)
66     home = @bot.httputil.get("http://www.urbandictionary.com/daily.php")
67     if home.nil?
68       m.reply "Couldn't get the urban dictionary word of the day"
69       return
70     end
71     home.match(%r{href="/define.php\?term=.*?">(.*?)<})
72     wotd = $1
73     debug "Urban word of the day: #{wotd}"
74     if !wotd
75       m.reply "Couldn't get the urban dictionary word of the day"
76       return
77     end
78     get_def(m, wotd, 1)
79   end
80 end
81
82 plugin = UrbanPlugin.new
83 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
84 plugin.map "urban [*words]", :action => 'urban'
85 plugin.map "urbanday", :action => 'uotd'
86