|
| 1 | +#!/usr/bin/env python |
| 2 | +import datetime |
| 3 | + |
| 4 | +############### |
| 5 | +# DESCRIPTION # |
| 6 | +############## |
| 7 | +# In this script, we list experiments from a user and save the recently modified into a PDF |
| 8 | +############## |
| 9 | + |
| 10 | +# the python library for elabftw |
| 11 | +import elabapi_python |
| 12 | + |
| 13 | +######################### |
| 14 | +# CONFIG # |
| 15 | +######################### |
| 16 | +# replace with the URL of your instance |
| 17 | +API_HOST_URL = 'https://elab.local:3148/api/v2' |
| 18 | +# replace with your api key |
| 19 | +API_KEY = 'apiKey4Test' |
| 20 | +# number of days to look back |
| 21 | +PERIOD_IN_DAYS = 7 |
| 22 | +######################### |
| 23 | +# END CONFIG # |
| 24 | +######################### |
| 25 | + |
| 26 | +# Configure the api client |
| 27 | +configuration = elabapi_python.Configuration() |
| 28 | +configuration.api_key['api_key'] = API_KEY |
| 29 | +configuration.api_key_prefix['api_key'] = 'Authorization' |
| 30 | +configuration.host = API_HOST_URL |
| 31 | +configuration.debug = False |
| 32 | +configuration.verify_ssl = False |
| 33 | + |
| 34 | +# create an instance of the API class |
| 35 | +api_client = elabapi_python.ApiClient(configuration) |
| 36 | +# fix issue with Authorization header not being properly set by the generated lib |
| 37 | +api_client.set_default_header(header_name='Authorization', header_value=API_KEY) |
| 38 | + |
| 39 | +#### SCRIPT START ################## |
| 40 | + |
| 41 | +# Load the experiments api |
| 42 | +experimentsApi = elabapi_python.ExperimentsApi(api_client) |
| 43 | + |
| 44 | +# calculate the date |
| 45 | +today = datetime.date.today() |
| 46 | +date_from = today - datetime.timedelta(days = PERIOD_IN_DAYS) |
| 47 | + |
| 48 | +# get a list of experiments for user with ID:2 |
| 49 | +# OWNER: the target userid. Note: using owner keyword requires elabapi 0.6.0 |
| 50 | +# LIMIT: the max number of items to return. Use a high limit so we get all at once |
| 51 | +# EXTENDED: this is the content of the advanced search, here we filter on timestamped entries during last week |
| 52 | +results = experimentsApi.read_experiments(owner=2, limit=9999, extended=f'timestamped:yes timestamped_at:>{date_from}') |
| 53 | +for exp in results: |
| 54 | + now = datetime.datetime.now() |
| 55 | + filename = f'{exp.id}-{exp.elabid}-{now.strftime("%Y-%m-%d_%H-%M-%S")}-export.pdf' |
| 56 | + print(f'Saving file {filename}') |
| 57 | + with open(filename, 'wb') as file: |
| 58 | + # the _preload_content flag is necessary so the api_client doesn't try and deserialize the response |
| 59 | + file.write(experimentsApi.get_experiment(exp.id, format='pdf', _preload_content=False).data) |
0 commit comments