get_image.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
require 'digest/sha1'
require 'nokogiri'
require 'pathname'
require 'parallel'
require 'open-uri'
dir = Pathname.new('/path/to/download')
dir.mkdir unless dir.exist?
html = Nokogiri::HTML(open(ARGV[0]))
Parallel.each(html.css('a'), :in_threads => 10) do |link|
next unless link['href'] =~ /\.jpg$/
filename = Digest::SHA1.hexdigest(link['href']) + '.jpg'
filepath = dir + filename
next if filepath.file?
puts "Download... #{link['href']}"
res = open(link['href']) rescue next
if res.content_type =~ /^image/
open(filepath, 'w').print res.read
end
end