]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/url.rb
use pretty printing with Logger (debug, info, warning and error messages)
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / url.rb
index 95ccb5d95dcdc2f8504efc70066cab10d3abf04d..a1a325eb3d5f9bdfbc60f0e5ef368bd2687194b4 100644 (file)
@@ -1,5 +1,3 @@
-require 'uri'
-
 Url = Struct.new("Url", :channel, :nick, :time, :url)
 TITLE_RE = /<\s*?title\s*?>(.+?)<\s*?\/title\s*?>/im
 LINK_INFO = "[Link Info]"
@@ -56,28 +54,34 @@ class UrlPlugin < Plugin
         # be a webpage, retrieve the title from the page
         debug "+ getting #{url.request_uri}"
 
-        # we act differently depending on whether we want the first par or not:
-        # in the first case we download the initial part and the parse it; in the second
-        # case we only download as much as we need to find the title
+       title = get_title_from_html(body)
         if @bot.config['url.first_par']
-          first_par = Utils.ircify_first_html_par(body)
-          extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty?
-          title = get_title_from_html(body)
-          return "title: #{title}#{extra}" if title
+          first_par = Utils.ircify_first_html_par(body, :strip => title)
+          extra << ", #{Bold}text#{Bold}: #{first_par}" unless first_par.empty?
+          return "#{Bold}title#{Bold}: #{title}#{extra}" if title
         else
-          title = get_title_from_html(body)
-          return "title: #{title}" if title
+          return "#{Bold}title#{Bold}: #{title}" if title
         end
 
         # if nothing was found, provide more basic info
       end
 
       debug response.to_hash.inspect
+
+      enc = response['content-encoding']
+
+      extra << ", #{Bold}encoding#{Bold}: #{enc}" if enc
+
       unless @bot.config['url.titles_only']
         # content doesn't have title, just display info.
         size = response['content-length'].gsub(/(\d)(?=\d{3}+(?:\.|$))(\d{3}\..*)?/,'\1,\2') rescue nil
-        size = size ? ", size: #{size} bytes" : ""
-        return "type: #{response['content-type']}#{size}#{extra}"
+        if response.code == '206'
+          if response['content-range'] =~ /bytes\s*[^\/]+\/(\d+)/
+            size = $1.to_s.reverse.scan(/\d{1,3}/).join(',').reverse
+          end
+        end
+        size = size ? ", #{Bold}size#{Bold}: #{size} bytes" : ""
+        return "#{Bold}type#{Bold}: #{response['content-type']}#{size}#{extra}"
       end
     rescue Exception => e
       error e.inspect