methods to download experiments attachments up and tested
to-do: clean code
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
import os, requests
|
import os, requests
|
||||||
|
from getpass import getpass
|
||||||
import elabapi_python as elabapi
|
import elabapi_python as elabapi
|
||||||
|
|
||||||
|
|
||||||
@@ -80,16 +81,17 @@ class APIHandler:
|
|||||||
entry_data = response.json()
|
entry_data = response.json()
|
||||||
return entry_data
|
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.
|
Downloads a specific attachment of a certain eLabFTW experiment (default) or item.
|
||||||
Only returns their binary data. Use method download_attachments_to_disk to save to file.
|
Only returns its binary data. Use method download_attachment_to_disk to save to file.
|
||||||
NOTE: Output is a dictionary where:
|
NOTE: Output is a dictionary where:
|
||||||
* The keys are the attachments' filenames;
|
* The key is the attachment's filename;
|
||||||
* The values are the binary data for those attachments.
|
* The value is the attachment's binary data.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
elabid: eLabFTW internal ID of the selected resource.
|
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.
|
entryType: Resource type. Anything other than "experiments" or "items" WILL raise an error.
|
||||||
"""
|
"""
|
||||||
if entryType not in ["experiments", "items"]:
|
if entryType not in ["experiments", "items"]:
|
||||||
@@ -108,29 +110,33 @@ class APIHandler:
|
|||||||
)
|
)
|
||||||
uploads_api = elabapi.UploadsApi(api_client)
|
uploads_api = elabapi.UploadsApi(api_client)
|
||||||
|
|
||||||
# Actual uploads (dictionary):
|
# Scans through the attachments and selects the one with corresponing ID.
|
||||||
uploads = {
|
attachment = {
|
||||||
upload.real_name: uploads_api.read_upload(
|
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
|
).data
|
||||||
for upload in uploads_api.read_uploads(entryType, elabid)
|
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,
|
self,
|
||||||
elabid,
|
elabid,
|
||||||
|
upload_id,
|
||||||
entryType="experiments",
|
entryType="experiments",
|
||||||
dump_dir="output/attachments",
|
dump_dir="output/attachments",
|
||||||
# persistent=True,
|
# 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.
|
Downloads their binary data through method download_attachments_data and dumps it to dump_dir.
|
||||||
|
Returns nothing on success.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
elabid: eLabFTW internal ID of the selected resource.
|
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.
|
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".
|
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.
|
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."
|
"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:
|
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:
|
with open(os.path.join(dump_dir, f"exp{elabid}-{file}"), "wb") as f:
|
||||||
f.write(raw_data)
|
f.write(raw_data)
|
||||||
return
|
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