-
Notifications
You must be signed in to change notification settings - Fork 0
/
turns13.py
160 lines (123 loc) · 3.71 KB
/
turns13.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
# !pip install -U sqlalchemy
# !pip install -U psycopg2-binary
# !pip install -U requests
# set up the logging
# https://docs.python.org/3.7/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing
# https://docs.python.org/3/howto/logging-cookbook.html#using-a-rotator-and-namer-to-customize-log-rotation-processing
import gzip
import logging
import logging.handlers
import os
import shutil
def namer(name):
return name + ".gz"
def rotator(source, dest):
with open(source, 'rb') as f_in:
with gzip.open(dest, 'wb') as f_out:
shutil.copyfileobj(f_in, f_out)
os.remove(source)
rh = logging.handlers.RotatingFileHandler(
'turns13.log',
maxBytes=524288,
backupCount=3
)
rh.rotator = rotator
rh.namer = namer
root = logging.getLogger()
root.setLevel(logging.INFO)
root.addHandler(rh)
f = logging.Formatter('%(asctime)s %(name)s %(levelname)s %(message)s')
rh.setFormatter(f)
logging.info('Script Started')
# sierraAPI are helper functions for the REST API
import sierraAPI
import json
import requests
from sqlalchemy import create_engine, text
from datetime import datetime
# read the configuration file
try:
with open("config.json", "r") as f:
config = json.load(f)
client_key = config["client_key"]
client_secret = config["client_secret"]
base_url = config["base_url"]
db_connection_string = config["db_connection_string"]
logging.info(f"base_url: {base_url}")
except:
logging.error('error opening config.json')
# TODO: logging
exit()
# connect to the Sierra DB
try:
sierra_engine = create_engine(db_connection_string)
# print(f"sierra_engine.url: {sierra_engine.url}")
except:
logging.error('error connecting to Sierra DB')
exit()
# get the header for API authorization
try:
headers = sierraAPI.get_access_headers(
client_key=client_key,
client_secret=client_secret,
base_url=base_url
)
r = requests.get(base_url + 'info/token', headers=headers, verify=True)
logging.info(f"token expires in: {r.json()['expiresIn']}")
except:
logging.error(f"Could not get API authorization: {r.text}")
exit()
# get relevant information related to patrons who have turned 13 on this date
sql = """\
select
rm.record_num as patron_record_num,
pr.ptype_code,
pr.expiration_date_gmt::date as expiration_date,
(
select
v.field_content
from
sierra_view.varfield as v
where
v.record_id = rm.id
and v.varfield_type_code = 'b'
order by
v.occ_num
limit
1
) as barcode
FROM
sierra_view.record_metadata as rm
join sierra_view.patron_record as pr on (
pr.record_id = rm.id
and pr.ptype_code in (
1
)
and (pr.birth_date_gmt + interval '13 years')::date = now()::date
)
WHERE
rm.record_type_code = 'p'
and rm.campus_code = ''
"""
# this is the patron type that these will change to in the patron record
data = {
"patronType": 2
}
with sierra_engine.connect() as connection:
try:
result = connection.execute(text(sql))
except:
logging.error('Could not execute sql')
exit()
for i, row in enumerate(result):
try:
url=f"{base_url}patrons/{row['patron_record_num']}"
r = requests.put(
url=url,
headers=headers,
json=data
)
logging.info(f"{i} PUT: {url} patron_data: {row} status_code: {r.status_code}")
except:
logging.error(f"Could not patch patron: {row['patron_record_num']}")
logging.info('Script Finished')