-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadd_files_authors.py
More file actions
50 lines (47 loc) · 1.23 KB
/
add_files_authors.py
File metadata and controls
50 lines (47 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import requests, os, argparse
from caltechdata_api import write_files_rdm
parser = argparse.ArgumentParser(
description="Add files to an existing CaltechAUTHORS record."
)
parser.add_argument(
"idv",
type=str,
help="The CaltechAUTHORS record idv to edit.",
)
parser.add_argument(
"files",
type=str,
nargs="+",
help="The files to upload to the record.",
)
args = parser.parse_args()
idv = args.idv
files = args.files
token = os.environ["RDMTOK"]
url = "https://authors.library.caltech.edu"
headers = {
"Authorization": "Bearer %s" % token,
"Content-type": "application/json",
}
f_headers = {
"Authorization": "Bearer %s" % token,
"Content-type": "application/octet-stream",
}
existing = requests.get(
url + "/api/records/" + idv + "/draft",
headers=headers,
)
if existing.status_code != 200:
raise Exception(f"Record {idv} does not exist, cannot edit")
data = existing.json()
data["files"] = {"enabled": True}
# Update metadata
result = requests.put(
url + "/api/records/" + idv + "/draft",
headers=headers,
json=data,
)
if result.status_code != 200:
raise Exception(result.text)
file_link = result.json()["links"]["files"]
write_files_rdm(files, file_link, headers, f_headers)