-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetArchivalObjectRefIdsForResource.py
81 lines (68 loc) · 2.28 KB
/
getArchivalObjectRefIdsForResource.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
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()
def findKey(d, key):
"""Find all instances of key."""
if key in d:
yield d[key]
for k in d:
if isinstance(d[k], list) and k == 'children':
for i in d[k]:
for j in findKey(i, key):
yield j
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'}
resourceID = input('Enter resource ID: ')
f = csv.writer(open('archivalObjectRefIdForResource.csv', 'w'))
f.writerow(['title'] + ['uri'] + ['ref_id'] + ['dateExpression']
+ ['dataBegin'] + ['level'])
endpoint = '/repositories/' + repository + '/resources/' + resourceID + '/tree'
output = requests.get(baseURL + endpoint, headers=headers).json()
archivalObjects = []
for value in findKey(output, 'record_uri'):
print(value)
if 'archival_objects' in value:
archivalObjects.append(value)
print('downloading aos')
for archivalObject in archivalObjects:
output = requests.get(baseURL + archivalObject, headers=headers).json()
print(json.dumps(output))
title = output['title']
uri = output['uri']
ref_id = output['ref_id']
level = output['level']
for date in output['dates']:
try:
dateExpression = date['expression']
except ValueError:
dateExpression = ''
try:
dateBegin = date['begin']
except ValueError:
dateBegin = ''
f.writerow([title] + [uri] + [ref_id] + [dateExpression] + [dateBegin]
+ [level])
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))