]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/urban.rb
imdb plugin: some titles with extra info where missed when searching for movies by...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
index d2ea86456eed50d16c4a0f3ce2693b87e793138a..0b75febea9f024dbcb74a77760c28f61236e5f7c 100644 (file)
@@ -11,14 +11,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)
+      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")
-    page = @bot.httputil.get_cached(uri)
+    uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=65536"
+    page = @bot.httputil.get(uri)
     if page.nil?
       m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
       return
@@ -38,8 +39,8 @@ 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}")
-      page = @bot.httputil.get_cached(uri)
+      uri = "http://www.urbanwap.com/search.php?term=#{URI.escape words}&skip=#{n-1}"
+      page = @bot.httputil.get(uri)
       if page.nil?
         case n % 10
         when 1
@@ -60,14 +61,14 @@ class UrbanPlugin < Plugin
 
   def get_def(text)
     # Start by removing the prev/home/next links
-    t = text.gsub(/(?:<a href.*?>prev<\/a> )?<a href.*?>home<\/a>(?: <a href.*?>next<\/a>)?/,'')
+    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}")
+    t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
     # Reverse examples
     t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
     # Clear anything else
@@ -77,7 +78,7 @@ class UrbanPlugin < Plugin
   end
 
   def uotd(m, params)
-    home = @bot.httputil.get_cached("http://www.urbanwap.com/")
+    home = @bot.httputil.get("http://www.urbanwap.com/")
     if home.nil?
       m.reply "Couldn't get the urban dictionary word of the day"
       return
@@ -85,7 +86,7 @@ class UrbanPlugin < Plugin
     home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
     wotd = $1
     debug "Urban word of the day: #{wotd}"
-    page = @bot.httputil.get_cached(wotd)
+    page = @bot.httputil.get(wotd)
     if page.nil?
       m.reply "Couldn't get the urban dictionary word of the day"
     else