Use optparse to add command line options
This commit is contained in:
parent
9055a1e0d0
commit
97cca39507
|
@ -5,7 +5,10 @@ dependency-less ruby script that downloads - [in other words, steals](https://na
|
||||||
|
|
||||||
usage
|
usage
|
||||||
-----
|
-----
|
||||||
ruby vsco-dl.rb username
|
Usage: vsco-dl.rb [options] username
|
||||||
|
-m, --[no-]metadata Save metadata
|
||||||
|
-o, --output=output Where to save the files (default is cwd)
|
||||||
|
-w, --[no-]overwrite Overwrite previously downloaded files
|
||||||
|
|
||||||
y tho
|
y tho
|
||||||
-----
|
-----
|
||||||
|
|
48
vsco-dl.rb
48
vsco-dl.rb
|
@ -1,8 +1,36 @@
|
||||||
require 'json'
|
require 'json'
|
||||||
require 'open-uri'
|
require 'open-uri'
|
||||||
|
require 'fileutils'
|
||||||
|
require 'optparse'
|
||||||
|
|
||||||
|
options = {}
|
||||||
|
parser = OptionParser.new do |opts|
|
||||||
|
opts.banner = "Usage: vsco-dl.rb [options] username"
|
||||||
|
|
||||||
|
opts.on "-m", "--[no-]metadata", "Save metadata" do |m|
|
||||||
|
options[:metadata] = m
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on "-oOUTPUT", "--output=output", "Where to save the files (default is cwd)" do |o|
|
||||||
|
options[:output] = o
|
||||||
|
end
|
||||||
|
|
||||||
|
opts.on "-w", "--[no-]overwrite", "Overwrite previously downloaded files" do |w|
|
||||||
|
options[:overwrite] = w
|
||||||
|
end
|
||||||
|
end
|
||||||
|
parser.parse!
|
||||||
|
|
||||||
user = ARGV[0]
|
user = ARGV[0]
|
||||||
Dir.mkdir user unless File.exist? user
|
if user.nil?
|
||||||
|
$stderr.puts "Error: Username is required."
|
||||||
|
$stderr.puts parser
|
||||||
|
exit 1
|
||||||
|
end
|
||||||
|
|
||||||
|
path = user
|
||||||
|
path = File.join options[:output], user unless options[:output].nil?
|
||||||
|
FileUtils.mkdir_p path unless File.exist? path
|
||||||
|
|
||||||
# this is v gross, but they probably do it like this on purpose
|
# this is v gross, but they probably do it like this on purpose
|
||||||
initial = open "https://vsco.co/#{user}/images/1"
|
initial = open "https://vsco.co/#{user}/images/1"
|
||||||
|
@ -15,17 +43,21 @@ images.each_with_index do |r, i|
|
||||||
print "Image #{i + 1} of #{images.length}\r"
|
print "Image #{i + 1} of #{images.length}\r"
|
||||||
$stdout.flush
|
$stdout.flush
|
||||||
|
|
||||||
jsonPath = File.join user, "#{r['upload_date']}.json"
|
file_path = File.join path, "#{r['upload_date']}"
|
||||||
unless File.exist? jsonPath
|
|
||||||
File.open jsonPath, 'w' do |file|
|
if options[:metadata]
|
||||||
file.write JSON.pretty_generate r
|
json_path = "#{file_path}.json"
|
||||||
|
if options[:overwrite] or not File.exist? json_path
|
||||||
|
File.open json_path, 'w' do |file|
|
||||||
|
file.write JSON.pretty_generate r
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
jpgPath = File.join user, "#{r['upload_date']}.jpg"
|
jpg_path = "#{file_path}.jpg"
|
||||||
unless File.exist? jpgPath
|
if options[:overwrite] or not File.exist? jpg_path
|
||||||
open "https://#{r['responsive_url']}" do |f|
|
open "https://#{r['responsive_url']}" do |f|
|
||||||
File.open jpgPath, 'wb' do |file|
|
File.open jpg_path, 'wb' do |file|
|
||||||
file.write f.read
|
file.write f.read
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue