Skip to content

Commit 4177855

Browse files
committed
new script
1 parent 8f999bd commit 4177855

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ Retrieves all of the top containers from a particular repository into a JSON fil
6767
#### [getUrisAndIds.py](getUrisAndIds.py)
6868
For the specified record type, retrieves URI and the 'id_0,' 'id_1,' 'id_2,' 'id_3,' and a concatenated version of all the 'id' fields.
6969

70+
#### [modifyDigitalObjectUrls.py](/modifyDigitalObjectUrls.py)
71+
Based on user input, replaces a string in the URLs in both the 'Identifier' and 'File URI' fields for digital objects across the repository.
72+
7073
#### [postContainersFromCSV.py](/postContainersFromCSV.py)
7174
Creates instances (consisting of top_containers) from a separate CSV file. The CSV file should have two columns, indicator and barcode. The directory where this file is stored must match the directory in the filePath variable. The script will prompt you first for the exact name of the CSV file, and then for the exact resource or accession to attach the containers to.
7275

modifyDigitalObjectUrls.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import json
2+
import requests
3+
import secrets
4+
import time
5+
import csv
6+
import argparse
7+
8+
parser = argparse.ArgumentParser()
9+
parser.add_argument('-1', '--replacedValue', help='the value to be replaced. optional - if not provided, the script will ask for input')
10+
parser.add_argument('-2', '--replacementValue', help='the replacement value. optional - if not provided, the script will ask for input')
11+
args = parser.parse_args()
12+
13+
if args.replacedValue:
14+
replacedValue = args.replacedValue
15+
else:
16+
replacedValue = raw_input('Enter the value to be replaced: ')
17+
if args.replacementValue:
18+
replacementValue = args.replacementValue
19+
else:
20+
replacementValue = raw_input('Enter the replacement value: ')
21+
22+
startTime = time.time()
23+
24+
baseURL = secrets.baseURL
25+
user = secrets.user
26+
password = secrets.password
27+
28+
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
29+
session = auth["session"]
30+
headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'}
31+
print 'authenticated'
32+
33+
endpoint = '/repositories/3/digital_objects?all_ids=true'
34+
35+
ids = requests.get(baseURL + endpoint, headers=headers).json()
36+
print len(ids)
37+
38+
f=csv.writer(open('doUrlEdits.csv', 'wb'))
39+
f.writerow(['uri']+['orignalValue']+['editedValue'])
40+
41+
for id in ids:
42+
endpoint = '/repositories/3/digital_objects/'+str(id)
43+
output = requests.get(baseURL + endpoint, headers=headers).json()
44+
value = output['digital_object_id']
45+
editedValue = value.replace(replacedValue, replacementValue)
46+
output['digital_object_id'] = editedValue
47+
file_versions = output['file_versions']
48+
if value != editedValue:
49+
for file_version in file_versions:
50+
file_version['file_uri'] = editedValue
51+
output['file_versions'] = file_versions
52+
output = json.dumps(output)
53+
doPost = requests.post(baseURL + '/repositories/3/digital_objects/'+str(id), headers=headers, data=output).json()
54+
print doPost
55+
f.writerow([endpoint]+[value]+[editedValue])
56+
57+
58+
elapsedTime = time.time() - startTime
59+
m, s = divmod(elapsedTime, 60)
60+
h, m = divmod(m, 60)
61+
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)

0 commit comments

Comments
 (0)