From b7877a3bf2ebb23050bfbdc7a807b76ec85ce9ba Mon Sep 17 00:00:00 2001 From: Lars Holm Nielsen Date: Sun, 15 Oct 2023 14:20:35 +0200 Subject: [PATCH] scripts: add script to clear pending files --- scripts/admin/clear_pending_files.py | 33 ++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 scripts/admin/clear_pending_files.py diff --git a/scripts/admin/clear_pending_files.py b/scripts/admin/clear_pending_files.py new file mode 100644 index 00000000..d844ad44 --- /dev/null +++ b/scripts/admin/clear_pending_files.py @@ -0,0 +1,33 @@ +""" +Usage: + export RDMTOK=.... + python3 clear_pending_files.py +""" + +import requests +import os, argparse + +parser = argparse.ArgumentParser(description="Clear stuck files from UI uploader") +parser.add_argument("ids", nargs="*", help="Record IDs to clear") + +args = parser.parse_args() + +# Get access token as environment variable +token = os.environ["RDMTOK"] + +url = "https://zenodo.org/api/records" + +headers = { + "Authorization": "Bearer %s" % token, + "Content-type": "application/json", +} + +for idv in args.ids: + response = requests.get(f"{url}/{idv}/draft/files", headers=headers) + entries = response.json()["entries"] + for entry in entries: + if entry["status"] == "pending": + response = requests.delete(entry["links"]["self"], headers=headers) + if response.status_code != 204: + print(response.text) + exit()