-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunpublishNotesOnAOs.py
110 lines (100 loc) · 3.94 KB
/
unpublishNotesOnAOs.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
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
print(baseURL)
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
print(session)
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
date = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
f = csv.writer(open('unpublishedNotesOnAOs' + date + '.csv', 'w'))
f.writerow(['uri'] + ['aoTitle'] + ['post'])
f2 = csv.writer(open('errorsUnpublishedNotesOnAOs' + date + '.csv', 'w'))
f2.writerow(['uri'] + ['post'])
page = 0
results = ''
while results != []:
page += 1
endpoint = ('search?q=*&page=' + str(page)
+ '&page_size=100&type[]=archival_object')
output = requests.get(baseURL + '/repositories/2/' + endpoint,
headers=headers).json()
results = output['results']
print(output['last_page'] - output['this_page'], output['total_hits'])
for result in results:
aoUri = result['uri']
aoTitle = result['title']
jsonString = json.loads(result['json'])
publish = jsonString['publish']
level = jsonString['level']
notes = jsonString['notes']
processinfo = False
acqinfo = False
for note in notes:
if 'type' in note:
if note['type'] == 'processinfo':
for subnote in note['subnotes']:
if subnote['publish'] is True:
processinfo = True
elif note['type'] == 'acqinfo':
if note['publish'] is True:
acqinfo = True
if processinfo is True or acqinfo is True:
output = requests.get(baseURL + aoUri, headers=headers)
output = output.json()
updatedAO = output
aoTitle = updatedAO['title']
editedNotes = updatedAO['notes']
for editedNote in editedNotes:
postAO = False
if 'type' in note:
if editedNote['type'] == 'processinfo':
editedSubnotes = editedNote['subnotes']
for editedSubnote in editedSubnotes:
if editedSubnote['publish'] is True:
editedSubnote['publish'] = False
postAO = True
editedNote['subnotes'] = editedSubnotes
elif editedNote['type'] == 'acqinfo':
if editedNote['publish'] is True:
editedNote['publish'] = False
postAO = True
if postAO is True:
updatedAO['notes'] = editedNotes
updatedAO = json.dumps(updatedAO)
aoPost = requests.post(baseURL + aoUri, headers=headers,
data=updatedAO)
print(aoPost.status_code)
if aoPost.status_code != 200:
f2.writerow([id] + [aoPost])
print('error')
else:
aoPost = aoPost.json()
aoPost = json.dumps(aoPost)
print(aoPost)
f.writerow([aoUri] + [aoTitle] + [aoPost])
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))