Skip to content

Commit 8936835

Browse files
authored
Merge pull request #1 from ehanson8/master
merge
2 parents 173507a + dea317f commit 8936835

File tree

2 files changed

+60
-9
lines changed

2 files changed

+60
-9
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
Python scripts used to perform various tasks with the ArchivesSpace API
22

3-
##Authenticating to the API
3+
## Authenticating to the API
44

55
All of these scripts require a secrets.py file in the same directory that must contain the following text:
66

77
baseURL='[ArchivesSpace API URL]'
88
user='[user name]'
99
password='[password]'
1010

11-
##Scripts
11+
## Scripts
1212

13-
####[getResources.py](/getResources.py)
13+
#### [getResources.py](/getResources.py)
1414
This GET scripts retrieves all of the resources from a particular repository into a JSON file which is specified in variable 'f' on line 16. This GET script can be adapted to other record types by editing the 'endpoint' variable on line 13 (e.g. 'repositories/[repo ID]/accessions' or 'agents/corporate_entities').
1515

16-
####[getSingleRecord.py](/getSingleRecord.py)
16+
#### [getSingleRecord.py](/getSingleRecord.py)
1717
This GET script retrieves a single ArchivesSpace record based on the record's 'uri,' which is specified in the 'endpoint' variable on line 13.
1818

19-
####[postNew.py](/postNew.py)
19+
#### [postNew.py](/postNew.py)
2020
This POST script will post new records to a generic API endpoint based the record type, 'agents/people' in this example. This script can be modified to accommodate other data types (e.g. 'repositories/[repo ID]/resources' or 'agents/corporate_entities'). It requires a properly formatted JSON file (specified where [JSON File] appears in the 'records' variable on line 13) for the particular ArchivesSpace record type you are trying to post.
2121

22-
####[postOverwrite.py](/postOverwrite.py)
22+
#### [postOverwrite.py](/postOverwrite.py)
2323
This POST script will overwrite existing ArchivesSpace records based the 'uri' and can be used with any ArchivesSpace record type (e.g. resource, accession, subject, agent_people, agent_corporate_entity, archival_object, etc.). It requires a properly formatted JSON file (specified where [JSON File] appears in the 'records' variable on line 13) for the particular ArchivesSpace record type you are trying to post.
2424

25-
####[getPropertiesFromAgentsPeopleCSV.py](/getPropertiesFromAgentsPeopleCSV.py)
25+
#### [getPropertiesFromAgentsPeopleCSV.py](/getPropertiesFromAgentsPeopleCSV.py)
2626
This GET script retrieves specific properties from the JSON of ArchivesSpace agent_people records into a CSV file which is specified in variable 'f' on line 17. In this example, the script retrieves the 'uri,' 'sort_name,' 'authority_id,' and 'names' properties from the JSON records by iterating through the JSON records with the function 'for i in range (...)' on line 19. The f.writerow(....) function on line 20 specifies which properties are retrieved from the JSON and the f.writerow(....) on line 18 specifies header row of the CSV file.
2727

28-
####[getArrayPropertiesFromAgentsPeopleCSV.py](/getArrayPropertiesFromAgentsPeopleCSV.py)
28+
#### [getArrayPropertiesFromAgentsPeopleCSV.py](/getArrayPropertiesFromAgentsPeopleCSV.py)
2929
This GET script retrieves specific properties, including proprerties that have arrays as values, from the JSON of ArchivesSpace agent_people records. In this example, the 'dates_of existence' property contains an array that must be iterated over. This requires a second level of iteration with 'for j in range (...)' on line 20, which is in addition to the iteration function 'for i in range (...)' on line 19, which was also found in the getPropertiesFromAgentsPeopleCSV.py script. As with the previous script, it also writes the properties' values into a CSV file which is specified in variable 'f' on line 17.
3030

31-
####[searchArchivalObjectsByResource.py](/searchArchivalObjectsByResource.py)
31+
#### [searchArchivalObjectsByResource.py](/searchArchivalObjectsByResource.py)
3232
A GET script to extract all of the archival objects associated with a particular resource. To set the resource you want to search for, adjust the 'resourceNumber' variable on line 13

postContainersFromCSV.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# This script works to create instances (consisting of top_containers) from a separate CSV file. The CSV file should have two columns, indicator and barcode.
2+
# The directory where this file is stored must match the directory in the filePath variable, below.
3+
# 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.
4+
5+
import json
6+
import requests
7+
import secrets
8+
import csv
9+
10+
filePath = '[Enter File Path]'
11+
12+
targetFile = raw_input('Enter file name: ')
13+
targetRecord = raw_input('Enter record type and id (e.g. \'accessions/2049\'): ')
14+
15+
baseURL = secrets.baseURL
16+
user = secrets.user
17+
password = secrets.password
18+
19+
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
20+
session = auth["session"]
21+
headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'}
22+
23+
csv = csv.DictReader(open(filePath+targetFile))
24+
25+
containerList = []
26+
for row in csv:
27+
containerRecord = {}
28+
containerRecord['barcode'] = row['barcode']
29+
containerRecord['indicator'] = row['indicator']
30+
containerRecord = json.dumps(containerRecord)
31+
post = requests.post(baseURL + '/repositories/3/top_containers', headers=headers, data=containerRecord).json()
32+
print post
33+
containerList.append(post['uri'].encode('utf-8'))
34+
35+
asRecord = requests.get(baseURL+'/repositories/3/'+targetRecord, headers=headers).json()
36+
instanceArray = asRecord['instances']
37+
38+
for i in range (0, len (containerList)):
39+
top_container = {}
40+
top_container['ref'] = containerList[i]
41+
sub_container = {}
42+
sub_container['top_container'] = top_container
43+
instance = {}
44+
instance['sub_container'] = sub_container
45+
instance['instance_type'] = 'mixed_materials'
46+
instanceArray.append(instance)
47+
48+
asRecord['instances'] = instanceArray
49+
asRecord = json.dumps(asRecord)
50+
post = requests.post(baseURL+'/repositories/3/'+targetRecord, headers=headers, data=asRecord).json()
51+
print post

0 commit comments

Comments
 (0)