-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateItemFieldsFromCSV_BarcodeEndpoint.py
112 lines (101 loc) · 3.77 KB
/
updateItemFieldsFromCSV_BarcodeEndpoint.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
import requests
import secret
import json
import pandas as pd
import time
from datetime import datetime
start_time = time.time()
baseURL = 'https://api-na.hosted.exlibrisgroup.com'
# Use secret files to request from either production or stage.
secretsVersion = input('To edit production server, enter secret filename: ')
if secretsVersion != '':
try:
secret = __import__(secretsVersion)
print('Editing Production')
except ImportError:
print('Editing Stage')
else:
print('Editing Stage')
# From selected secret file, grab the api_key.
api_key = secret.api_key
# Create headers to authorize with api_key and to request output in JSON.
headers = {"Authorization": "apikey "+api_key,
"Accept": "application/json",
"Content-Type": "application/json"}
filename = 'LSC AFA Update Spreadsheet 6-14-24.csv'
df = pd.read_csv(filename, dtype='string')
# Function to extract errors from JSON response.
def get_errors(metadata):
if isinstance(metadata, dict):
all_errors = []
error_list = metadata['errorList']['error']
for error in error_list:
error_message = error['errorMessage']
all_errors.append(error_message)
all_errors = '|'.join(all_errors)
error = all_errors
else:
error = metadata
return error
all_items = []
for count, row in df.iterrows():
row = row
current_barcode = row['current_barcode']
new_barcode = row['new_barcode']
rmst_to_add = row['rmst_number']
print(count, current_barcode)
barcode_endpoint = '/almaws/v1/items?item_barcode={}'.format(current_barcode)
get_item_url = baseURL + barcode_endpoint
# Make request for item.
try:
item_metadata = requests.get(get_item_url, headers=headers, timeout=10).json()
except requests.exceptions.RequestException as e:
row['error'] = e
print(e)
all_items.append(row)
continue
try:
# Try getting 'link' field from item_metadata.
full_link = item_metadata['link']
row['full_link'] = full_link
except KeyError:
errors = get_errors(item_metadata)
row['error'] = errors
print(errors)
all_items.append(row)
continue
item_data = item_metadata['item_data']
rmst = item_data['storage_location_id']
item_barcode = item_data['barcode']
if new_barcode != item_barcode:
if rmst_to_add != rmst:
item_data['storage_location_id'] = rmst_to_add
item_data['barcode'] = new_barcode
item_metadata.pop('bib_data')
# Convert item_metadata into a json string.
item_metadata = json.dumps(item_metadata)
update_link = full_link
print(update_link)
try:
updated_metadata = requests.put(update_link, headers=headers, data=item_metadata,
timeout=10).json()
except requests.exceptions.RequestException as e:
row['error'] = e
all_items.append(row)
continue
try:
updated_item = updated_metadata['item_data']
updated_description = updated_item['description']
updated_rmst = updated_item['storage_location_id']
row['updated_description'] = updated_description
row['updated_rmst'] = updated_rmst
except KeyError:
errors = get_errors(item_metadata)
row['error'] = errors
else:
row['error'] = 'Item already updated'
all_items.append(row)
updated_df = pd.DataFrame.from_dict(all_items)
dt = datetime.now().strftime('%Y-%m-%d%H.%M.%S')
updated_df.to_csv('updated_lsc_afa_'+dt+'.csv', index=False)
print("--- %s seconds ---" % (time.time() - start_time))