|
1 | 1 | import requests |
2 | 2 | import math |
3 | 3 | from progressbar import progressbar |
| 4 | +from caltechdata_api import caltechdata_edit |
| 5 | + |
| 6 | +def fix_name(metadata): |
| 7 | + fixed = False |
| 8 | + for name in metadata: |
| 9 | + if name['nameType'] == 'Personal': |
| 10 | + if 'givenName' not in name: |
| 11 | + fixed = True |
| 12 | + given = name['name'].split(',')[1] |
| 13 | + name['givenName'] = given.strip() |
| 14 | + return metadata,fixed |
4 | 15 |
|
5 | 16 | url = "https://data.caltech.edu/api/records" |
6 | 17 |
|
7 | 18 | headers = { |
8 | 19 | "accept": "application/vnd.datacite.datacite+json", |
9 | 20 | } |
10 | 21 |
|
11 | | -response = requests.get(f"{url}") |
| 22 | +response = requests.get(f"{url}?search_type=scan&scroll=5m") |
12 | 23 |
|
13 | 24 | total = response.json()["hits"]["total"] |
14 | 25 | pages = math.ceil(int(total) / 1000) |
|
18 | 29 | chunkurl = ( |
19 | 30 | f"{url}?&sort=newest&size=1000&page={c}" |
20 | 31 | ) |
21 | | - response = requests.get(chunkurl).json() |
| 32 | + response = requests.get(chunkurl) |
| 33 | + response = response.json() |
22 | 34 |
|
23 | 35 | hits += response["hits"]["hits"] |
24 | 36 |
|
25 | 37 | for h in progressbar(hits): |
26 | | - rid = str(h["id"]) |
27 | | - print(rid) |
| 38 | + idv = str(h["id"]) |
| 39 | + response = requests.get(f'{url}/{idv}', headers=headers) |
| 40 | + if response.status_code != 200: |
| 41 | + print(response.text) |
| 42 | + exit() |
| 43 | + else: |
| 44 | + metadata = response.json() |
| 45 | + metadata['creators'], fixed = fix_name(metadata['creators']) |
| 46 | + if 'contributors' in metadata: |
| 47 | + metadata['contributors'] = fix_name(metadata['contributors']) |
| 48 | + if fixed: |
| 49 | + print(idv) |
| 50 | + caltechdata_edit(idv,metadata,production=True,publish=True) |
| 51 | + exit() |
0 commit comments