1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
require 'rubygems'
require 'open-uri'
require 'net/http'
require 'hpricot'
require 'uri'
site = ARGV[0];
def get_size(url)
uri = URI.parse(url)
response = nil
Net::HTTP.start(uri.host, uri.port) do |http|
response = http.head(uri.path + ((uri.query) ? "?#{uri.query}" : ""))
end
response["content-length"].to_i
end
if site
images = []
base = site.gsub(/[^\/]+$/, '')
full_base = base.gsub(/^(.+\/\/[^\/]+).*/, '\1')
doc = Hpricot(open(site))
(doc / "img").each do |image|
src = image.attributes["src"]
src = "#{full_base}#{src}" if src.match(/^\//)
src = "#{base}#{src}" unless src.match(/^http/)
width = image.attributes["width"].to_i
height = image.attributes["height"].to_i
size = 0
if (width.zero? && height.zero?)
size = get_size(src)
end
images << { :src => src, :width => width, :height => height, :size => size || height * width}
end
images.sort! { |a, b| a[:size] <=> b[:size] }
images.reverse.each do |image|
puts "<img src='#{image[:src]}'/>#{image.inspect}"
end
end
|