-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetPropertiesFromSingleResource.py
93 lines (83 loc) · 2.99 KB
/
getPropertiesFromSingleResource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import requests
import time
import csv
secretsVersion = input('To edit production server, enter the name of the \
secrets file: ')
if secretsVersion != '':
try:
secrets = __import__(secretsVersion)
print('Editing Production')
except ImportError:
secrets = __import__('secrets')
print('Editing Development')
else:
print('Editing Development')
startTime = time.time()
baseURL = secrets.baseURL
user = secrets.user
password = secrets.password
repository = secrets.repository
auth = requests.post(baseURL + '/users/' + user + '/login?password='
+ password).json()
session = auth['session']
headers = {'X-ArchivesSpace-Session': session,
'Content_Type': 'application/json'}
f = csv.writer(open('resourceProperties.csv', 'w'))
f.writerow(['title'] + ['uri'] + ['bibnum'] + ['type'] + ['value'])
endpoint = input('Enter resource ID: ')
output = requests.get(baseURL + endpoint, headers=headers).json()
print(json.dumps(output))
title = output['title']
uri = output['uri']
try:
bibnum = output['user_defined']['real_1']
except ValueError:
bibnum = ''
try:
agents = output['linked_agents']
for agent in agents:
agentUri = agent['ref']
agentOutput = requests.get(baseURL + agentUri, headers=headers).json()
agentName = agentOutput['title']
f.writerow([title] + [uri] + [bibnum] + ['name'] + [agentName])
except ValueError:
pass
try:
subjects = output['subjects']
for subject in subjects:
subjectUri = subject['ref']
subjectOutput = requests.get(baseURL + subjectUri,
headers=headers).json()
subjectName = subjectOutput['title']
f.writerow([title] + [uri] + [bibnum] + ['subject'] + [subjectName])
except ValueError:
pass
for note in output['notes']:
abstract = ''
scopecontent = ''
acqinfo = ''
custodhist = ''
if note['type'] == 'abstract':
abstract = note['content'][0]
f.writerow([title] + [uri] + [bibnum] + ['abstract'] + [abstract])
if note['type'] == 'scopecontent':
scopecontentSubnotes = note['subnotes']
for subnote in scopecontentSubnotes:
scopecontent = scopecontent + subnote['content'] + ' '
f.writerow([title] + [uri] + [bibnum] + ['scopecontent']
+ [scopecontent])
if note['type'] == 'acqinfo':
acqinfoSubnotes = note['subnotes']
for subnote in acqinfoSubnotes:
acqinfo = acqinfo + subnote['content'] + ' '
f.writerow([title] + [uri] + [bibnum] + ['acqinfo'] + [acqinfo])
if note['type'] == 'custodhist':
custodhistSubnotes = note['subnotes']
for subnote in custodhistSubnotes:
custodhist = custodhist + subnote['content'] + ' '
f.writerow([title] + [uri] + [bibnum] + ['custodhist'] + [custodhist])
elapsedTime = time.time() - startTime
m, s = divmod(elapsedTime, 60)
h, m = divmod(m, 60)
print('Total script run time: ', '%d:%02d:%02d' % (h, m, s))