|
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 |