Skip to content

Instantly share code, notes, and snippets.

@aisuii
Created October 31, 2011 16:10
Show Gist options
  • Save aisuii/1327850 to your computer and use it in GitHub Desktop.
Save aisuii/1327850 to your computer and use it in GitHub Desktop.
image downloader
# coding: utf-8
require 'open-uri'
if ARGV.empty? and $stdin.stat.zero?
$stdout.print(<<-EOL)
usage:
ruby #{__FILE__} url_include.text
ruby #{__FILE__} http://hato.2ch.net/akb/dat/1320063456.dat
ruby #{__FILE__} < url_include.text
EOL
exit
end
candidate_pattern = %r{(ttp://(:?\S*?)\.(:?jpe?g|gif|png))}
downloader = lambda do |io|
io.each_line do |line|
line.scan(candidate_pattern).each do |uri|
candidate = 'h' + uri.first
open(candidate){|rf| open(File.basename(candidate), 'wb+'){|lf| lf.write(rf.read) } } rescue $stderr.puts(candidate, $!)
$stdout.puts candidate
sleep 0.5
end
end
end
ARGV.each do |resource|
downloader[open(resource)]
end
downloader[$stdin] unless $stdin.stat.zero?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment