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',
14 if resp.code == "302" && (loc = resp['location'])
15 words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
18 # we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition
19 uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=65536"
20 page = @bot.httputil.get(uri)
22 m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
25 if page =~ / is undefined<\/card><\/wml>/
26 m.reply "There is no urban dictionary definition of #{words}"
29 if page =~ /&skip=(\d+)">prev<\/a>/
34 n = numdefs + n + 1 if n < 0
36 m.reply "Urban dictionary only has #{numdefs} definitions for '#{words}'"
40 uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=#{n-1}"
41 page = @bot.httputil.get(uri)
53 m.reply "Couldn't retrieve the #{n}#{ord} urban dictionary definition of #{words}"
57 m.reply "#{get_def(page)} (#{n}/#{numdefs})"
61 # Start by removing the prev/home/next links
62 t = text.gsub(/(?:<a href[^>]*>prev<\/a> )?<a href[^>]*>home<\/a>(?: <a href[^>]*>next<\/a>)?/,'')
64 t.gsub!(/<\/?p>/, ' ')
67 t.gsub!(/<\/?b>/,"#{Reverse}")
69 t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
71 t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
75 Utils.decode_html_entities t.strip
79 home = @bot.httputil.get("http://www.urbanwap.com/")
81 m.reply "Couldn't get the urban dictionary word of the day"
84 home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
86 debug "Urban word of the day: #{wotd}"
87 page = @bot.httputil.get(wotd)
89 m.reply "Couldn't get the urban dictionary word of the day"
96 plugin = UrbanPlugin.new
97 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
98 plugin.map "urban [*words]", :action => 'urban'
99 plugin.map "urbanday", :action => 'uotd'