]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/urban.rb
Totally reworked urban dictionary plugin to use WAP. Much faster, much cleaner, doesn...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
1 require 'uri'
2
3 class UrbanPlugin < Plugin
4
5   def help( plugin, topic="")
6     "urban [word] [n]: give the [n]th definition of [word] from urbandictionary.com. urbanday: give the word-of-the-day at urban"
7   end
8
9   def urban(m, params)
10     words = params[:words].to_s
11     n = params[:n].nil? ? 1 : params[:n].to_i rescue 1
12
13     if words.empty?
14       uri = URI.parse( "http://www.urbandictionary.com/random.php" )
15       @bot.httputil.head(uri) { |redir|
16         words = URI.unescape(redir.match(/define.php\?term=(.*)$/)[1]) rescue nil
17       }
18     end
19     # we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition
20     uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536")
21     page = @bot.httputil.get(uri)
22     if page.nil?
23       m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
24       return
25     end
26     if page =~ / is undefined<\/card><\/wml>/
27       m.reply "There is no urban dictionary definition of #{words}"
28       return
29     end
30     if page =~ /&amp;skip=(\d+)">prev<\/a>/
31       numdefs = $1.to_i + 1
32     else
33       numdefs = 1
34     end
35     n = numdefs + n + 1 if n < 0
36     if n > numdefs
37       m.reply "Urban dictionary only has #{numdefs} definitions for '#{words}'"
38       n = numdefs
39     end
40     if n < numdefs
41       uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}")
42       page = @bot.httputil.get(uri)
43       if page.nil?
44         case n % 10
45         when 1
46           ord = 'st'
47         when 2
48           ord = 'nd'
49         when 3
50           ord = 'rd'
51         else
52           ord = 'th'
53         end
54         m.reply "Couldn't retrieve the #{n}#{ord} urban dictionary definition of #{words}"
55         return
56       end
57     end
58     m.reply "#{get_def(page)} (#{n}/#{numdefs})"
59   end
60
61   def get_def(text)
62     Utils.decode_html_entities text.gsub(/(?:<a href.*?>prev<\/a> )?<a href.*?>home<\/a>(?: <a href.*?>next<\/a>)?/,'').gsub(/<\/?p>/, ' ').gsub(/<.*?>/, '').gsub("\n", ' ').strip
63   end
64
65   def uotd(m, params)
66     home = @bot.httputil.get("http://www.urbanwap.com/")
67     if home.nil?
68       m.reply "Couldn't get the urban dictionary word of the day"
69       return
70     end
71     home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
72     wotd = $1
73     debug "Urban word of the day: #{wotd}"
74     page = @bot.httputil.get(wotd)
75     if page.nil?
76       m.reply "Couldn't get the urban dictionary word of the day"
77     else
78       m.reply get_def(page)
79     end
80   end
81 end
82
83 plugin = UrbanPlugin.new
84 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
85 plugin.map "urban [*words]", :action => 'urban'
86 plugin.map "urbanday", :action => 'uotd'
87