|
| 1 | +#!/usr/bin/env python |
| 2 | +import time |
| 3 | +import datetime |
| 4 | +import elabapi_python |
| 5 | +from elabapi_python.rest import ApiException |
| 6 | + |
| 7 | +######################### |
| 8 | +# CONFIG # |
| 9 | +######################### |
| 10 | +API_HOST_URL = 'https://elab.local:3148/api/v2' |
| 11 | +# replace with your api key |
| 12 | +API_KEY = 'apiKey4Test' |
| 13 | +# number of days to look back |
| 14 | +PERIOD_IN_DAYS = 7 |
| 15 | +######################### |
| 16 | +# END CONFIG # |
| 17 | +######################### |
| 18 | + |
| 19 | +# Configure the api client |
| 20 | +configuration = elabapi_python.Configuration() |
| 21 | +configuration.api_key['api_key'] = API_KEY |
| 22 | +configuration.api_key_prefix['api_key'] = 'Authorization' |
| 23 | +configuration.host = API_HOST_URL |
| 24 | +configuration.debug = False |
| 25 | +configuration.verify_ssl = False |
| 26 | + |
| 27 | +# create an instance of the API class |
| 28 | +api_client = elabapi_python.ApiClient(configuration) |
| 29 | +# fix issue with Authorization header not being properly set by the generated lib |
| 30 | +api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 31 | + |
| 32 | +# create an instance of Experiments and another for Uploads |
| 33 | +experimentsApi = elabapi_python.ExperimentsApi(api_client) |
| 34 | +uploadsApi = elabapi_python.UploadsApi(api_client) |
| 35 | + |
| 36 | +# calculate the date |
| 37 | +today = datetime.date.today() |
| 38 | +date_from = today - datetime.timedelta(days = PERIOD_IN_DAYS) |
| 39 | + |
| 40 | +# look for experiments that are timestamped |
| 41 | +for exp in experimentsApi.read_experiments(extended=f'timestamped:yes timestamped_at:>{date_from}'): |
| 42 | + for upload in uploadsApi.read_uploads('experiments', exp.id): |
| 43 | + # get and save binary file |
| 44 | + now = datetime.datetime.now() |
| 45 | + with open(f'{exp.id}-{exp.elabid}-{now.strftime("%Y-%m-%d_%H-%M-%S")}-timestamp-archive.zip', 'wb') as zipfile: |
| 46 | + # the _preload_content flag is necessary so the api_client doesn't try and deserialize the response |
| 47 | + zipfile.write(uploadsApi.read_upload('experiments', exp.id, upload.id, format='binary', _preload_content=False).data) |
| 48 | + |
0 commit comments