Skip to content

Instantly share code, notes, and snippets.

@ntalbott
Created February 16, 2012 19:06
Show Gist options
  • Save ntalbott/1847068 to your computer and use it in GitHub Desktop.
Save ntalbott/1847068 to your computer and use it in GitHub Desktop.
Downloads all of your Basecamp files for archiving

Basecamp Exporter

This is a little script that will walk your Basecamp XML export (which you can download from Basecamp directly) and download all the associated files for you. Makes for a much more complete Basecamp export, especially if you did much design work in your projects.

Enjoy!

Caveats

  • Probably doesn't work with old-school FTP file storage; but if you're using that, you should have all the files yourself anyhow.
require 'nokogiri'
require 'open-uri'
require 'fileutils'
require 'openssl'
require 'progress'
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
def OpenURI.redirectable?(uri1, uri2)
true
end
def help!; puts "Usage: <subdomain> <api_token> <export_xml_file>"; exit(1); end
$subdomain = (ARGV[0] || help!)
$api_token = (ARGV[1] || help!)
$xml_file = (ARGV[2] || help!)
def get(path, bare=false)
path = (bare ? path : "https://#{$subdomain}.basecamphq.com#{path}")
open(path, http_basic_authentication: [$api_token, 'X']).read
end
doc = Nokogiri.XML(File.read($xml_file))
doc.xpath("/account/projects/project").with_progress("Projects").each do |project|
name = project.xpath("name").first.text
project_directory = File.join("exports", $subdomain, name)
project.xpath("posts/post").with_progress("Posts").each do |post|
next unless(post.xpath("attachments-count").text.to_i > 0)
post_directory = File.join(project_directory, "[#{post.xpath("id").text.rjust(10, "0")}] #{post.xpath("title").text.gsub('/', '-')}")
FileUtils.mkdir_p(post_directory)
full_post = Nokogiri.XML(get("/posts/#{post.xpath("id").text}.xml"))
full_post.xpath("/post/attachments/attachment").with_progress("Attachments").each do |attachment|
filename = File.join(post_directory, attachment.xpath("name").text)
next if(File.exist?(filename))
begin
url = attachment.xpath("download-url").text
File.open(filename, 'wb') do |f|
f.write(get(url, :bare))
end
rescue OpenURI::HTTPError
puts "Error downloading #{url}: #{$!.message}"
end
end
end
end
source 'http://rubygems.org'
gem 'nokogiri'
gem 'progress'
GEM
remote: http://rubygems.org/
specs:
nokogiri (1.5.0)
progress (2.4.0)
PLATFORMS
ruby
DEPENDENCIES
nokogiri
progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment