Last active
December 17, 2015 18:09
-
-
Save ToQoz/5651503 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'optparse' | |
require 'fileutils' | |
require 'open-uri' | |
require 'nokogiri' | |
# = Options = | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: dl.rb [options]" | |
opts.on("-u VALUE", "--url VALUE", "Target url") do |v| | |
options[:url] = v | |
end | |
opts.on("-p VALUE", "--parent_css_selector VALUE", "Parent css selector") do |v| | |
options[:parent_css_selector] = v | |
end | |
opts.on("-d VALUE", "--dir VALUE", "Save dir") do |v| | |
options[:dir] = v | |
end | |
end.parse! | |
[:url, :parent_css_selector, :dir].each do |required_key| | |
raise OptionParser::MissingArgument, "missing argument: --#{required_key}. this is required" if options[required_key].nil? | |
end | |
# = Execute = | |
FileUtils.mkdir_p(options[:dir]) unless File.exists?(options[:dir]) | |
document = Nokogiri::HTML(open(options[:url])) | |
img_selector = "#{options[:parent_css_selector]} img" | |
document.css(img_selector).each_with_index do |img, index| | |
src = img[:src] | |
ext = src.split('.').last | |
filepath = File.join(options[:dir], "#{index}.#{ext}") | |
File.open(filepath, 'wb') { |f| | |
f.puts open(src).read | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment