Skip to content

Instantly share code, notes, and snippets.

@eckardm
Last active October 15, 2019 18:38
from bhlaspaceapiclient import ASpaceClient
from dappr import DAPPr
import sys
print("initializing ASpaceAPIClient and DAPPr")
archivesspace = ASpaceClient(instance_name="PROD")
resource_id = 9345
collection_id = ''
print("parsing resource")
resource_children_with_digital_objects = archivesspace.get_resource_children_with_instances(resource_id, instance_type="digital_object")
for resource_child_with_digital_objects in resource_children_with_digital_objects:
deepblue = DAPPr(instance_name="PROD")
archival_object_id = resource_child_with_digital_objects.split("/")[-1]
archival_object = archivesspace.get_archival_object(archival_object_id)
archival_object_uri = archival_object["uri"]
# characterize archival object
print("processing archival object: " + str(archival_object_id))
instances = archival_object["instances"]
archival_object_type = ""
# btw, already checked to make sure there was only one deepblue link
deepblue_links = archivesspace.get_digital_object_instance_links(archival_object, "hdl.handle.net")
bdml_links = archivesspace.get_digital_object_instance_links(archival_object, "bentley.mivideo.it.umich.edu")
bhl_digital_archive_links = archivesspace.get_digital_object_instance_links(archival_object, "R:")
web_archives_links = archivesspace.get_digital_object_instance_links(archival_object, "wayback.archive-it.org")
# btw, also already checked to make sure that all archival objects with digital objects fell into the following categories
if len(deepblue_links) == 0 and len(bdml_links) == 0 and len(bhl_digital_archive_links) == 0 and len(web_archives_links) >= 1:
archival_object_type = "web archives"
elif len(deepblue_links) >= 1 and len(bdml_links) == 0 and len(bhl_digital_archive_links) == 0 and len(web_archives_links) == 0:
archival_object_type = "born-digital archives"
elif len(deepblue_links) >= 1 and len(bdml_links) >= 1 and len(bhl_digital_archive_links) == 0 and len(web_archives_links) == 0:
archival_object_type = "born-digital a/v"
elif len(deepblue_links) == 0 and len(bdml_links) >= 1 and len(bhl_digital_archive_links) >= 1 and len(web_archives_links) == 0:
archival_object_type = "a/v only"
# born-digital archives
if archival_object_type == "born-digital archives":
print("is born-digital archives")
'''
notes = archival_object["notes"]
for note in notes:
if note.get("type") == "accessrestrict":
notes.remove(note)
archival_object["notes"] = notes'''
# an alternative would be to do something like:
accessrestricts = archivesspace.find_notes_by_type(archival_object, "accessrestrict")
if accessrestricts:
for accessrestrict in accessrestricts:
archival_object["notes"].remove(accessrestrict)
# archivesspace.update_aspace_object(archival_object_uri, archival_object)
# so, if it's a born-digital archives, the deepblue links should be published
for instance in instances:
digital_object_ref = instance['digital_object']['ref']
digital_object_id = digital_object_ref.split("/")[-1]
digital_object = archivesspace.get_digital_object(digital_object_id)
digital_object['publish'] = True
# archivesspace.update_aspace_object(digital_object_ref, digital_object)
handle = "2027.42/" + deepblue_links[0].split("/")[-1].rstrip()
print("processing item: " + handle)
item = deepblue.get_handle(handle)
item_id = item["id"]
metadata = deepblue.get_item_metadata(item_id)
for metadatum in metadata:
if metadatum["key"] == "dc.rights.access" or metadatum["key"] == "dc.rights.copyright" or metadatum["key"] == "dc.date.open" or metadatum["key"] == "dc.description.restriction":
metadata.remove(metadatum)
metadata.append({
"key": "dc.rights.copyright",
"value": "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."
})
# deepblue.post_item_metadata(item_id, metadata)
bitstreams = deepblue.get_item_bitstreams(item_id)
for bitstream in bitstreams:
bitstream_id = bitstream["id"]
bitstream_name = bitstream["name"]
if bitstream_name == "objects.zip" or bitstream_name.startswith("39015"):
bitstream["description"] = "Archival materials."
# deepblue.put_bitstream(bitstream_id, bitstream)
bitstream_policies = deepblue.get_bitstream_policy(bitstream_id)
for bitstream_policy in bitstream_policies:
bitstream_policy["groupId"] = 0
# deepblue.put_bitstream_policy(bitstream_id, bitstream_policies)
# born-digital a/v
if archival_object_type == "born-digital a/v":
print("is born-digital a/v")
'''
notes = archival_object["notes"]
for note in notes:
if note["type"] == "accessrestrict":
note["subnotes"][0]["content"] = "Access to this material is restricted to the reading room of the Bentley Historical Library."
archival_object["notes"] = notes'''
# an alternative would be to do something like:
accessrestricts = archivesspace.find_notes_by_type(archival_object, 'accessrestrict')
if accessrestricts:
accessrestrict = accessrestricts[0]
accessrestrict["publish"] = True
accessrestrict['subnotes'][0]['content'] = "Access to this material is restricted to the reading room of the Bentley Historical Library."
accessrestrict["rights_restriction"]["local_access_restriction_type"] = ["ReadingRoom"]
else:
accessrestrict = {
"type": "accessrestrict",
"rights_restriction": {
"local_access_restriction_type": ["ReadingRoom"]
},
"subnotes": [{
"content": "Access to this material is restricted to the reading room of the Bentley Historical Library.",
"jsonmodel_type": "note_text"
}],
"jsonmodel_type": "note_multipart"
}
archival_object["notes"].append(accessrestrict)
# archivesspace.update_aspace_object(archival_object_uri, archival_object)
# if it's a born-digital A/V, the BDML link should be published, the deep blue link should not
for instance in instances:
digital_object_ref = instance['digital_object']['ref']
digital_object_id = digital_object_ref.split("/")[-1]
digital_object = archivesspace.get_digital_object(digital_object_id)
if "hdl.handle.net" in digital_object['file_versions'][0]["file_uri"]:
digital_object['publish'] = False
else:
digital_object['publish'] = True
# archivesspace.update_aspace_object(digital_object_ref, digital_object)
handle = "2027.42/" + deepblue_links[0].split("/")[-1].rstrip()
print("processing item: " + handle)
item = deepblue.get_handle(handle)
item_id = item["id"]
metadata = deepblue.get_item_metadata(item_id)
for metadatum in metadata:
if metadatum["key"] == "dc.rights.access" or metadatum["key"] == "dc.rights.copyright" or metadatum["key"] == "dc.date.open" or metadatum["key"] == "dc.description.restriction":
metadata.remove(metadatum)
metadata.append([{
"key": "dc.rights.copyright",
"value": "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."
},
{
"key": "dc.rights.access",
"value": "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."
}])
# deepblue.post_item_metadata(item_id, metadata)
bitstreams = deepblue.get_item_bitstreams(item_id)
for bitstream in bitstreams:
bitstream_id = bitstream["id"]
bitstream_name = bitstream["name"]
if bitstream_name == "objects.zip" or bitstream_name.startswith("39015"):
bitstream["description"] = "Archival materials. Access restricted to Bentley staff."
# deepblue.put_bitstream(bitstream_id, bitstream)
bitstream_policies = deepblue.get_bitstream_policy(bitstream_id)
for bitstream_policy in bitstream_policies:
bitstream_policy["groupId"] = 560
# deepblue.put_bitstream_policy(bitstream_id, bitstream_policies)
print("publishing archival object")
archival_object["publish"] = True
# archivesspace.update_aspace_object(archival_object_uri, archival_object)
deepblue.logout()
print("\n")
print("Alright, we're done!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment