Compare commits
2 Commits
07aac3e6b3
...
df927b7c0e
| Author | SHA256 | Date | |
|---|---|---|---|
| df927b7c0e | |||
| ccf74fca26 |
@@ -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)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import os, json, requests
|
||||
from getpass import getpass
|
||||
from APIHandler import APIHandler
|
||||
|
||||
|
||||
@@ -149,8 +150,8 @@ class Layer:
|
||||
def list_attachments(self):
|
||||
"""
|
||||
Returns a dictionary of all the attachments linked to the layer, where:
|
||||
* Each key is the attachment's elabid;
|
||||
* Each value is a dictionary containing the attachment's filename, hashname and related experiment elabid (= self.elabid).
|
||||
* Each key is the attachment's progressive ID (0, 1...);
|
||||
* Each value is a dictionary containing the attachment's elabid, filename, hashname and related experiment elabid (= self.elabid).
|
||||
|
||||
Data is already in layer_data, so the API key is unrequired. Same goes for:
|
||||
* fetch_textual_uploads() - no arguments;
|
||||
@@ -162,7 +163,8 @@ class Layer:
|
||||
if self.uploads == []:
|
||||
return {}
|
||||
attachments = {
|
||||
attachment["id"]: {
|
||||
self.uploads.index(attachment): {
|
||||
"id": attachment["id"],
|
||||
"filename": attachment["real_name"],
|
||||
"hashname": attachment["long_name"],
|
||||
"related_experiment": attachment["item_id"],
|
||||
@@ -329,6 +331,13 @@ class Target(Material):
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
head = APIHandler("MyApiKey-123456789abcdef")
|
||||
print(f"Example header:\n\t{head.header}\n")
|
||||
print("Warning: you're not supposed to be running this as the main program.")
|
||||
# head = APIHandler("MyApiKey-123456789abcdef")
|
||||
# print(f"Example header:\n\t{head.header}\n")
|
||||
# print("Warning: you're not supposed to be running this as the main program.")
|
||||
api_key = getpass("Paste API key here [no echo]: ")
|
||||
handler = APIHandler(api_key=api_key)
|
||||
exp58 = handler.get_entry_from_elabid(elabid=58, entryType="experiments")
|
||||
layer58 = Layer(exp58)
|
||||
print(layer58.list_attachments())
|
||||
print(layer58.fetch_textual_uploads())
|
||||
print(layer58.fetch_images())
|
||||
|
||||
Reference in New Issue
Block a user