Skip to content

Instantly share code, notes, and snippets.

@petrbela
Last active December 16, 2015 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petrbela/5367183 to your computer and use it in GitHub Desktop.
Save petrbela/5367183 to your computer and use it in GitHub Desktop.
Downloads all assets from a chute to local folder
#!/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