From b3e3901266e4cf81aba0b4020f2e5aee65cd7387 Mon Sep 17 00:00:00 2001 From: mjanowiecki <32551917+mjanowiecki@users.noreply.github.com> Date: Thu, 13 Dec 2018 14:46:21 -0500 Subject: [PATCH] add new script and update readme --- README.md | 3 ++ postContainerLinksToRecordsFromCSV.py | 50 +++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 postContainerLinksToRecordsFromCSV.py diff --git a/README.md b/README.md index 6a8d519..9642f08 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,9 @@ Creates instances (consisting of top_containers) from a separate CSV file. The C #### [postContainerLinksToRecords.py](/postContainerLinksToRecords.py) Based on user input, posts containers to a specified record based on a specified CSV file. +#### [postContainerLinksToRecordsFromCSV.py](/postContainerLinksToRecordsFromCSV.py) +Based on user input, posts containers to a specified record based on a specified CSV file of top container and resource URIs. + #### [postCorporateAgentsFromCSV.py](/postCorporateAgentsFromCSV.py) Based on user input, posts corporate agents based on a specified CSV file. diff --git a/postContainerLinksToRecordsFromCSV.py b/postContainerLinksToRecordsFromCSV.py new file mode 100644 index 0000000..c79de71 --- /dev/null +++ b/postContainerLinksToRecordsFromCSV.py @@ -0,0 +1,50 @@ +import json +import requests +import secrets +import csv + +secretsVersion = raw_input('To edit production server, enter the name of the secrets file: ') +if secretsVersion != '': + try: + secrets = __import__(secretsVersion) + print 'Editing Production' + except ImportError: + print 'Editing Development' +else: + print 'Editing Development' + +targetFile = raw_input('Enter file name: ') + +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'} + +csvfile = csv.DictReader(open(targetFile)) + +f=csv.writer(open('containerLinksPostedFromCSV.csv', 'wb')) +f.writerow(['topContainer']+['resource']+['post']) + +for row in csvfile: + uri = row['uri'] + resourceUri = row['resourceuri'] + print baseURL+resourceUri + asRecord = requests.get(baseURL+resourceUri, headers=headers).json() + instanceArray = asRecord['instances'] + top_container = {} + top_container['ref'] = uri + sub_container = {} + sub_container['top_container'] = top_container + instance = {} + instance['sub_container'] = sub_container + instance['instance_type'] = 'mixed_materials' + instanceArray.append(instance) + asRecord['instances'] = instanceArray + asRecord = json.dumps(asRecord) + post = requests.post(baseURL+resourceUri, headers=headers, data=asRecord).json() + print post + f.writerow([uri]+[resourceUri]+[post])