|
| 1 | +import json |
| 2 | +import requests |
| 3 | +import secrets |
| 4 | +import time |
| 5 | +import csv |
| 6 | + |
| 7 | +startTime = time.time() |
| 8 | + |
| 9 | +baseURL = secrets.baseURL |
| 10 | +user = secrets.user |
| 11 | +password = secrets.password |
| 12 | + |
| 13 | +auth = requests.post(baseURL + '/users/'+user+'/login?password='+password).json() |
| 14 | +session = auth["session"] |
| 15 | +headers = {'X-ArchivesSpace-Session':session, 'Content_Type':'application/json'} |
| 16 | + |
| 17 | +f=csv.writer(open('resourceProperties.csv', 'wb')) |
| 18 | +f.writerow(['title']+['uri']+['bibnum']+['type']+['value']) |
| 19 | + |
| 20 | + |
| 21 | +endpoint = raw_input('Enter resource ID: ') |
| 22 | +output = requests.get(baseURL + endpoint, headers=headers).json() |
| 23 | +print json.dumps(output) |
| 24 | + |
| 25 | +title = output['title'].encode('utf-8') |
| 26 | +uri = output['uri'] |
| 27 | +try: |
| 28 | + bibnum = output['user_defined']['real_1'] |
| 29 | +except: |
| 30 | + bibnum = '' |
| 31 | +try: |
| 32 | + agents = output['linked_agents'] |
| 33 | + for agent in agents: |
| 34 | + agentUri = agent['ref'] |
| 35 | + agentOutput = requests.get(baseURL + agentUri, headers=headers).json() |
| 36 | + agentName = agentOutput['title'] |
| 37 | + f.writerow([title]+[uri]+[bibnum]+['name']+[agentName]) |
| 38 | +except: |
| 39 | + pass |
| 40 | +try: |
| 41 | + subjects = output['subjects'] |
| 42 | + for subject in subjects: |
| 43 | + subjectUri = subject['ref'] |
| 44 | + subjectOutput = requests.get(baseURL + subjectUri, headers=headers).json() |
| 45 | + subjectName = subjectOutput['title'] |
| 46 | + f.writerow([title]+[uri]+[bibnum]+['subject']+[subjectName]) |
| 47 | +except: |
| 48 | + pass |
| 49 | +for note in output['notes']: |
| 50 | + abstract = '' |
| 51 | + scopecontent = '' |
| 52 | + acqinfo = '' |
| 53 | + custodhist = '' |
| 54 | + if note['type'] == 'abstract': |
| 55 | + abstract = note['content'][0] |
| 56 | + |
| 57 | + f.writerow([title]+[uri]+[bibnum]+['abstract']+[abstract]) |
| 58 | + if note['type'] == 'scopecontent': |
| 59 | + scopecontentSubnotes = note['subnotes'] |
| 60 | + for subnote in scopecontentSubnotes: |
| 61 | + scopecontent = scopecontent + subnote['content'] + ' ' |
| 62 | + f.writerow([title]+[uri]+[bibnum]+['scopecontent']+[scopecontent]) |
| 63 | + if note['type'] == 'acqinfo': |
| 64 | + acqinfoSubnotes = note['subnotes'] |
| 65 | + for subnote in acqinfoSubnotes: |
| 66 | + acqinfo = acqinfo + subnote['content'] + ' ' |
| 67 | + f.writerow([title]+[uri]+[bibnum]+['acqinfo']+[acqinfo]) |
| 68 | + if note['type'] == 'custodhist': |
| 69 | + custodhistSubnotes = note['subnotes'] |
| 70 | + for subnote in custodhistSubnotes: |
| 71 | + custodhist = custodhist + subnote['content'] + ' ' |
| 72 | + f.writerow([title]+[uri]+[bibnum]+['custodhist']+[custodhist]) |
| 73 | + |
| 74 | +elapsedTime = time.time() - startTime |
| 75 | +m, s = divmod(elapsedTime, 60) |
| 76 | +h, m = divmod(m, 60) |
| 77 | +print 'Total script run time: ', '%d:%02d:%02d' % (h, m, s) |
0 commit comments