Skip to content

Instantly share code, notes, and snippets.

@lassoan
Created September 25, 2017 13:49
Show Gist options
  • Save lassoan/1188438b056e3717d56812bc5218705b to your computer and use it in GitHub Desktop.
Save lassoan/1188438b056e3717d56812bc5218705b to your computer and use it in GitHub Desktop.
Download all files from Assembla (Files tool - including all ticket and wiki attachments)
##################
## Written by: Kyle Sunderland
##
## Downloads all files from an assembla project
## Need to change repo_name, api_key, and api_secret for the script to work
##################
$repo_name = "REPO_NAME" # Name of the repo
# personal keys found at: https://app.assembla.com/user/edit/manage_clients
$api_key = "API_KEY" # Assembla api key
$api_secret = "API_SECRET" # Assembla api secret
$output_directory = $pwd.Path + "\" + $repo_name + "\"
If(!(Test-Path $output_directory))
{
mkdir $output_directory
}
$current_page=1
$per_page=100
$base_uri = "https://api.assembla.com/v1/spaces/" + $repo_name + "/documents.json"
$base_uri += "?per_page=" + $per_page
$web_client = New-Object System.Net.WebClient
$web_client.Headers["X-Api-Key"] = $api_key
$web_client.Headers["X-Api-Secret"] = $api_secret
$headers = @{}
$headers.Add("X-Api-Key", $api_key)
$headers.Add("X-Api-Secret", $api_secret)
$number_of_files = 0
$file_urls = New-Object System.Collections.ArrayList
$file_names = New-Object System.Collections.ArrayList
Write-Host "Getting file list..."
# Collect the file names
Do
{
$continue = 1
# Get the current page of files
$uri = $base_uri + "&page=" + $current_page
$response = Invoke-WebRequest $uri -Headers $headers
#Success
If($response.StatusCode -eq 200)
{
$document_object = $response | ConvertFrom-Json
$number_of_documents = $document_object.Count
for ($i=0; $i -lt $number_of_documents; ++$i)
{
$number_of_files = $file_urls.Add($document_object[$i].url)
$number_of_files = $file_names.Add($document_object[$i].name)
}
++$current_page
}
# Too many requests. Pause for 15 seconds
ElseIf($response.StatusCode -eq 429)
{
Write-Host "Too many requests..."
Start-Sleep -s 15
}
# No more files
ElseIf($response.StatusCode -eq 204)
{
$continue = 0
}
}While($continue)
$number_of_files = $file_names.Count
Write-Host "..." $number_of_files "files"
$n = $file_names | Select-Object -Unique
For ($i=0; $i -lt $number_of_files; ++$i)
{
$document_url = $file_urls[$i]
$document_name = $file_names[$i]
$output_file = $output_directory + $document_name
$print_string = "(" + ($i+1) + "/" + $number_of_files + ") Downloading: " + $document_name
Write-Host $print_string
$temp_file = $output_file
$file_num = 1
While (Test-Path $temp_file)
{
$temp_file = $output_directory + (Get-Item $output_file).BaseName + "($file_num)" + (Get-Item $output_file).Extension
++$file_num
}
$output_file = $temp_file
# Download the file
$web_client.DownloadFile($document_url, $output_file)
#slower alternative
#$response = Invoke-WebRequest $document_url -Headers $headers -OutFile $output_file
}
# Done
[System.Media.SystemSounds]::Asterisk.Play()
Write-Host "Press any key to continue ..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment