-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
172 lines (135 loc) · 4.87 KB
/
main.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import json
import sys
import time
import multiprocessing
import requests
print("Script starting")
start = time.time()
print('Will post data to')
okapi_url = sys.argv[3]
print("\tOkapi URL:\t", okapi_url)
tenant_id = sys.argv[4]
print("\tTenanti Id:\t", tenant_id)
okapi_token = sys.argv[5]
print("\tToken: \t", okapi_token)
print("Opening file", sys.argv[2])
myi = 0
i = 0
url = ""
instance_id_mappings = {}
lookups = 0
cache_hits = 0
okapi_headers = {'x-okapi-token': okapi_token,
'x-okapi-tenant': tenant_id,
'content-type': 'application/json'}
def get_folio_instance_id(old_id):
if old_id in instance_id_mappings:
print('cache hit!')
return instance_id_mappings[old_id]
else:
instance_id_mappings[old_id] = lookup_folio_instance_id(old_id)
return instance_id_mappings[old_id]
def lookup_folio_instance_id(old_id):
path = "/instance-storage/instances"
identifierTypeId = '7e591197-f335-4afb-bc6d-a6d76ca3bace'
url = ('?limit=2&query=identifiers == \"*\\\"value\\\": \\\"{}*\\\",'
'\\\"identifierTypeId\\\": \\\"{}\\\"*\" sortby'
'title').format(old_id, identifierTypeId)
req = requests.get(okapi_url+path+url,
headers=okapi_headers)
folio_instance_id = json.loads(req.text)['instances'][0]['id']
return folio_instance_id
def delete_item(item_id):
path = '/items-storage/temss/{0}'.format(item_id)
req = requests.delete(okapi_url+path, headers=okapi_headers)
def delete_instance(instance_id):
path = '/instance-storage/instances/{0}'.format(instance_id)
req = requests.delete(okapi_url+path, headers=okapi_headers)
def delete_holding(holding_id):
path = '/holdings-storage/holdings/{0}'.format(holding_id)
req = requests.delete(okapi_url+path, headers=okapi_headers)
def post_instance(instance):
path = '/instance-storage/instances'
try:
req = requests.post(okapi_url+path,
data=json.dumps(instance),
headers=okapi_headers,
timeout=2)
if req.status_code == 400 and 'already exists.' in req.text:
delete_instance(instance['id'])
# TODO: take care of infinite loop
if post_instance(instance):
return True
if req.status_code == 201:
return True
if 401 <= req.status_code <= 599:
print("Error!")
print("Code:\t{} Message:\t{}".format(req.status_code, req.text))
print(instance)
return False
except Exception as e:
print("ERROR!\ttype\t{} ID:\t{}".format(type(e), instance["id"]))
return False
def post_item(item):
path = '/item-storage/items'
req = requests.post(okapi_url+path,
data=json.dumps(item),
headers=okapi_headers)
if req.status_code == 400 and 'already exists.' in req.text:
delete_item(item['id'])
# TODO: take care of infinite loop
post_item(item)
print("Exists")
def post_holding(holding):
path = '/holdings-storage/holdings'
req = requests.post(okapi_url+path,
data=json.dumps(holding),
headers=okapi_headers)
if req.status_code == 400 and 'already exists.' in req.text:
delete_holding(holding['id'])
# TODO: take care of infinite loop
post_holding(holding)
def myformat(x):
return ('%.2f' % x).rstrip('0').rstrip('.')
def handle_holding(line):
holding = json.loads(line)
old_instance_id = holding["instance_id"]
holding["instance_id"] = get_folio_instance_id(old_instance_id)
post_holding(holding)
def handle_instance(line):
instance = json.loads(line)
post_instance(instance)
def hadle_item(line):
item = json.loads(line)
post_item(item)
successful = 0
failed = 0
start = time.time()
def cb(result):
global start
global successful
global failed
if result:
successful += 1
else:
failed += 1
# if i % 10 == 0:
elapsed = myformat((successful+failed)/(time.time() - start))
print("r/s: {}\tFailed: {}\tSuccessful: {}".format(elapsed,
successful,
failed), end="\r")
return successful
# Main
with multiprocessing.Pool(processes=1) as pool:
if sys.argv[1] in ['holdings', 'holding', 'hold']:
with open(sys.argv[2]) as f:
pool.map(handle_holding, f)
elif sys.argv[1] in ['items', 'item']:
with open(sys.argv[2]) as f:
for line in f:
pool.map(hadle_item, f)
elif sys.argv[1] in ['bibs', 'bib', 'instance', 'instances']:
with open(sys.argv[2]) as f:
for line in f:
cb(handle_instance(line))
print("Script finished")