-require 'uri'
-
class UrbanPlugin < Plugin
def help( plugin, topic="")
n = params[:n].nil? ? 1 : params[:n].to_i rescue 1
if words.empty?
- uri = URI.parse( "http://www.urbandictionary.com/random.php" )
- @bot.httputil.head(uri) { |redir|
- words = URI.unescape(redir.match(/define.php\?term=(.*)$/)[1]) rescue nil
- }
+ resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
+ :max_redir => -1,
+ :cache => false)
+ if resp.code == "302" && (loc = resp['location'])
+ words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
+ end
end
# we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition
- uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536")
+ uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=65536"
page = @bot.httputil.get(uri)
if page.nil?
m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
n = numdefs
end
if n < numdefs
- uri = URI.parse("http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}")
+ uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=#{n-1}"
page = @bot.httputil.get(uri)
if page.nil?
case n % 10
end
def get_def(text)
- Utils.decode_html_entities text.gsub(/(?:<a href.*?>prev<\/a> )?<a href.*?>home<\/a>(?: <a href.*?>next<\/a>)?/,'').gsub(/<\/?p>/, ' ').gsub(/<.*?>/, '').gsub("\n", ' ').strip
+ # Start by removing the prev/home/next links
+ t = text.gsub(/(?:<a href[^>]*>prev<\/a> )?<a href[^>]*>home<\/a>(?: <a href[^>]*>next<\/a>)?/,'')
+ # Close up paragraphs
+ t.gsub!(/<\/?p>/, ' ')
+ t.gsub!("\n", ' ')
+ # Reverse headings
+ t.gsub!(/<\/?b>/,"#{Reverse}")
+ # Enbolden links
+ t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
+ # Reverse examples
+ t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
+ # Clear anything else
+ t.gsub!(/<.*?>/, '')
+
+ Utils.decode_html_entities t.strip
end
def uotd(m, params)