-
Notifications
You must be signed in to change notification settings - Fork 4
/
fix_names.py
53 lines (42 loc) · 1.58 KB
/
fix_names.py
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
51
52
53
import requests
import math
from progressbar import progressbar
from caltechdata_api import caltechdata_edit
def fix_name(metadata, fixed):
for name in metadata:
if name["nameType"] == "Personal":
if "givenName" not in name:
fixed = True
given = name["name"].split(",")[1]
name["givenName"] = given.strip()
return metadata, fixed
url = 'https://data.caltech.edu/api/records?q=-metadata.related_identifiers.identifier%3A"10.25989%2Fes8t-kswe"'
headers = {
"accept": "application/vnd.datacite.datacite+json",
}
response = requests.get(f"{url}&search_type=scan&scroll=5m")
total = response.json()["hits"]["total"]
pages = math.ceil(int(total) / 1000)
hits = [] # [{'id':'a7f64-a8k10'}]
print(total)
for c in progressbar(range(1, pages + 1)):
chunkurl = f"{url}&sort=newest&size=1000&page={c}"
response = requests.get(chunkurl)
response = response.json()
hits += response["hits"]["hits"]
url = "https://data.caltech.edu/api/records"
for h in progressbar(hits):
idv = str(h["id"])
response = requests.get(f"{url}/{idv}", headers=headers)
if response.status_code != 200:
print(response.text)
exit()
else:
fixed = False
metadata = response.json()
metadata["creators"], fixed = fix_name(metadata["creators"], fixed)
if "contributors" in metadata:
metadata["contributors"], fixed = fix_name(metadata["contributors"], fixed)
if fixed:
print(idv)
caltechdata_edit(idv, metadata, production=True, publish=True)