Last active
December 16, 2015 03:08
-
-
Save petrbela/5367183 to your computer and use it in GitHub Desktop.
Downloads all assets from a chute to local folder
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 'FileUtils' | |
require 'net/http' | |
require 'open-uri' | |
require 'parallel' | |
require 'json' | |
print "Chute shortcut: " | |
chute_shortcut = gets.chomp | |
begin | |
print "This will download all assets from chute #{chute_shortcut} to the current folder. Continue? [Y/n] " | |
cont = gets.chomp | |
exit if cont == 'n' || cont == 'N' | |
end while cont != 'Y' && cont != 'y' && cont != '' | |
per_page = 50 | |
page = 0 | |
max_id = nil | |
assets = [] | |
begin | |
page = page + 1 | |
response = Net::HTTP.get_response(URI("http://api.getchute.com/v2/albums/#{chute_shortcut}/assets?per_page=#{per_page}" + (max_id ? "&max_id=#{max_id}" : ""))) | |
unless response.code == '200' | |
if response.code == '401' | |
puts "Error! Invalid auth token." | |
else | |
puts "Invalid request." | |
end | |
puts response.body | |
exit | |
end | |
puts "Downloading assets... page #{page}" | |
assets = JSON.parse(response.body)['data'] | |
Parallel.map(assets, :in_threads => 8) do |asset| | |
url = asset['source']['source_url'] | |
File.open("#{asset['id']}#{File.extname(url)}", 'wb') do |file| | |
begin | |
file.write open(url).read | |
puts "Downloaded asset id=#{asset['id']}" | |
rescue StandardError | |
puts "Cannot download asset with id=#{asset['id']}, probably removed." | |
end | |
end | |
end | |
max_id = assets.last['chute_asset_id'] if assets.last | |
end while assets.size == per_page | |
puts "Finished! #{(page-1)*per_page + assets.size} assets downloaded." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment