Created
October 3, 2019 18:08
-
-
Save jackflaps/5dfa3411b5171ec2796733ef4953db4c to your computer and use it in GitHub Desktop.
update metadata in a batch from CSV of URIs using ArchivesSnake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv, json, os | |
from asnake.aspace import ASpace | |
AS = ASpace() | |
repo = AS.repositories(2) | |
def get_json(uri): | |
r = AS.client.get(uri) | |
if r.status_code == 200: | |
return json.loads(r.text) | |
else: | |
r.raise_for_status() | |
def post_json(uri, data): | |
r = AS.client.post(uri,json=data) | |
message = json.loads(r.text) | |
if r.status_code == 200: | |
print("{}: {}".format(message['status'], message['uri'])) | |
else: | |
print("Error: {}".format(message['error'])) | |
def post_digital_object(uri, row, obj): | |
dao = { | |
'title': obj['display_string'], | |
'digital_object_type': "text", | |
'jsonmodel_type': "digital_object", | |
'publish': True, | |
'file_versions': [] | |
} | |
dao['digital_object_id'] = row[1] | |
dao['file_versions'].append({ | |
'file_uri': "{}.pdf".format(row[0]), | |
'file_format_type': "pdf", | |
'publish': True | |
}) | |
r = AS.client.post("/repositories/2/digital_objects", json=dao) | |
message = json.loads(r.text) | |
if r.status_code == 200: | |
print("{}: {}".format(message['status'], message['uri'])) | |
obj['external_documents'].append({ | |
'title': "Special Collections @ DU", | |
'location': row[1] | |
}) | |
obj['instances'].append({ | |
'instance_type': "digital_object", | |
'jsonmodel_type': "instance", | |
'is_representative': True, | |
'digital_object': { 'ref': message['uri'] } | |
}) | |
post_json(uri, obj) | |
else: | |
print("Error: {}".format(message['error'])) | |
pass | |
def search_for(obj): | |
r = AS.client.get('/repositories/2/search', params={'q': obj, 'type[]': "archival_object", 'page': "1"}) | |
if r.status_code == 200: | |
results = json.loads(r.text)['results'] | |
uris = [r for r in results if r['component_id'] == obj and 'pui' not in r['types']] | |
if len(uris) == 0: | |
print("No URI found: {}".format(row[0])) | |
pass | |
elif len(uris) == 1: | |
return uris[0]['uri'] | |
else: | |
print("Found multiple URIs for {}".format(row[0])) | |
pass | |
else: | |
r.raise_for_status() | |
file = input("File containing URIs: ") | |
if os.path.exists(file): | |
with open(file, 'r') as f: | |
reader = csv.reader(f) | |
for row in reader: | |
obj = get_json(row[0]) | |
# do whatever here | |
post_json(row[0], obj) | |
else: | |
print("File not found: {}".format(file)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment