Last active
February 27, 2020 14:06
-
-
Save stelabouras/1318941 to your computer and use it in GitHub Desktop.
Download all your file from Basecamp and organise them in folders
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
<? | |
// make sure the folder of the script is writeble (0777) | |
$basecampUrl = '[YOUR BASECAMP URL HERE]'; // e.g. https://stelabouras.basecamphq.com/ (Don't forget the trailing slash!) | |
$apiKey = '[YOUR API KEY HERE]'; // e.g. one huge string (found in 'My info' in the Authentication tokens section) | |
function BasecampCall($endPoint, $usePrefix = true) { | |
global $apiKey, $basecampUrl; | |
// From: http://prattski.com/2008/10/22/basecamp-api-examples-using-php-and-curl-get/ | |
$session = curl_init(); | |
$username = $apiKey; | |
$password = 'X'; | |
curl_setopt($session, CURLOPT_URL, ($usePrefix == true ? $basecampUrl : "") . $endPoint); | |
curl_setopt($session, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |
curl_setopt($session, CURLOPT_HTTPGET, 1); | |
curl_setopt($session, CURLOPT_HEADER, false); | |
curl_setopt($session, CURLOPT_HTTPHEADER, array('Accept: application/xml', 'Content-Type: application/xml')); | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($session,CURLOPT_USERPWD,$username . ":" . $password); | |
if(ereg("^(https)",$request)) curl_setopt($session,CURLOPT_SSL_VERIFYPEER,false); | |
$response = curl_exec($session); | |
curl_close($session); | |
return ($usePrefix ? simplexml_load_string($response) : $response); | |
} | |
$projects = BasecampCall('projects.xml'); | |
// For each project take name and id | |
foreach($projects->project as $proj) { | |
$pr = array( | |
"id" => (string)$proj->id, | |
"name" => (string)$proj->name | |
); | |
$n = 0; | |
$i = 0; | |
// Retrieve the attachments | |
echo "\nSaving attachments for project: " . $pr['name'] . "...\n"; | |
@mkdir($pr['name']); | |
$filesArray = array(); | |
do { | |
$attachments = BasecampCall("projects/" . $proj->id . "/attachments.xml?n=" . $n); | |
if(count($attachments->attachment) > 0) { | |
foreach($attachments->attachment as $attachment) { | |
$file = pathinfo($attachment->name); | |
@file_put_contents($pr['name'] . "/" . $file['filename'] . (in_array($file['filename'], $filesArray) ? "-" . rand() : "") . "." . $file['extension'], BasecampCall($attachment->{'download-url'}, false)); | |
$filesArray[] = $file['filename']; | |
echo "Saving file " . $attachment->name . "...\n"; | |
} | |
} | |
$n += 100; | |
} while(count($attachments->attachment) == 100); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run in from console with: php basecamp.php