Skip to content

Instantly share code, notes, and snippets.

@zachary-johnson
Created February 28, 2019 17:31
Show Gist options
  • Save zachary-johnson/f1910dd111683749b3ad08b5d7f95eb0 to your computer and use it in GitHub Desktop.
Save zachary-johnson/f1910dd111683749b3ad08b5d7f95eb0 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os, requests, json, sys, logging, ConfigParser
#config = ConfigParser.ConfigParser()
#config.read('local_settings.cfg')
#dictionary = {'baseURL': config.get('ArchivesSpace', 'baseURL'), 'repository':config.get('ArchivesSpace', 'repository'), 'user': config.get('ArchivesSpace', 'user'), 'password': config.get('ArchivesSpace', 'password'), 'destination': config.get('Destinations', 'EADdestination')}
# parses arguments, if any. This allows you to pass in an string to match against resource IDs
exportIds = sys.argv[0]
logging.basicConfig(filename='simple-log.txt',format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p', level=logging.DEBUG)
baseURL = 'http://dev-asb.library.vanderbilt.edu/'
repository = '2'
user = 'username'
password = 'password'
destination = '~/Desktop/aspace/'
EADdestination = '~/Desktop/aspace/'
# authenticates the session
def authenticate():
auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json()
session = auth["session"]
headers = {'X-ArchivesSpace-Session':session}
return headers
headers = authenticate()
# Gets the IDs of all resources in the repository
logging.info('Getting a list of resources')
resourceIds = requests.get(baseURL + '/repositories/'+repository+'/resources?all_ids=true', headers=headers)
# Exports EAD for all resources whose IDs contain argument
for id in resourceIds.json():
if not requests.get(baseURL + '/repositories/'+repository+'/resources/' + str(id), headers=headers):
headers = authenticate()
resource = (requests.get(baseURL + '/repositories/'+repository+'/resources/' + str(id), headers=headers)).json()
resourceID = resource["id_0"]
if exportIds:
if exportIds in resourceID:
logging.info('Exporting ' + resourceID)
ead = requests.get(baseURL + '/repositories/'+repository+'/resource_descriptions/'+str(id)+'.xml', headers=headers, stream=True)
if not os.path.exists(destination):
os.makedirs(destination)
with open(destination+resourceID+'.xml', 'wb') as f:
for chunk in ead.iter_content(10240):
f.write(chunk)
f.close
else:
logging.info('Exporting ' + resourceID)
ead = requests.get(baseURL + '/repositories/'+repository+'/resource_descriptions/'+str(id)+'.xml', headers=headers, stream=True)
if not os.path.exists(destination):
os.makedirs(destination)
with open(destination+resourceID+'.xml', 'wb') as f:
for chunk in ead.iter_content(10240):
f.write(chunk)
f.close
logging.info('Done!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment