* (plugins/rss) fixed item unique ids
[rbot] / 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{<div style="color: #669FCE"><i>.*?</i> isn't defined}
23
24     numpages = s[%r{<div id='paginator'>.*?</div>}m].scan(/\d+/).collect {|x| x.to_i}.max || 1
25
26     rv = Array.new
27     s.scan(%r{<td class='index'[^>]*>.*?(\d+)\..*?</td>.*?<td class='word'>(?:<a.*?>)?([^>]+)(?:</a>)?</td>.*?<div class='definition'>(.+?)</div>.*?<div class='example'>(.+?)</div>}m) do |num, wrd, desc, ex|
28       rv << [num.to_i, wrd.strip, desc.strip, ex.strip]
29     end
30
31     maxnum = rv.collect {|x| x[0]}.max || 0
32     return m.reply("#{Bold}#{word}#{Bold} not found") if rv.empty?
33
34     if notfound
35       suggestions = rv.map { |s| Underline + s[1] + Underline }.uniq.join ', '
36       m.reply "#{Bold}#{word}#{Bold} not found. maybe you mean #{suggestions}?"
37       return
38     end
39
40     answer = rv.find { |a| a[0] == n }
41     answer ||= (n > maxnum ? rv.last : rv.first)
42     m.reply format_definition((p == numpages ? maxnum : "#{(numpages-1)*7 + 1}+"), *answer)
43   end
44
45   def urban(m, params)
46     words = params[:words].to_s
47     if words.empty?
48       resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
49                                :max_redir => -1,
50                                :cache => false)
51       return m.reply "Couldn't get a random urban dictionary word" if resp.nil?
52       if resp.code == "302" && (loc = resp['location'])
53         words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
54       end
55     end
56     get_def(m, words, params[:n])
57   end
58
59   def uotd(m, params)
60     home = @bot.httputil.get("http://www.urbandictionary.com/daily.php")
61     if home.nil?
62       m.reply "Couldn't get the urban dictionary word of the day"
63       return
64     end
65     home.match(%r{href="/define.php\?term=.*?">(.*?)<})
66     wotd = $1
67     debug "Urban word of the day: #{wotd}"
68     if !wotd
69       m.reply "Couldn't get the urban dictionary word of the day"
70       return
71     end
72     get_def(m, wotd, 1)
73   end
74 end
75
76 plugin = UrbanPlugin.new
77 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
78 plugin.map "urban [*words]", :action => 'urban'
79 plugin.map "urbanday", :action => 'uotd'
80