Created
February 25, 2012 23:42
-
-
Save stevehodgkiss/1911699 to your computer and use it in GitHub Desktop.
Script to download all your attachments on FreeAgent
This file contains hidden or 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
require 'rubygems' | |
require 'freeagent' | |
require 'eventmachine' | |
require 'em-synchrony' | |
require 'em-http-request' | |
require 'em-synchrony/fiber_iterator' | |
require 'active_support' | |
ATTACHMENTS_DIR = '/Users/steve/Documents/freeagent_attachments' | |
FreeAgent.configure do |config| | |
config.username = "" | |
config.password = "" | |
config.subdomain = "" | |
end | |
class FreeAgent::Attachment | |
def download | |
file_name = [ | |
attachable_type.underscore, | |
id, | |
description.try(:parameterize), | |
file_name | |
].compact.join('_') | |
file_name << File.extname(location) | |
save_as = File.join(ATTACHMENTS_DIR, file_name) | |
file = EM::Synchrony.sync EventMachine::HttpRequest.new(location).get | |
File.open(save_as, 'wb') {|f| f.write(file.response) } | |
end | |
end | |
attachments = FreeAgent::Attachment.find(:all) | |
EM.synchrony do | |
EM::Synchrony::FiberIterator.new(attachments, 50).each do |image| | |
puts "Downloading image ID: #{image.id}" | |
image.download | |
end | |
EM.stop | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@stevehodgkiss thank you very much!!! 👍