Skip to content

Commit fb9c8a7

Browse files
author
ehanson8
committed
script updates
1 parent e43d8a5 commit fb9c8a7

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

getArchivalObjectsByResource.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,43 @@
55

66
startTime = time.time()
77

8+
def findKey(d, key):
9+
if key in d:
10+
yield d[key]
11+
for k in d:
12+
if isinstance(d[k], list):
13+
for i in d[k]:
14+
for j in findKey(i, key):
15+
yield j
16+
817
baseURL = secrets.baseURL
918
user = secrets.user
1019
password = secrets.password
1120

21+
resourceID= raw_input('Enter resource ID: ')
22+
1223
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
1324
session = auth["session"]
14-
headers = {'X-ArchivesSpace-Session':session}
25+
headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'}
26+
27+
endpoint = '/repositories/3/resources/'+resourceID+'/tree'
1528

16-
resourceNumber = '1051'#Update 'resourceNumber' with the resource number for which you wish to find all of the archival objects
17-
search ='\"/repositories/3/resources/'+resourceNumber+'\"'
18-
payload = {'page': '1', 'page_size': '5000', 'q': search, 'type[]': 'archival_object'}
29+
output = requests.get(baseURL + endpoint, headers=headers).json()
1930

20-
search = requests.get(baseURL+'/search', headers=headers, params=payload).json()
21-
arrayURI = []
31+
archivalObjects = []
32+
for value in findKey(output, 'record_uri'):
33+
if 'archival_objects' in value:
34+
archivalObjects.append(value)
2235

23-
for i in range (0, len (search['results'])):
24-
uri = search['results'][i]['uri']
25-
arrayURI.append(uri)
36+
records = []
37+
for archivalObject in archivalObjects:
38+
output = requests.get(baseURL + archivalObject, headers=headers).json()
39+
records.append(output)
2640

27-
f=open('asSearchResults.json', 'w')
28-
arrayJSON = []
29-
for j in arrayURI:
30-
output = requests.get(baseURL+j, headers=headers).json()
31-
arrayJSON.append(output)
41+
f=open('archivalObjects.json', 'w')
42+
json.dump(records, f)
43+
f.close()
3244

33-
json.dump(arrayJSON, f)
3445
elapsedTime = time.time() - startTime
3546
m, s = divmod(elapsedTime, 60)
3647
h, m = divmod(m, 60)

0 commit comments

Comments
 (0)