3 class UrbanPlugin < Plugin
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"
10 words = params[:words].to_s
11 n = params[:n].nil? ? 1 : params[:n].to_i rescue 1
14 resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
16 if resp.code == "302" && (loc = resp['location'])
17 words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
20 # we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition
21 uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536"
22 page = @bot.httputil.get(uri)
24 m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
27 if page =~ / is undefined<\/card><\/wml>/
28 m.reply "There is no urban dictionary definition of #{words}"
31 if page =~ /&skip=(\d+)">prev<\/a>/
36 n = numdefs + n + 1 if n < 0
38 m.reply "Urban dictionary only has #{numdefs} definitions for '#{words}'"
42 uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}"
43 page = @bot.httputil.get(uri)
55 m.reply "Couldn't retrieve the #{n}#{ord} urban dictionary definition of #{words}"
59 m.reply "#{get_def(page)} (#{n}/#{numdefs})"
63 # Start by removing the prev/home/next links
64 t = text.gsub(/(?:<a href[^>]*>prev<\/a> )?<a href[^>]*>home<\/a>(?: <a href[^>]*>next<\/a>)?/,'')
66 t.gsub!(/<\/?p>/, ' ')
69 t.gsub!(/<\/?b>/,"#{Reverse}")
71 t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
73 t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
77 Utils.decode_html_entities t.strip
81 home = @bot.httputil.get("http://www.urbanwap.com/")
83 m.reply "Couldn't get the urban dictionary word of the day"
86 home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
88 debug "Urban word of the day: #{wotd}"
89 page = @bot.httputil.get(wotd)
91 m.reply "Couldn't get the urban dictionary word of the day"
98 plugin = UrbanPlugin.new
99 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
100 plugin.map "urban [*words]", :action => 'urban'
101 plugin.map "urbanday", :action => 'uotd'