This function will bundle files into downloadable zips. This includes retrieving files from the archive
Data must be provided
fileids: Database ids of files to be bundled (list of integers)
#Get token
api_url = "https://arisemdsvm.science.uva.nl//api-token-auth/"
response = requests.post(api_url,{"username":username,"password":password})
token=response.json()['token']
#Get files based on a query
api_url = "http://arisemds.science.uva.nl/api/File/"
response = requests.get(api_url,
headers={'Authorization': 'token {}'.format(token)},
params={"extrainfo":"test__foo"}
)
#Get file IDs from this query
allids=[x['id'] for x in response.json()]
#pass file IDs to bundle creator
api_url = "https://arisemdsvm.science.uva.nl//api/CreateDatabundle/"
response = requests.post(api_url,
headers={'Authorization': 'token {}'.format(token)},
data={"fileids":allids
}
)
result=response.json()
#get the names of the bundles that will be created
bundlenames=result['bundlenames']
result={}
#repeat query about whether these bundles are ready until all the bundles are ready
while len(result)!=len(bundlenames):
time.sleep(30)
api_url = "https://arisemdsvm.science.uva.nl//api/Databundle/"
response = requests.get(api_url,
headers={'Authorization': 'token {}'.format(token)},
params={"bundlename__in":bundlenames,
"ready":True}
)
result=response.json()
#get urls
urls=[x['fileurl'] for x in result]
#download each url
for url in urls:
fullurl="https://arisemdsvm.science.uva.nl/"+url
r = requests.get(fullurl, stream = True)
with open(url.split("/")[-1],"wb") as f:
for chunk in r.iter_content(chunk_size=1024):
# writing one chunk at a time
if chunk:
f.write(chunk)
#on completion, delete these data bundles
api_url = "https://arisemdsvm.science.uva.nl//api/Databundle/"
response = requests.delete(api_url,
headers={'Authorization': 'token {}'.format(token)},
params={"bundlename__in":bundlenames,"ready":True}
)
URL : api/CreateDatabundle/
Method : PUT
DELETE
Auth required : YES
Permissions required : None
```