lib/linkser/objects/html.rb in linkser-0.0.8 vs lib/linkser/objects/html.rb in linkser-0.0.9

- old
+ new

@@ -22,11 +22,11 @@ max_images = @options[:max_images] || 5 Array.new.tap do |images| if ogp and ogp.image begin img_spec = ImageSpec.new(ogp.image) - if valid_img? img_spec.width.to_f, img_spec.height.to_f + if valid_img? img_spec.width.to_f, img_spec.height.to_f and can_hotlink_img? ogp.image images << Linkser::Resource.new({:type => "image", :url => ogp.image, :width => img_spec.width, :height => img_spec.height}) end rescue end end @@ -40,11 +40,11 @@ img_name = File.basename(img_uri.path,img_ext) if [".jpg", ".jpeg", ".png"].include? img_ext begin img_spec = ImageSpec.new(img_src) - if valid_img? img_spec.width.to_f, img_spec.height.to_f + if valid_img? img_spec.width.to_f, img_spec.height.to_f and can_hotlink_img? img_src images << Linkser::Resource.new({:type => "image", :url => img_src, :width => img_spec.width, :height => img_spec.height}) end rescue end end @@ -95,9 +95,28 @@ if ((w > 0 and h > 0 and ((w / h) < 3) and ((w / h) > 0.2)) or (w > 0 and h == 0 and w < 700) or (w == 0 and h > 0 and h < 700)) return true end end false + end + + def can_hotlink_img? url, limit=10 + uri = URI.parse(url) + http = Net::HTTP.new uri.host, uri.port + http.start do |agent| + agent.add_field 'Referrer', 'http://www.linkser.com/' + response = agent.head uri.request_uri + case response + when Net::HTTPSuccess then + return true + when Net::HTTPRedirection then + location = response['location'] + warn "Redirecting image to #{location}" + return can_hotlink_img? location, limit - 1 + else + return false + end + end end end end end