Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions python/asnake_export_publish_EADs.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
ARK_owner = config.get('EZID', 'ark_owner')

#set EAD export options: number components and include DAOs
export_options = '?include_daos=true&numbered_cs=true&include_unpublished=false'
# include uris = false so we dont get ASpace URIs
export_options = '?include_daos=true&numbered_cs=true&include_unpublished=false&include_uris=false'


#Log Into ASpace
Expand Down Expand Up @@ -66,48 +67,48 @@ def aspace_ark_check(resource_uri):

#Mint new ARK and POST to ASpace
def mint_and_post_new_ark(eadID):

#Set ArcLight URL prefix as target URL base + EADID (should come from AS resource record) and assign owner as "duke_ddr"
ark_data = '_target: https://archives.lib.duke.edu/catalog/{0}\n_owner: {1}'.format(eadID, ARK_owner)

#EZID seems to need these headers to be explicit
headers = {'Content-type': 'text/plain; charset=UTF-8'}

print (eadID + " | Minting an ARK...")
try:

try:
mint_ark = requests.post('https://ezid.cdlib.org/shoulder/{0}'.format(ARK_shoulder), headers=headers, data=ark_data, auth=(EZID_username, EZID_password))
#below for testing
#mint_ark = requests.post('https://ezid.cdlib.org/shoulder/ark:/99999/fk4', headers=headers, data=ark_data, auth=(EZID_username, EZID_password))

#responses look like (success: ark:/99999/fk4jm3mb69)
mint_response_text = mint_ark.text

#convert response to a Dict (e.g. {'success': 'ark:/99999/fdkl;jlkj'})
mint_response_dict = dict(mint_response_text.split(': ',1) for line in mint_response_text.strip().split('\n'))

#print (mint_response_dict['success'])
minted_ark = mint_response_dict['success']
#print (minted_ark)

#get metadata about the ARK you just minted...
get_minted_ark = requests.get('https://ezid.cdlib.org/id/{0}'.format(minted_ark), auth=(EZID_username, EZID_password))
print (get_minted_ark.text)

#Prepend Duke URL stuff to beginning of ARK
full_ark_uri = "https://idn.duke.edu/{0}".format(minted_ark)
print (eadID + " | Minted ARK: " + full_ark_uri)

#Get ASpace resource record
resource_json = aspace_client.get(resource_uri).json()

#Overwrite existing ead_location field value with new ARK URL
resource_json['ead_location'] = full_ark_uri

#Post updated ARK
resource_update_ark = aspace_client.post(resource_uri, json=resource_json).json()
print (eadID + " | Writing ARK to ASpace: " + resource_update_ark['status'])

except:
print (eadID + " | ERROR with ARK Minting / Posting")

Expand Down Expand Up @@ -136,10 +137,10 @@ def check_for_unpublished_nodes():

#Exports EAD for all resources matching EADID input in repository
for eadid in eadids_list:
#advanced search for EADID

#advanced search for EADID
results = aspace_client.get('repositories/'+AS_repository_id+'/search?page=1&aq={\"query\":{\"field\":\"ead_id\",\"value\":\"'+eadid+'\",\"jsonmodel_type\":\"field_query\",\"negated\":false,\"literal\":false}}').json()

if results["total_hits"] != 0:
#get the URI of the first search result (should only be one)
uri = results["results"][0]["id"]
Expand Down Expand Up @@ -179,14 +180,11 @@ def check_for_unpublished_nodes():
except:
pass


#Check for ARK, if none, mint new ARK and POST to ASpace
if aspace_ark_check(resource_uri) == False:
mint_and_post_new_ark(eadID)
else:
pass


#Test for unpublished nodes, if none continue with publishing
if check_for_unpublished_nodes() == False:

Expand All @@ -204,7 +202,7 @@ def check_for_unpublished_nodes():
f.write(ead)
f.close()
print (eadID + '.xml' ' | ' + resource_id + ' | ' + aspace_id_short + ' | ' + last_modified_by + ' | ' + user_mtime_slice + ' | ' + 'exported')

#If not published, set finding aid status to published
else:
print (eadID + " | Finding aid status: " + publish_status)
Expand Down Expand Up @@ -232,4 +230,4 @@ def check_for_unpublished_nodes():
else:
print (eadID + ' | ***ERROR***: ' + eadid + ' does not exist!')

input("All done!. Press Enter to Exit")
input("All done!. Press Enter to Exit")
Loading