-
Notifications
You must be signed in to change notification settings - Fork 5
/
updateResourceWithAgentOrSubjectLinks.py
127 lines (114 loc) · 4.06 KB
/
updateResourceWithAgentOrSubjectLinks.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import json
import requests
import time
import csv
import argparse
from datetime import datetime
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')
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--type', help='the type of links to create \
("subject" or "agent"). optional - if not provided, the script will ask \
for input')
args = parser.parse_args()
if args.type:
type = args.type
else:
type = input('Enter the type of links to create ("subject" or "agent"): ')
def addUriLink(key, valueSource):
"""Add URI link to record."""
uri = '/repositories/' + repository + '/resources/' + row['ResourceUri']
value = row[valueSource]
print(value)
asRecord = requests.get(baseURL + uri, headers=headers).json()
updatedRecord = asRecord
if key == 'subjects':
subjects = updatedRecord['subjects']
subject = {}
subject['ref'] = value
if subject not in subjects:
subjects.append(subject)
updatedRecord['subjects'] = subjects
print(updatedRecord['subjects'])
updatedRecord = json.dumps(updatedRecord)
print(baseURL + uri)
post = requests.post(baseURL + uri, headers=headers,
data=updatedRecord).json()
print(post)
f.writerow([uri] + [subjects] + [post])
else:
print('no update')
f.writerow([uri] + ['no update'] + [])
elif key == 'linked_agents':
agents = updatedRecord['linked_agents']
print('originalAgents')
print(agents)
agent = {}
agent['terms'] = []
agent['ref'] = value
if row['tag'].startswith('1'):
agent['role'] = 'creator'
elif row['tag'].startswith('7'):
agent['role'] = 'creator'
elif row['tag'].startswith('6'):
agent['role'] = 'subject'
else:
print('error')
f.writerow([uri] + ['tag error'] + [])
if agent not in agents:
agents.append(agent)
print('updatedAgents')
print(agents)
updatedRecord['linked_agents'] = agents
updatedRecord = json.dumps(updatedRecord)
print(baseURL + uri)
post = requests.post(baseURL + uri, headers=headers,
data=updatedRecord).json()
print(post)
f.writerow([uri] + [agents] + [post])
else:
print('no update')
print(agent)
f.writerow([uri] + ['no update'] + [])
else:
print('error')
f.writerow([uri] + ['error'] + [])
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'}
filename = input('Enter filename (including \'.csv\'): ')
date = datetime.now().strftime('%Y-%m-%d %H.%M.%S')
f = csv.writer(open(filename + 'Post' + date + '.csv', 'w'))
f.writerow(['uri'] + ['links'] + ['post'])
with open(filename) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
if type == 'agent':
addUriLink('linked_agents', 'agentUri')
elif type == 'subject':
addUriLink('subjects', 'subjectUri')
else:
f.writerow(['error - invalid type, should be "subject" or "agent"']
+ [] + [])
print('error - invalid type, should be "subject" or "agent"')
break
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))