-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpublishAgents.py
74 lines (64 loc) · 2.28 KB
/
publishAgents.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
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:
secrets = __import__('secrets')
print('Editing Development')
startTime = time.time()
baseURL = secrets.baseURL
user = secrets.user
password = secrets.password
repository = secrets.repository
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
agentTypes = ['people', 'families', 'corporate_entities', 'software']
date = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
f = csv.writer(open('publishedAgents' + date + '.csv', 'w'))
f.writerow(['uri'] + ['post'])
f2 = csv.writer(open('errorsAgents' + date + '.csv', 'w'))
f2.writerow(['uri'] + ['post'])
for agentType in agentTypes:
endpoint = '/agents/' + agentType + '?all_ids=true'
ids = requests.get(baseURL + endpoint, headers=headers).json()
counter = 0
for id in ids:
counter += 1
print(counter)
id = str(id)
print(baseURL + '/agents/' + agentType + '/' + id)
output = requests.get(baseURL + '/agents/' + agentType + '/' + id,
headers=headers)
output = output.json()
output['publish'] = True
asRecord = json.dumps(output)
post = requests.post(baseURL + '/agents/' + agentType + '/' + id,
headers=headers, data=asRecord)
print(post.status_code)
if post.status_code != 200:
f2.writerow([id] + [post])
print('error')
else:
post = post.json()
post = json.dumps(post)
print(post)
f.writerow(['/agents/' + agentType + '/' + id]
+ [post])
print('row written - updated')
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))