1 class UrbanPlugin < Plugin
3 def help( plugin, topic="")
4 "urban [word] [n]: give the [n]th definition of [word] from urbandictionary.com. urbanday: give the word-of-the-day at urban"
8 words = params[:words].to_s
9 n = params[:n].nil? ? 1 : params[:n].to_i rescue 1
12 resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
15 if resp.code == "302" && (loc = resp['location'])
16 words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
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 = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=65536"
21 page = @bot.httputil.get(uri)
23 m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
26 if page =~ / is undefined<\/card><\/wml>/
27 m.reply "There is no urban dictionary definition of #{words}"
30 if page =~ /&skip=(\d+)">prev<\/a>/
35 n = numdefs + n + 1 if n < 0
37 m.reply "Urban dictionary only has #{numdefs} definitions for '#{words}'"
41 uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=#{n-1}"
42 page = @bot.httputil.get(uri)
54 m.reply "Couldn't retrieve the #{n}#{ord} urban dictionary definition of #{words}"
58 m.reply "#{get_def(page)} (#{n}/#{numdefs})"
62 # Start by removing the prev/home/next links
63 t = text.gsub(/(?:<a href[^>]*>prev<\/a> )?<a href[^>]*>home<\/a>(?: <a href[^>]*>next<\/a>)?/,'')
65 t.gsub!(/<\/?p>/, ' ')
68 t.gsub!(/<\/?b>/,"#{Reverse}")
70 t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
72 t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
76 Utils.decode_html_entities t.strip
80 home = @bot.httputil.get("http://www.urbanwap.com/")
82 m.reply "Couldn't get the urban dictionary word of the day"
85 home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
87 debug "Urban word of the day: #{wotd}"
88 page = @bot.httputil.get(wotd)
90 m.reply "Couldn't get the urban dictionary word of the day"
97 plugin = UrbanPlugin.new
98 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
99 plugin.map "urban [*words]", :action => 'urban'
100 plugin.map "urbanday", :action => 'uotd'