-
-
Save trevorturk/2989101 to your computer and use it in GitHub Desktop.
Script to download all attachments from a basecamp project
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
require 'faraday' | |
require 'faraday_middleware' | |
url_project = 'https://ciblonet.basecamphq.com' | |
project_id = 3684800 | |
user_name = 'shingara' | |
password = 'xxxx' | |
class Attachment | |
def initialize(conn, attachment) | |
@conn = conn | |
@attachment = attachment | |
end | |
def collection | |
@attachment['collection'].to_s | |
end | |
def dir | |
Dir.mkdir(collection) unless Dir.exists?(collection) | |
collection | |
end | |
def fetch | |
File.new(File.join(dir, @attachment['name']), 'w').write(@conn.get(@attachment['download_url']).body) | |
end | |
end | |
class Attachments | |
def initialize(conn, project_id, step=nil) | |
@conn = conn | |
@project_id = project_id | |
@attachments_index = 100 | |
@step = step | |
end | |
def url | |
if @step | |
"/projects/#{@project_id}/attachments.xml?n=#{@step}" | |
else | |
"/projects/#{@project_id}/attachments.xml" | |
end | |
end | |
def entries | |
@entries ||= @conn.get(url) do |req| | |
req.headers['Accept'] = 'application/xml' | |
req.headers['Content-Type'] = 'application/xml' | |
end.body['attachments'] | |
end | |
def fetch | |
entries.each do |attachment| | |
Attachment.new(@conn, attachment).fetch | |
end | |
end | |
def more? | |
entries.size > 99 | |
end | |
end | |
class FetchAttachments | |
def self.execute(conn, project_id) | |
a = Attachments.new(conn, project_id) | |
a.fetch | |
step = 100 | |
while a.more? | |
a = Attachments.new(conn, project_id, step) | |
a.fetch | |
step += 100 | |
end | |
end | |
end | |
con = Faraday.new(:url => url_project) do |builder| | |
builder.use Faraday::Request::BasicAuthentication, user_name, password | |
builder.use Faraday::Response::Logger | |
builder.response :xml, :content_type => /\bxml$/ | |
builder.use Faraday::Adapter::NetHttp | |
end | |
FetchAttachments.execute(con, project_id) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment