-
Notifications
You must be signed in to change notification settings - Fork 0
/
gesy-vaccine-finder.py
executable file
·381 lines (294 loc) · 12.9 KB
/
gesy-vaccine-finder.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env python3
import sys
import os
import re
import json
import logging
import datetime
import subprocess
from collections import defaultdict
from http import cookiejar
from urllib import request
from urllib.parse import parse_qs, urlencode, urlsplit
from urllib.error import HTTPError
from socketserver import ThreadingMixIn
from http.server import HTTPServer, BaseHTTPRequestHandler
class HTTPRequestHandler(BaseHTTPRequestHandler):
demo = True
timeout = 30
centers = None
cookies = cookiejar.CookieJar()
def do_GET(self):
_, _, path, query, _ = urlsplit(self.path)
params = parse_qs(query)
logging.info("do_GET: path=%s, params=%s", path, params)
if path == "/":
return self.get_home_page()
if path == "/login":
return self.send_html_page("login")
if path == "/login-tfa":
self.send_tfa_request()
return self.send_html_page("login-tfa")
if path == "/logout":
return self.do_logout()
if path == "/gateway-status":
return self.get_gateway_status()
if path == "/user-info":
return self.get_user_info()
if path == "/vaccination-centers":
return self.get_vaccination_centers()
if path == "/vaccination-center-timeslots":
return self.get_vaccination_center_timeslots(params)
self.send_error(404, "do_GET: %s not supported!", self.path)
def do_POST(self):
logging.debug("POST %s", self.path)
content_length = int(self.headers['Content-Length'])
data = parse_qs(self.rfile.read(content_length).decode("utf-8"))
logging.info("POST %s: %s", self.path, data)
if self.path == "/login":
return self.do_login(data)
if self.path == "/login-tfa":
return self.do_login_tfa(data)
if self.path == "/appointment":
return self.do_appointment(data)
self.send_error(404, "POST {} not supported!".format(self.path))
def is_demo(self):
return self.__class__.demo
def set_demo(self, value):
logging.info("demo mode: %s", value)
if not value:
self.__class__.centers = None
self.__class__.demo = value
def read_html(self, name):
return open(os.path.join("pages", "{}.html".format(name)), mode='rb').read()
def send_html(self, html):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(html)
def send_html_page(self, name):
self.send_html(self.read_html(name))
def send_json(self, obj):
self.send_response(200)
self.send_header("Content-type", "application/json; charset=UTF-8")
self.end_headers()
self.wfile.write(
json.dumps(obj, ensure_ascii=False, indent=3).encode("utf-8")
)
def read_demo_json(self, name):
return json.load(open(os.path.join("demo", "{}.json".format(name))))
def send_redirect(self, url, code=302):
self.send_response(302)
self.send_header("Location", url)
self.end_headers()
def get_home_page(self):
html = self.read_html("index")
if self.is_demo():
html = html.replace(b"GESY Vaccination", b"GESY Vaccination (DEMO)")
html = html.replace(b"Logout", b"Login")
html = html.replace(b"/logout", b"/login")
return self.send_html(html)
def send_tfa_request(self):
text = self.make_text_request("https://vaccination.gesy.org.cy/api/tfa/request", data=b"{}")
logging.info("send_tfa_request: %s", text)
def do_login(self, data):
self.make_request("https://vaccination.gesy.org.cy")
self.make_request(
"https://oam.gesy.org.cy/oam/server/auth_cred_submit",
data=urlencode(dict(username=data["username"][0], password=data["password"][0])).encode("utf-8")
)
tfa_status = self.make_text_request("https://vaccination.gesy.org.cy/api/tfa/status")
if "2FA_PROMPT" in tfa_status:
self.set_demo(False)
return self.send_redirect("/login-tfa")
logging.warning("unknown tfa response: %s", tfa_status)
self.send_redirect("/login")
def do_login_tfa(self, data):
code = data["code"][0]
text = self.make_text_request("https://vaccination.gesy.org.cy/api/tfa/validate?otp={}".format(code), data=b"{}")
logging.info("tfa validate: %s", text)
if "2FA_OTP_VALID" in text:
return self.send_redirect("/")
return self.send_redirect("/login-tfa")
def do_logout(self):
self.make_request(
"https://vaccination.gesy.org.cy/logout?end_url=https://vaccination.gesy.org.cy/"
)
self.set_demo(True)
self.send_redirect("/")
def do_appointment(self, data):
params = {
"name": data["name"][0],
"surname": data["surname"][0],
"dob": data["dob"][0],
"idDocType": data["idDocType"][0],
"idNumber": data["idNumber"][0],
"phonenumber": data["phonenumber"][0],
"email": data["email"][0],
"vcCenter": data["vcCenter"][0],
"vcDate": data["vcDate"][0],
"vcTime": data["vcDate"][0].split("T")[1]
}
logging.info("do_appointment: sending request: %s", params)
if self.is_demo():
text = "DEMO Mode, not submitted"
else:
text = self.make_text_request(
"https://vaccination.gesy.org.cy/vscy/vaccination/insertnewbeneficiaryappointment",
data=urlencode(params).encode("utf-8")
)
logging.info("do_appointment: response: %s", text)
self.send_json(dict(response=text))
def get_demo_status(self):
self.send_json(self.demo)
def get_gateway_status(self):
if self.is_demo():
status = self.read_demo_json("gateway_status")
else:
status = self.make_json_request("https://vaccination.gesy.org.cy/vscy/vaccination/hiogatewayavailability")
if status is None:
logging.error("get_gateway_status: cannot make request")
return
logging.info("get_gateway_status: %s", status)
self.send_json(status)
def get_user_info(self):
if self.is_demo():
user_info = self.read_demo_json("user_info")
user_profile = self.read_demo_json("user_profile")
else:
user = self.make_json_request("https://vaccination.gesy.org.cy/api/user/benctxt")
if user is None:
logging.error("get_user_info: cannot get user")
return
user_info = self.make_json_request(
"https://vaccination.gesy.org.cy/api/beneficiaries/info",
headers={"X-act-for": user["benId"]}
)
user_profile = self.make_json_request("https://vaccination.gesy.org.cy/api/user/profile")
if user_info is None:
logging.error("get_user_info: cannot get user_info")
return
if user_profile is None:
logging.error("get_user_info: cannot get user_info")
return
for k in ["mobile", "email"]:
user_info[k] = user_profile[k]
logging.info("get_user_info: %s", user_info)
self.send_json(user_info)
def get_vaccination_centers(self):
if self.__class__.centers:
return self.send_json(self.__class__.centers)
if self.is_demo():
centers = self.read_demo_json("getcentersallocations")
else:
centers = self.make_json_request("https://vaccination.gesy.org.cy/vscy/vaccination/getcentersallocations")
centers["dict"] = {}
for center in centers["allocations"]:
centers["dict"][center["centerCd"]] = center
center["type-by-days"], center["default-type"] = self.parse_vaccine_type(center["type"])
self.__class__.centers = centers
self.send_json(centers)
@staticmethod
def parse_vaccine_type(text):
if "(" not in text:
return dict(), text
composite_vaccine_type = re.compile(r"([^(]+[(][^)]+[)])[ /]*")
composite_vaccine_dates = re.compile(r"([0-9,]+/[0-9]+/2021),?")
vaccine_type_by_days = dict()
vaccine_type_default = "Unknown"
for vaccine_title in composite_vaccine_type.findall(text):
vaccine_name = vaccine_title.split("(")[0]
for vaccine_dates in composite_vaccine_dates.findall(vaccine_title):
days, month, year = vaccine_dates.split("/")
for day in days.split(","):
date = "{0}-{1:0>2s}-{2:0>2s}".format(year, month, day)
vaccine_type_by_days[date] = vaccine_name
return vaccine_type_by_days, vaccine_type_default
def get_vaccination_center_timeslots(self, params):
center = params["center"][0]
centers = self.__class__.centers
if self.is_demo():
data = self.read_demo_json("gettwoweekslotsbycenter-{}".format(center))
else:
data = self.make_json_request(
"https://vaccination.gesy.org.cy/vscy/Vaccination/gettwoweekslotsbycenter?vcId={}".format(center),
data=b"{}"
)
timeslots = []
for slot in data:
day = slot["vcStartDate"].split("T")[0]
timeslots.append(dict(
id="{}@{}".format(slot["vcStartDate"].rsplit(":", 1)[0], slot["vcId"]),
time=slot["vcStartTime"],
datetime=slot["vcStartDate"],
centerName=centers["dict"][center]["center"],
typeTitle=centers["dict"][center]["type"],
type=centers["dict"][center]["type-by-days"].get(day, centers["dict"][center]["default-type"])
))
self.send_json(timeslots)
def dump_files(self):
logging.info("dump_files")
centers = self.make_json_request("https://vaccination.gesy.org.cy/vscy/vaccination/getcentersallocations")
with open("dump/getcentersallocations.json", "w", encoding='utf-8') as f:
json.dump(centers, fp=f, ensure_ascii=False, indent=3)
for center in centers["allocations"]:
centerCd=center["centerCd"]
logging.info(centerCd)
timeslots = self.make_json_request(
"https://vaccination.gesy.org.cy/vscy/Vaccination/gettwoweekslotsbycenter?vcId={}".format(centerCd),
data=b"{}"
)
with open("dump/gettwoweekslotsbycenter-{}".format(centerCd), "w", encoding='utf-8') as f:
json.dump(timeslots, fp=f, ensure_ascii=False, indent=3)
def make_request(self, url, data=None, headers=None):
headers_list = [
("Accept-Language", "en"),
("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) "
"AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1 Safari/605.1.15"),
]
logging.info("make_request: %s", url)
opener = request.build_opener(
request.HTTPCookieProcessor(self.__class__.cookies)
)
if headers is not None:
headers_list.extend(list(headers.items()))
opener.addheaders = headers_list
try:
res = opener.open(url, data, self.timeout)
except HTTPError as e:
logging.error(e)
return
logging.debug("make_request: status=%d, content-type=%s", res.status, res.info().get_content_type())
return res
def make_text_request(self, url, data=None, headers=None):
res = self.make_request(url, data, headers)
return res.read().decode("utf-8")
def make_json_request(self, url, data=None, headers=None):
res = self.make_request(url, data, headers)
content_type = res.info().get_content_type()
text = res.read().decode("utf-8")
if content_type != "application/json":
logging.error("make_json_request: wrong content type: %s", content_type)
logging.error("make_json_request: text: '%s'", text)
return None
return json.loads(text)
class ThreadingSimpleServer(ThreadingMixIn, HTTPServer):
pass
def open_browser(url):
if sys.platform == "win32":
os.startfile(url)
elif sys.platform == "darwin":
subprocess.Popen(["open", url])
else:
try:
subprocess.Popen(["xdg-open", url])
except OSError:
print("Please open a browser on: %s".format(url))
if __name__ == "__main__":
logging.basicConfig(format='%(asctime)s %(threadName)s %(levelname)s %(message)s', level=logging.DEBUG)
server = ThreadingSimpleServer(("127.0.0.1", 8080), HTTPRequestHandler)
open_browser("http://127.0.0.1:8080")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nShutting down server")