Skip to content

Instantly share code, notes, and snippets.

@threedaymonk
Created June 14, 2011 14:52
Show Gist options
  • Save threedaymonk/1025049 to your computer and use it in GitHub Desktop.
Save threedaymonk/1025049 to your computer and use it in GitHub Desktop.
Quick and dirty script to download all the files from a Basecamp account.
require "nokogiri"
require "time"
require "fileutils"
url = "https://whatever.basecamphq.com"
api_key = YOUR_API_KEY
curl_options = "-u #{api_key}:X -H 'Accept: application/xml' -H 'Content-Type: application/xml'"
def sanitize(s)
s.downcase.gsub(/[^a-z0-9\.]+/, "_")
end
xml = `curl #{curl_options} #{url}/projects.xml`
doc = Nokogiri::XML(xml)
projects = doc.xpath("//project").map{ |project|
[project.xpath("id").first.text, project.xpath("name").first.text]
}
projects.each do |project, pname|
dir = sanitize(pname)
puts dir
FileUtils.mkdir_p dir
Dir.chdir dir do
offset = 0
loop do
xml = `curl #{curl_options} #{url}/projects/#{project}/attachments.xml?n=#{offset}`
doc = Nokogiri::XML(xml)
attachments = doc.xpath("//attachment")
attachments.each do |attachment|
datetime = Time.parse(attachment.xpath("created-on").first.text)
name = attachment.xpath("name").first.text
download = attachment.xpath("download-url").first.text
filename = sanitize(name)
puts filename
system %{curl #{curl_options} -o "#{filename}" "#{download}"}
end
if attachments.any?
offset += attachments.length
else
break
end
end
end
end
@alxrogan
Copy link

alxrogan commented Oct 4, 2011

Excellent script, I just needed to add in the rubygems requirement and add quotes around the API key. Thanks much!

require "rubygems"
...
api_key = "YOUR_API_KEY"

@shinyidol
Copy link

I'm getting the following error. Any idea?

basecamp.rb:15:in ``': No such file or directory - curl -u [api_key]:X -H 'Accept: application/xml' -H 'Content-Type: application/xml' [url] (Errno::ENOENT)
from basecamp.rb:15:in`

'

@threedaymonk
Copy link
Author

Do you have curl installed?

@shinyidol
Copy link

doh. installed curl. but not having ssl host issues.

curl: (6) Could not resolve host: 'Accept; No data record of requested type
curl: (6) Could not resolve host: application; Host not found
curl: (6) Could not resolve host: 'Content-Type; No data record of requested type
curl: (6) Could not resolve host: application; Host not found

not a huge deal. just trying to save some right click save silliness.

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