From 457f0006628e9cd79421965d26d33873c005f135 Mon Sep 17 00:00:00 2001 From: mjanowiecki <32551917+mjanowiecki@users.noreply.github.com> Date: Wed, 12 Dec 2018 11:00:49 -0500 Subject: [PATCH] add updateResourceWithCSV --- README.md | 3 +++ updateResourceWithCSV.py | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100644 updateResourceWithCSV.py diff --git a/README.md b/README.md index 5df7ccf..4934b97 100644 --- a/README.md +++ b/README.md @@ -116,3 +116,6 @@ Unpublishes all archival objects associated with the specified resource. Upon ru #### [updateResourceWithAgentOrSubjectLinks.py](/updateResourceWithAgentOrSubjectLinks.py) Based on user input, posts agent or subject links to resources based on a specified CSV file. + +#### [updateResourceWithCSV.py](/updateResourceWithCSV.py) +Based on user input, updates first level (['title']) and second level (['user_defined']['real_1']) elements for resources based on a specified CSV file. diff --git a/updateResourceWithCSV.py b/updateResourceWithCSV.py new file mode 100644 index 0000000..9674ef3 --- /dev/null +++ b/updateResourceWithCSV.py @@ -0,0 +1,56 @@ +import json +import requests +import secrets +import time +import csv + +def firstLevelUpdateFromCSV (key, valueSource): + uri = row['uri'] + value = row[valueSource] + if value != '': + asRecord = requests.get(baseURL+uri, headers=headers).json() + asRecord[key] = value + asRecord = json.dumps(asRecord) + post = requests.post(baseURL + uri, headers=headers, data=asRecord).json() + print post + else: + pass + +def secondLevelUpdateFromCSV (key, valueSource, firstLevel): + uri = row['uri'] + value = row[valueSource] + if value != '': + asRecord = requests.get(baseURL+uri, headers=headers).json() + try: + asRecord[firstLevel][key] = value + except: + asRecord[firstLevel]= {} + asRecord[firstLevel][key] = value + asRecord = json.dumps(asRecord) + post = requests.post(baseURL + uri, headers=headers, data=asRecord).json() + print post + else: + pass + +startTime = time.time() + +baseURL = secrets.baseURL +user = secrets.user +password = secrets.password + +auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json() +session = auth["session"] +headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'} + +filename = raw_input('Enter filename (including \'.csv\'): ') +filename = 'bibNumbers.csv' + +with open(filename) as csvfile: + reader = csv.DictReader(csvfile) + for row in reader: + secondLevelUpdateFromCSV('real_1', 'bib', 'user_defined') + +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)