Skip to content

Instantly share code, notes, and snippets.

@stevehodgkiss
Created February 25, 2012 23:42
Show Gist options
  • Save stevehodgkiss/1911699 to your computer and use it in GitHub Desktop.
Save stevehodgkiss/1911699 to your computer and use it in GitHub Desktop.
Script to download all your attachments on FreeAgent
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
@makaroni4
Copy link

@stevehodgkiss thank you very much!!! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment