Created
March 13, 2023 11:57
-
-
Save prio/e40b01262ed696c0c8538f1fae56a3b8 to your computer and use it in GitHub Desktop.
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
HOST = '' | |
headers = { | |
'Accept': "application/json", | |
'Content-Type': "application/json", | |
'Authorization': f'Bearer {get_token()}' | |
} | |
# Just runs Export atm | |
def main(command: str, directory: str): | |
"Runs outline export and either downloads the zip (default) or extracts it to dir" | |
endpoint = '/api/collections.export_all' | |
op = requests.post(f'{HOST}{endpoint}', headers=headers).json() | |
if not op['ok']: | |
print(f'Export failed: {op}') | |
return | |
endpoint = '/api/fileOperations.info' | |
done = False | |
while not done: | |
fop = requests.post(f'{HOST}{endpoint}', headers=headers, json={'id': op['data']['fileOperation']['id']}).json() | |
if not fop['ok']: | |
print(f'Export failed: {fop}') | |
return | |
done = fop['data']['state'] == 'complete' | |
time.sleep(1) | |
endpoint = '/api/fileOperations.redirect' | |
zipbytes = BytesIO() | |
with requests.post(f'{HOST}{endpoint}', headers=headers, json={'id': op['data']['fileOperation']['id']}, stream=True) as r: | |
r.raise_for_status() | |
for chunk in r.iter_content(chunk_size=8192): | |
zipbytes.write(chunk) | |
ZipFile(zipbytes).extractall(directory) | |
endpoint = '/api/fileOperations.delete' | |
dop = requests.post(f'{HOST}{endpoint}', headers=headers, json={'id': op['data']['fileOperation']['id']}).json() | |
if not dop['ok']: | |
print(f'Export deletion failed: {fop}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment