methods to download experiments attachments up and tested
to-do: clean code
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import os, requests
|
||||
from getpass import getpass
|
||||
import elabapi_python as elabapi
|
||||
|
||||
|
||||
@@ -80,16 +81,17 @@ class APIHandler:
|
||||
entry_data = response.json()
|
||||
return entry_data
|
||||
|
||||
def download_all_attachments_data(self, elabid, entryType="experiments"):
|
||||
def download_attachment_data(self, elabid, upload_id, entryType="experiments"):
|
||||
"""
|
||||
Downloads attachments of a certain eLabFTW experiment (default) or item.
|
||||
Only returns their binary data. Use method download_attachments_to_disk to save to file.
|
||||
Downloads a specific attachment of a certain eLabFTW experiment (default) or item.
|
||||
Only returns its binary data. Use method download_attachment_to_disk to save to file.
|
||||
NOTE: Output is a dictionary where:
|
||||
* The keys are the attachments' filenames;
|
||||
* The values are the binary data for those attachments.
|
||||
* The key is the attachment's filename;
|
||||
* The value is the attachment's binary data.
|
||||
|
||||
Args:
|
||||
elabid: eLabFTW internal ID of the selected resource.
|
||||
upload_id: eLabFTW internal ID of the selected upload.
|
||||
entryType: Resource type. Anything other than "experiments" or "items" WILL raise an error.
|
||||
"""
|
||||
if entryType not in ["experiments", "items"]:
|
||||
@@ -108,29 +110,33 @@ class APIHandler:
|
||||
)
|
||||
uploads_api = elabapi.UploadsApi(api_client)
|
||||
|
||||
# Actual uploads (dictionary):
|
||||
uploads = {
|
||||
# Scans through the attachments and selects the one with corresponing ID.
|
||||
attachment = {
|
||||
upload.real_name: uploads_api.read_upload(
|
||||
entryType, elabid, upload.id, format="binary", _preload_content=False
|
||||
entryType, elabid, upload_id, format="binary", _preload_content=False
|
||||
).data
|
||||
for upload in uploads_api.read_uploads(entryType, elabid)
|
||||
if upload.id == upload_id
|
||||
}
|
||||
|
||||
return uploads
|
||||
return attachment
|
||||
|
||||
def download_attachments_to_disk(
|
||||
def download_attachment_to_disk(
|
||||
self,
|
||||
elabid,
|
||||
upload_id,
|
||||
entryType="experiments",
|
||||
dump_dir="output/attachments",
|
||||
# persistent=True,
|
||||
):
|
||||
"""
|
||||
Downloads attachments of a certain eLabFTW experiment (default) or item.
|
||||
Downloads a specific attachment of a certain eLabFTW experiment (default) or item.
|
||||
Downloads their binary data through method download_attachments_data and dumps it to dump_dir.
|
||||
Returns nothing on success.
|
||||
|
||||
Args:
|
||||
elabid: eLabFTW internal ID of the selected resource.
|
||||
upload_id: eLabFTW internal ID of the selected upload.
|
||||
entryType: Resource type. Anything other than "experiments" or "items" WILL raise an error.
|
||||
dump_dir: Directory to which to save the attachments. Default is "output/attachments".
|
||||
persistent: [Unused] Decides if the files will stay on disk after all operations are completed.
|
||||
@@ -142,9 +148,16 @@ class APIHandler:
|
||||
"You can only download attachments from experiments or items."
|
||||
)
|
||||
|
||||
uploads = download_all_attachments_data(elabid, entryType=entryType)
|
||||
uploads = self.download_attachment_data(elabid, upload_id, entryType=entryType)
|
||||
for file in uploads:
|
||||
raw_data = uploads["file"]
|
||||
raw_data = uploads[file]
|
||||
with open(os.path.join(dump_dir, f"exp{elabid}-{file}"), "wb") as f:
|
||||
f.write(raw_data)
|
||||
return
|
||||
|
||||
|
||||
# Testing methods
|
||||
if __name__ == "__main__":
|
||||
api_key = getpass("Paste API key here [no echo]: ")
|
||||
handler = APIHandler(api_key=api_key)
|
||||
handler.download_attachment_to_disk(elabid=58, upload_id=81)
|
||||
|
||||
Reference in New Issue
Block a user