X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Furl.rb;h=f3eb3a7f506237e2f68d574e34e872b9de835987;hb=bbf28120c7975c1b5d464d35649d5a62c50bcd2f;hp=23a326384f264aecaca9c66933a4020f3d67caa0;hpb=a3cf806450893638f98096ab96c4c25023bb01c3;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/url.rb b/data/rbot/plugins/url.rb index 23a32638..f3eb3a7f 100644 --- a/data/rbot/plugins/url.rb +++ b/data/rbot/plugins/url.rb @@ -40,59 +40,48 @@ class UrlPlugin < Plugin title = nil begin - @bot.httputil.get_response(url) { |response| - case response - when Net::HTTPSuccess - extra = String.new - - if response['content-type'] =~ /^text\// - - title = String.new - - # since the content is 'text/*' and is small enough to - # 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 - if @bot.config['url.first_par'] - partial = response.partial_body(@bot.config['http.info_bytes']) - first_par = Utils.ircify_first_html_par(partial) - extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty? - title = get_title_from_html(partial) - if title - return "title: #{title}#{extra}" - end - else - response.partial_body(@bot.config['http.info_bytes']) { |part| - title = get_title_from_html(part) - return "title: #{title}" if title - } - end - # if nothing was found, provide more basic info - end + range = @bot.config['http.info_bytes'] + response = @bot.httputil.get_response(url, :range => "bytes=0-#{range}") + if response.code != "206" && response.code != "200" + return "Error getting link (#{response.code} - #{response.message})" + end + extra = String.new - debug response.to_hash.inspect - 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}" - end - when Net::HTTPResponse - return "Error getting link (#{response.code} - #{response.message})" + if response['content-type'] =~ /^text\// + + body = response.body.slice(0, range) + title = String.new + + # since the content is 'text/*' and is small enough to + # 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 + if @bot.config['url.first_par'] + title = get_title_from_html(body) + first_par = Utils.ircify_first_html_par(body, :strip => title) + extra << "\n#{LINK_INFO} text: #{first_par}" unless first_par.empty? + return "title: #{title}#{extra}" if title else - raise response + title = get_title_from_html(body) + return "title: #{title}" if title end - } - rescue Object => e - if e.class <= StandardError - error e.inspect - debug e.backtrace.join("\n") + + # if nothing was found, provide more basic info end - msg = e.respond_to?(:message) ? e.message : e.to_s + debug response.to_hash.inspect + 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}" + end + rescue Exception => e + error e.inspect + debug e.backtrace.join("\n") return "Error connecting to site (#{e.message})" end end