-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtransferAoDatesToDos.py
178 lines (167 loc) · 6.24 KB
/
transferAoDatesToDos.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import json
import requests
import time
import csv
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
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
print('authenticated')
endpoint = '/repositories/' + repository + '/archival_objects?all_ids=true'
ids = requests.get(baseURL + endpoint, headers=headers).json()
ids.reverse()
print(len(ids))
# Generates a text file of AOs with DOs. Takes 2+ hours to generate so this
# code block is separate so the main portion of the script can be run
# more quickly.
# f = csv.writer(open('archivalObjectsWithDigitalObjects.csv', 'w'))
# f.writerow(['uri'])
# doAos = []
#
# for id in ids:
# endpoint = '/repositories/'+repository+'/archival_objects/'+str(id)
# output = requests.get(baseURL + endpoint, headers=headers).json()
# try:
# dates = output['dates']
# except ValueError:
# dates = ''
# uri = output['uri']
# instances = output['instances']
# for instance in instances:
# if instance['instance_type'] == 'digital_object':
# doUri = instance['digital_object']['ref']
# print(doUri)
# f.writerow([uri])
# doAos.append(uri)
#
# f2=open('archivalObjectsWithDigitalObjectsList.txt', 'w')
# f2.write(json.dumps(doAos))
f = csv.writer(open('DigitalObjectsDatesEdited.csv', 'w'))
f.writerow(['doUri'] + ['oldBegin'] + ['oldEnd'] + ['oldExpression']
+ ['oldLabel'] + ['aoUri'] + ['newBegin'] + ['newEnd']
+ ['newExpression'] + ['newLabel'] + ['post'])
doAos = json.load(open('archivalObjectsWithDigitalObjectsList.txt', 'r'))
for doAo in doAos:
print(doAo)
aoBegin = ''
aoExpression = ''
aoLabel = ''
aoEnd = ''
doBegin = ''
doExpression = ''
doLabel = ''
doEnd = ''
aoOutput = requests.get(baseURL + doAo, headers=headers).json()
try:
aoDates = aoOutput['dates']
for aoDate in aoDates:
try:
aoBegin = aoDate['begin']
except ValueError:
aoBegin = ''
try:
aoEnd = aoDate['end']
except ValueError:
aoEnd = ''
try:
aoExpression = aoDate['expression']
except ValueError:
aoExpression = ''
try:
aoLabel = aoDate['label']
except ValueError:
aoLabel = ''
except ValueError:
aoBegin = ''
aoExpression = ''
aoLabel = ''
aoEnd = ''
try:
instances = aoOutput['instances']
except ValueError:
continue
for instance in instances:
if instance['instance_type'] == 'digital_object':
if aoBegin + aoExpression + aoLabel != '':
doUri = instance['digital_object']['ref']
doOutput = requests.get(baseURL
+ str(doUri), headers=headers).json()
print('moving date from AO to DO')
doDates = doOutput['dates']
if doDates == []:
print('no date', doDates)
doBegin = ''
doExpression = ''
doLabel = ''
doEnd = ''
doDates = []
doDate = {}
doDate['begin'] = aoBegin
doDate['expression'] = aoExpression
doDate['label'] = aoLabel
doDate['date_type'] = 'single'
if aoEnd != '':
doDate['end'] = aoEnd
doDate['date_type'] = 'range'
doDates.append(doDate)
doOutput['dates'] = doDates
output = json.dumps(doOutput)
doPost = requests.post(baseURL + doUri, headers=headers,
data=output).json()
print(doPost)
else:
print('existing date', doDates)
for doDate in doDates:
try:
doBegin = doDate['begin']
except ValueError:
doBegin = ''
try:
doEnd = doDate['end']
except ValueError:
doEnd = ''
try:
doExpression = doDate['expression']
except ValueError:
doExpression = ''
try:
doLabel = doDate['label']
except ValueError:
doLabel = ''
if aoBegin != '':
doDate['begin'] = aoBegin
if aoExpression != '':
doDate['expression'] = aoExpression
if aoLabel != '':
doDate['label'] = aoLabel
if aoEnd != '':
doDate['end'] = aoEnd
doOutput['dates'] = doDates
output = json.dumps(doOutput)
doPost = requests.post(baseURL + doUri, headers=headers,
data=output).json()
print(doPost)
f.writerow([doUri] + [doBegin] + [doEnd] + [doExpression]
+ [doLabel] + [doAo] + [aoBegin] + [aoEnd]
+ [aoExpression] + [aoLabel] + [doPost])
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))