Skip to content

Commit 82bf8f8

Browse files
authored
Merge pull request #12 from mjanowiecki/master
add updateResourceWithCSV
2 parents 5ebc405 + 457f000 commit 82bf8f8

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,6 @@ Unpublishes all archival objects associated with the specified resource. Upon ru
116116

117117
#### [updateResourceWithAgentOrSubjectLinks.py](/updateResourceWithAgentOrSubjectLinks.py)
118118
Based on user input, posts agent or subject links to resources based on a specified CSV file.
119+
120+
#### [updateResourceWithCSV.py](/updateResourceWithCSV.py)
121+
Based on user input, updates first level (['title']) and second level (['user_defined']['real_1']) elements for resources based on a specified CSV file.

updateResourceWithCSV.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import json
2+
import requests
3+
import secrets
4+
import time
5+
import csv
6+
7+
def firstLevelUpdateFromCSV (key, valueSource):
8+
uri = row['uri']
9+
value = row[valueSource]
10+
if value != '':
11+
asRecord = requests.get(baseURL+uri, headers=headers).json()
12+
asRecord[key] = value
13+
asRecord = json.dumps(asRecord)
14+
post = requests.post(baseURL + uri, headers=headers, data=asRecord).json()
15+
print post
16+
else:
17+
pass
18+
19+
def secondLevelUpdateFromCSV (key, valueSource, firstLevel):
20+
uri = row['uri']
21+
value = row[valueSource]
22+
if value != '':
23+
asRecord = requests.get(baseURL+uri, headers=headers).json()
24+
try:
25+
asRecord[firstLevel][key] = value
26+
except:
27+
asRecord[firstLevel]= {}
28+
asRecord[firstLevel][key] = value
29+
asRecord = json.dumps(asRecord)
30+
post = requests.post(baseURL + uri, headers=headers, data=asRecord).json()
31+
print post
32+
else:
33+
pass
34+
35+
startTime = time.time()
36+
37+
baseURL = secrets.baseURL
38+
user = secrets.user
39+
password = secrets.password
40+
41+
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
42+
session = auth["session"]
43+
headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'}
44+
45+
filename = raw_input('Enter filename (including \'.csv\'): ')
46+
filename = 'bibNumbers.csv'
47+
48+
with open(filename) as csvfile:
49+
reader = csv.DictReader(csvfile)
50+
for row in reader:
51+
secondLevelUpdateFromCSV('real_1', 'bib', 'user_defined')
52+
53+
elapsedTime = time.time() - startTime
54+
m, s = divmod(elapsedTime, 60)
55+
h, m = divmod(m, 60)
56+
print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s)

0 commit comments

Comments
 (0)