-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpostPeopleAgentsFromCSV.py
115 lines (105 loc) · 3.22 KB
/
postPeopleAgentsFromCSV.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import json
import requests
import time
import csv
from datetime import datetime
secretsVersion = input('To edit production server, enter the name of the \
secrets file: ')
if secretsVersion != '':
try:
secrets = __import__(secretsVersion)
print('Editing Production')
except ImportError:
secrets = __import__('secrets')
print('Editing Development')
else:
print('Editing Development')
startTime = time.time()
baseURL = secrets.baseURL
user = secrets.user
password = secrets.password
repository = secrets.repository
targetFile = input('Enter file name: ')
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
date = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
f = csv.writer(open('postNewPersonalAgents' + date + '.csv', 'w'))
f.writerow(['sortName'] + ['uri'])
csvfile = csv.DictReader(open(targetFile))
for row in csvfile:
agentRecord = {}
names = []
name = {}
name['primary_name'] = row['primaryName']
name['name_order'] = 'inverted'
name['jsonmodel_type'] = 'name_person'
name['rules'] = 'rda'
name['sort_name'] = row['sortName']
try:
name['authority_id'] = row['authorityID']
name['source'] = 'viaf'
except ValueError:
pass
try:
name['rest_of_name'] = row['restOfName']
except ValueError:
name['name_order'] = 'direct'
try:
name['fuller_form'] = row['fullerForm']
except ValueError:
pass
try:
name['title'] = row['title']
except ValueError:
pass
try:
name['prefix'] = row['prefix']
except ValueError:
pass
try:
name['suffix'] = row['suffix']
except ValueError:
pass
try:
name['dates'] = row['date']
except ValueError:
pass
names.append(name)
if row['date'] != '':
dates = []
date = {}
date['label'] = 'existence'
date['jsonmodel_type'] = 'date'
if row['expression'] != '':
date['expression'] = row['expression']
date['date_type'] = 'single'
elif row['begin'] != '' and row['end'] != '':
date['begin'] = row['begin']
date['end'] = row['end']
date['date_type'] = 'range'
elif row['begin'] != '':
date['begin'] = row['begin']
date['date_type'] = 'single'
elif row['end'] != '':
date['end'] = row['end']
date['date_type'] = 'single'
dates.append(date)
agentRecord['dates_of_existence'] = dates
print(dates)
agentRecord['names'] = names
agentRecord['publish'] = True
agentRecord['jsonmodel_type'] = 'agent_person'
agentRecord = json.dumps(agentRecord)
print(agentRecord)
post = requests.post(baseURL + '/agents/people', headers=headers,
data=agentRecord).json()
print(json.dumps(post))
uri = post['uri']
f.writerow([row['sortName']] + [uri])
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Total script run time: ', '%d:%02d:%02d' % (h, m, s))