Epidemiology & Technology

ODK Central Backup using Python

We have already seen how to get an access token using Python requests library for ODK central. Now, let us see how we can use that to download our backup file

import requests

TOKEN = 'TOKEN'
BASEURL = 'https://url.com/v1/backup'
headers = {
    'Authorization': 'Bearer ' + TOKEN,
}
response = requests.get(BASEURL, headers=headers, stream=True)
print(response.headers)

Content_Disposition = response.headers.get('Content-Disposition')
responseContentType = response.headers.get('Content-Type')

x = Content_Disposition.split("=")
responseFileName = str(x[1])
responseFileName = responseFileName.replace('"', '')
responseFileName = responseFileName.replace(':', '')

print(responseContentType)
print(responseFileName)

if responseContentType == 'application/zip':
    # 200 MB chunk size
    with open(responseFileName, mode="wb") as file:
        for chunk in response.iter_content(chunk_size=200 * 1024 * 1024):
            file.write(chunk)

Code language: Python (python)

Related Posts