blob: a1ae3282b81347d685ae528ffc0dee69538b4b05 [file] [log] [blame]
import requests
import shutil
import pathlib
import os.path
# import pprint
# import argparse
API_ENDPOINT = '{}?public_key={}'
def save_file(url, save_path):
r = requests.get(url, stream=True)
# TODO: verify size, checksum and re-download in necessary
with open(save_path, 'wb') as f:
shutil.copyfileobj(r.raw, f)
def recurse(url, save_path):
pathlib.Path(save_path).mkdir(parents=True, exist_ok=True)
items = requests.get(API_ENDPOINT.format('', url)).json()['_embedded']['items']
for i in items:
# pprint.pprint(i)
new_path = os.path.join(save_path, i['name'])
if 'file' in i:
print('file {}'.format(new_path))
save_file(i['file'], new_path)
print('folder {}'.format(new_path))
recurse(i['public_url'], new_path)
target_root = ''
recurse(target_root, 'dl')