-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfix_series_number.py
64 lines (56 loc) · 1.84 KB
/
fix_series_number.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
54
55
56
57
58
59
60
61
62
63
import sys,os,csv,json
import requests
from irdm import eprint2rdm, fixup_record, get_record_versions
from caltechdata_api import caltechdata_edit
from ames.harvesters import get_group_records
def fix_series_number(record,rdmid,token):
print(record)
print(rdmid)
response = requests.get(f'https://authors.library.caltech.edu/api/records/{rdmid}')
data = response.json()
custom = {}
if 'custom_fields' in data:
custom = data['custom_fields']
if 'caltech:series' in custom:
print(f"Series already set for {rdmid}")
return
if 'caltech:series_number' in custom:
print(f"Series number already set for {rdmid}")
return
series = record['series'].strip()
custom['caltech:series'] = series
number = record['number'].strip()
if number != 'NULL':
custom['caltech:series_number'] = number
data['custom_fields'] = custom
print(custom)
#print(json.dumps(data,indent=2))
#input("Press Enter to continue...")
caltechdata_edit(
rdmid,
metadata=data,
token=token,
production=True,
publish=True,
authors=True,
)
token = os.environ["CTATOK"]
with open('series_and_number.csv') as infile:
reader = csv.DictReader(infile)
to_update = []
for row in reader:
if row['eprint_status'] =='archive':
to_update.append(row)
eprint_ids = {}
with open('migrated_records.csv') as infile:
reader = csv.DictReader(infile)
for row in reader:
if row['record_status'] =='public':
eprint_ids[row['eprintid']] = row['rdmid']
for record in to_update:
eprintid = record['eprintid']
if eprintid in eprint_ids:
rdmid = eprint_ids[eprintid]
fix_series_number(record,rdmid,token)
else:
print(f"Missing mapping for: {eprintid}")