X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Furban.rb;h=0c7d98387cf8a10b466a7de1a95c172f02b2c01f;hb=8717ea03bf4e6e84caf185a0cc5df8299e4e67a7;hp=95210b252460980d7918c9067327ce929685189e;hpb=1d7ab28b2162db5c3a7b332a2f268d496b7c3c60;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/urban.rb b/data/rbot/plugins/urban.rb index 95210b25..0c7d9838 100644 --- a/data/rbot/plugins/urban.rb +++ b/data/rbot/plugins/urban.rb @@ -1,5 +1,3 @@ -require 'uri' - class UrbanPlugin < Plugin def help( plugin, topic="") @@ -11,13 +9,15 @@ class UrbanPlugin < Plugin 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}" @@ -38,7 +38,7 @@ class UrbanPlugin < Plugin 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 @@ -60,14 +60,14 @@ class UrbanPlugin < Plugin def get_def(text) # Start by removing the prev/home/next links - t = text.gsub(/(?:prev<\/a> )?home<\/a>(?: next<\/a>)?/,'') + t = text.gsub(/(?:]*>prev<\/a> )?]*>home<\/a>(?: ]*>next<\/a>)?/,'') # Close up paragraphs t.gsub!(/<\/?p>/, ' ') t.gsub!("\n", ' ') # Reverse headings t.gsub!(/<\/?b>/,"#{Reverse}") # Enbolden links - t.gsub!(/<\/?a(?: .*?)?>/,"#{Bold}") + t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}") # Reverse examples t.gsub!(/<\/?(?:i|em)>/,"#{Underline}") # Clear anything else