Skip to content

Instantly share code, notes, and snippets.

@elithrar
Last active July 4, 2016 19:14
Show Gist options
  • Save elithrar/086a26905de8394ddd6226779225267f to your computer and use it in GitHub Desktop.
Save elithrar/086a26905de8394ddd6226779225267f to your computer and use it in GitHub Desktop.
archive_cloudapp.rb - leverages https://github.com/aaronrussell/cloudapp_api to archive your CloudApp uploads to the current directory. Intentionally simple.
# vim: ts=2 et sw=2 sts=2
# #!/usr/bin/ruby
# (c) 2016, Matt Silverlock. BSD 3-Clause licensed.
# Downloads all items in your CloudApp account to the current directory.
require "cloudapp_api"
require "optparse"
require "json"
require "open-uri"
require "shellwords"
options = {}
options[:start] = 1
options[:per_page] = 24
options[:pages] = 10
OptionParser.new do |opts|
opts.banner = "Usage: archive_cloudapp.rb [options]"
opts.on("--email EMAIL", "CloudApp username (email address)") do |v|
options[:email] = v
end
opts.on("--password PASSWORD", "CloudApp password") do |v|
options[:password] = v
end
opts.on("--start-page NUM", "Page to start at") do |v|
options[:start_page] = v
end
opts.on("--pages NUM", "Pages to download") do |v|
options[:pages] = v
end
opts.on("--per-page NUM", "Items per page to download") do |v|
options[:per_page] = v
end
end.parse!
# Authenticate and download an array of all items.
CloudApp.authenticate options[:email], options[:password]
count = 0
items = {}
start = options[:start_page].to_i
last = options[:pages].to_i
(start..last).each do |page|
@drops = CloudApp::Drop.all({:page => page, :per_page => options[:per_page].to_i})
@drops.each do |drop|
items[drop.name] = drop.remote_url
count += 1
end
end
puts "Downloaded #{count} items.\n"
items.each do |name, url|
unless url.nil? || url == ""
name = name.gsub(/\//, "-")
path = (File.join(Dir.pwd, name))
puts "Writing #{path}"
File.open(path, "w") do |f|
f.write open(url).read
f.close
end
else
puts "Skipped non-file drop: #{name}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment