Created
March 21, 2019 17:53
-
-
Save eckardm/a1f314af637302e57e63f5862d92fa24 to your computer and use it in GitHub Desktop.
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
import csv | |
from dappr.dappr import DAPPr | |
from dappr.config import prod | |
from datetime import datetime | |
import os | |
import requests | |
accession_id = 170864 # 168861 | |
csv_metadata = [] | |
with open('170864_bhl_inventory_with_archival_object-with_mivideo_ids.csv', mode='r') as f: | |
reader = csv.DictReader(f) | |
for row in reader: | |
metadatum = {} | |
metadatum['barcode'] = row['barcode'] | |
metadatum['archival_object_id'] = row['archival_object_id'] | |
# metadatum['archival_object_id'] = row['archival_object_link'].split('archival_object_')[-1] | |
metadatum['kaltura_ids'] = row['mivideo_ids'] | |
metadatum['accessrestrict'] = row['access_restrictions'] | |
csv_metadata.append(metadatum) | |
archivesspace = { | |
'base_url': '', | |
'user': '', | |
'password': '', | |
'repository': 2 | |
} | |
# logging to archivesspace and dspace | |
url = archivesspace['base_url'] + '/users/' + archivesspace['user'] + '/login?password=' + archivesspace['password'] | |
response = requests.post(url) | |
token = response.json().get('session') | |
deepblue = DAPPr( | |
prod.get('base_url'), | |
prod.get('email'), | |
prod.get('password'), | |
) | |
print "deepblue deposit" | |
print "================" | |
for name in os.listdir(r"Q:\prod\aip_to_item-queue\Ready\\" + str(accession_id)): | |
aip_name = name | |
aip_uuid = name[-36:] | |
aip_barcode = name[0:14] | |
print "\n * depositing " + aip_name | |
archival_object_id = [metadatum['archival_object_id'] for metadatum in csv_metadata if metadatum['barcode'] == aip_barcode][0] | |
kaltura_ids = [metadatum['kaltura_ids'] for metadatum in csv_metadata if metadatum['barcode'] == aip_barcode][0] | |
accessrestrict = [metadatum['accessrestrict'] for metadatum in csv_metadata if metadatum['barcode'] == aip_barcode][0] | |
# getting metadata from archivesspace | |
url = archivesspace['base_url'] + '/repositories/' + str(archivesspace['repository']) + '/archival_objects/' + str(archival_object_id) | |
headers = {'X-ArchivesSpace-Session': token} | |
response = requests.get(url, headers=headers) | |
archival_object = response.json() | |
print "\nmetadata..." | |
title = archival_object['display_string'] | |
print "dc.title: " + title | |
# optional | |
description_abstract = '' | |
for note in archival_object['notes']: | |
if note['type'] == 'odd': | |
description_abstract = note['subnotes'][0]['content'] | |
contributor_author = '' | |
url = archivesspace['base_url'] + archival_object['resource']['ref'] | |
response = requests.get(url, headers=headers) | |
resource = response.json() | |
for linked_agent in resource['linked_agents'] : | |
if linked_agent['role'] == 'creator': | |
url = archivesspace['base_url'] + linked_agent['ref'] | |
response = requests.get(url, headers=headers) | |
creator = response.json() | |
contributor_author = creator['title'] | |
print "dc.contributor.author: " + contributor_author | |
date_issued = str(datetime.now().year) | |
print "dc.date.issued: " + date_issued | |
rights_copyright = 'This content may be under copyright. Researchers are responsible for determining the appropriate use or reuse of materials. Please consult the collection finding aid or catalog record for more information.' | |
print "dc.rights.copyright: " + rights_copyright | |
relation_ispartofseries = [] | |
while archival_object.get('parent'): | |
url = archivesspace['base_url'] + archival_object['parent']['ref'] | |
response = requests.get(url, headers=headers) | |
parent_archival_object = response.json() | |
relation_ispartofseries.append(parent_archival_object['display_string'].replace('<genreform normal="Photographs">', '').replace('</genreform>', '').replace('<title render="italic">', '').replace('</title>', '')) | |
archival_object = parent_archival_object | |
relation_ispartofseries.reverse() | |
relation_ispartofseries = ' - '.join(relation_ispartofseries) | |
print "dc.relation.ispartofseries: " + relation_ispartofseries | |
# optional | |
rights_access = '' | |
if accessrestrict == 'reading room' and kaltura_ids: | |
rights_access = 'Streaming Only: This recording may be protected by copyright law. Access to this material is restricted to the reading room of the Bentley Historical Library.' | |
elif accessrestrict == 'reading room': | |
rights_access = 'Access to this material is restricted to the reading room of the Bentley Historical Library.' | |
print 'dc.rights.access: ' + rights_access | |
identifier_videostreams = [] | |
if kaltura_ids: | |
kaltura_ids = kaltura_ids.split('; ') | |
player = 1455309001 | |
for kaltura_id in kaltura_ids: | |
identifier_videostream = "https://cdnapisec.kaltura.com/p/1758271/sp/175827100/embedIframeJs/uiconf_id/29300931/partner_id/1758271?autoembed=true&entry_id=" + kaltura_id + "&playerId=kaltura_player_" + str(player) + "&cache_st=1455309475&width=400&height=330&flashvars[streamerType]=auto" | |
identifier_videostreams.append(identifier_videostream) | |
player += 1 | |
print 'dc.identifier.videostream: ' + identifier_videostream | |
# depositing | |
collection_id = 206 # CHANGE | |
item = {'name': title} | |
print '\nposting collection item...' | |
item = deepblue.post_collection_item(int(collection_id), item) | |
item_id = item['id'] | |
item_handle = item['handle'] | |
metadata = [ | |
{'key': 'dc.title', 'value': title}, | |
{'key': 'dc.contributor.author', 'value': contributor_author}, | |
{'key': 'dc.date.issued', 'value': date_issued}, | |
{'key': 'dc.rights.copyright', 'value': rights_copyright}, | |
{'key': 'dc.relation.ispartofseries', 'value': relation_ispartofseries} | |
] | |
if description_abstract: | |
metadata.append({'key': 'dc.description.abstract', 'value': description_abstract}) | |
if rights_access: | |
metadata.append({'key': 'dc.rights.access', 'value': rights_access}) | |
if identifier_videostreams: | |
for identifier_videostream in identifier_videostreams: | |
metadata.append({'key': 'dc.identifier.videostream', 'value': identifier_videostream}) | |
print "updating item metadata..." | |
deepblue.put_item_metadata(int(item_id), metadata) | |
for bitstream in os.listdir(os.path.join(r"Q:\prod\aip_to_item-queue\Ready\\" + str(accession_id), name)): | |
if bitstream.startswith('objects'): | |
print 'posting item "objects" bitstream...' | |
bitstream = deepblue.post_item_bitstream(int(item_id), os.path.join(r"Q:\prod\aip_to_item-queue\Ready\\" + str(accession_id), name, bitstream)) | |
bitstream_id = bitstream['id'] | |
bitstream['name'] = 'objects.zip' | |
if accessrestrict == 'reading room': | |
bitstream['description'] = 'Archival materials. Access restricted to Bentley staff.' | |
print 'updating item "objects" bitstream...' | |
deepblue.put_bitstream(int(bitstream_id), bitstream) | |
print 'updating item "objects" bitstream policy if necessary...' | |
if accessrestrict == 'reading room': | |
deepblue.put_bitstream_policy(int(bitstream_id), [{"action": "READ", "rpType": "TYPE_CUSTOM", "groupId": 560}]) | |
elif bitstream.startswith('metadata'): | |
print 'posting item "metadata" bitstream...' | |
bitstream = deepblue.post_item_bitstream(int(item_id), os.path.join(r"Q:\prod\aip_to_item-queue\Ready\\" + str(accession_id), name, bitstream)) | |
bitstream_id = bitstream['id'] | |
bitstream['name'] = 'metadata.zip' | |
bitstream['description'] = 'Administrative information. Access restricted to Bentley staff.' | |
print 'updating item "metadata" bitstream...' | |
deepblue.put_bitstream(int(bitstream_id), bitstream) | |
print 'updating item "metadata" bitsream policy...' | |
deepblue.put_bitstream_policy(int(bitstream_id), [{"action": "READ", "rpType": "TYPE_CUSTOM", "groupId": 560}]) | |
print 'posting item license...' | |
deepblue.post_item_license(int(item_id)) | |
# updating archivespace | |
url = archivesspace['base_url'] + '/repositories/' + str(archivesspace['repository']) + '/digital_objects' | |
headers = {'X-ArchivesSpace-Session': token, 'Content-Type': 'application/json'} | |
body = { | |
"title": title, | |
"publish": True, | |
"digital_object_id": aip_barcode, | |
"jsonmodel_type": "digital_object", | |
"file_versions": [ | |
{ | |
"file_uri": 'http://hdl.handle.net/' + item_handle, | |
'xlink_show_attribute': 'new', | |
'xlink_actuate_attribute': 'onRequest' | |
} | |
] | |
} | |
if kaltura_ids: | |
body['publish'] = False | |
print '\nposting digital object...' | |
response = requests.post(url, headers=headers, json=body) | |
digital_object = response.json() | |
digital_object_uri = digital_object["uri"] | |
url = archivesspace['base_url'] + '/repositories/' + str(archivesspace['repository']) + '/archival_objects/' + str(archival_object_id) | |
response = requests.get(url, headers=headers) | |
archival_object = response.json() | |
instance = { | |
"instance_type": "digital_object", | |
"jsonmodel_type": "instance", | |
"is_representative": False, | |
"digital_object": { | |
"ref": digital_object_uri | |
} | |
} | |
archival_object['instances'].append(instance) | |
print 'appending digital object to archival object...' | |
response = requests.post(url, headers=headers, json=archival_object) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment