-
Notifications
You must be signed in to change notification settings - Fork 34
/
luxmedSnip.py
335 lines (280 loc) · 12.5 KB
/
luxmedSnip.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
import argparse
import datetime
import inspect
import json
import logging
import pathlib
import shelve
import sys
import time
import yaml
from typing import Any
from loguru import logger
import jsonschema
import requests
import schedule
class LuxMedSniper:
LUXMED_LOGIN_URL = 'https://portalpacjenta.luxmed.pl/PatientPortal/Account/LogIn'
NEW_PORTAL_RESERVATION_URL = 'https://portalpacjenta.luxmed.pl/PatientPortal/NewPortal/terms/index'
def __init__(self, configuration_files):
logger.info("LuxMedSniper logger initialized")
self._loadConfiguration(configuration_files)
self._setup_providers()
self._createSession()
self._logIn()
def _createSession(self):
self.session = requests.Session()
def validate(self) -> None:
schema_file = pathlib.Path("schema.json")
with schema_file.open(encoding="utf-8") as f:
schema = json.load(f)
jsonschema.validate(instance=self.config, schema=schema)
def _loadConfiguration(self, configuration_files):
def merge(a: dict[str, Any], b: dict[str, Any], error_path: str = "") -> dict[str, Any]:
for key in b:
if key in a:
if isinstance(a[key], dict) and isinstance(b[key], dict):
merge(a[key], b[key], f"{error_path}.{key}")
elif a[key] == b[key]:
pass
else:
raise LuxmedSniperError(f"Conflict at {error_path}.{key}")
else:
a[key] = b[key]
return a
self.config: dict[str, Any] = {}
for configuration_file in configuration_files:
configuration_path = pathlib.Path(configuration_file).expanduser()
with configuration_path.open(encoding="utf-8") as stream:
cf = yaml.load(stream, Loader=yaml.FullLoader)
self.config = merge(self.config, cf)
self.validate()
def _logIn(self):
json_data = {
"login": self.config["luxmed"]["email"],
"password": self.config["luxmed"]["password"],
}
response = self.session.post(
url=LuxMedSniper.LUXMED_LOGIN_URL,
json=json_data,
headers={"Content-Type": "application/json"},
)
logger.debug("Login response: {}.\nLogin cookies: {}", response.text, response.cookies)
if response.status_code != requests.codes["ok"]:
raise LuxmedSniperError(f"Unexpected response {response.status_code}, cannot log in")
logger.info("Successfully logged in!")
self.session.cookies = response.cookies
for k, v in self.session.cookies.items():
self.session.headers.update({k: v})
token = json.loads(response.text)["token"]
self.session.headers["authorization-token"] = f"Bearer {token}"
def _parseVisitsNewPortal(self, data, clinic_ids: list[int], doctor_ids: list[int]) -> list[dict]:
appointments = []
content = data.json()
for termForDay in content["termsForService"]["termsForDays"]:
for term in termForDay["terms"]:
doctor = term['doctor']
clinic_id = int(term["clinicGroupId"])
doctor_id = int(doctor["id"])
if doctor_ids and doctor_id not in doctor_ids:
continue
if clinic_ids and clinic_id not in clinic_ids:
continue
appointments.append(
{
'AppointmentDate': datetime.datetime.fromisoformat(term['dateTimeFrom']),
'ClinicId': term['clinicId'],
'ClinicPublicName': term['clinic'],
'DoctorName': f'{doctor["academicTitle"]} {doctor["firstName"]} {doctor["lastName"]}',
'ServiceId': term['serviceId']
}
)
return appointments
def _getAppointmentsNewPortal(self):
try:
(cityId, serviceId, clinicIds, doctorIds) = self.config['luxmedsniper'][
'doctor_locator_id'].strip().split('*')
clinicIds = [*filter(lambda x: x != -1, map(int, clinicIds.split(",")))]
clinic_ids = clinicIds + self.config["luxmedsniper"].get("facilities_ids", [])
doctor_ids = [*filter(lambda x: x != -1, map(int, doctorIds.split(",")))]
except ValueError as err:
raise LuxmedSniperError("DoctorLocatorID seems to be in invalid format") from err
lookup_days = self.config["luxmedsniper"]["lookup_time_days"]
date_to = datetime.date.today() + datetime.timedelta(days=lookup_days)
params = {
"searchPlace.id": cityId,
"searchPlace.type": 0,
"serviceVariantId": serviceId,
"languageId": 10,
"searchDateFrom": datetime.date.today().strftime("%Y-%m-%d"),
"searchDateTo": date_to.strftime("%Y-%m-%d"),
"searchDatePreset": lookup_days,
"delocalized": "false",
}
if clinic_ids:
params["facilitiesIds"] = clinic_ids
if doctor_ids:
params["doctorsIds"] = doctor_ids
response = self.session.get(url=LuxMedSniper.NEW_PORTAL_RESERVATION_URL, params=params)
logger.debug(response.text)
return [
*filter(
lambda appointment: appointment["AppointmentDate"].date() <= date_to,
self._parseVisitsNewPortal(response, clinic_ids, doctor_ids),
)
]
def check(self):
appointments = self._getAppointmentsNewPortal()
if not appointments:
logger.info("No appointments found.")
return
for appointment in appointments:
logger.info(
"Appointment found! {AppointmentDate} at {ClinicPublicName} - {DoctorName}".format(
**appointment))
if not self._isAlreadyKnown(appointment):
self._addToDatabase(appointment)
self._send_notification(appointment)
logger.info(
"Notification sent! {AppointmentDate} at {ClinicPublicName} - {DoctorName}".format(**appointment))
else:
logger.info('Notification was already sent.')
def _addToDatabase(self, appointment):
db = shelve.open(self.config['misc']['notifydb'])
notifications = db.get(appointment['DoctorName'], [])
notifications.append(appointment['AppointmentDate'])
db[appointment['DoctorName']] = notifications
db.close()
def _send_notification(self, appointment):
for provider in self.notification_providers:
provider(appointment)
def _isAlreadyKnown(self, appointment):
db = shelve.open(self.config['misc']['notifydb'])
notifications = db.get(appointment['DoctorName'], [])
db.close()
if appointment['AppointmentDate'] in notifications:
return True
return False
def _setup_providers(self) -> None:
self.notification_providers = []
providers = self.config['luxmedsniper']['notification_provider']
if "pushover" in providers:
pushover_client = PushoverClient(self.config['pushover']['user_key'], self.config['pushover']['api_token'])
# pushover_client.send_message("Luxmed Sniper is running!")
self.notification_providers.append(
lambda appointment: pushover_client.send_message(
self.config['pushover']['message_template'].format(
**appointment, title=self.config['pushover']['title'])))
if "slack" in providers:
from slack_sdk import WebClient
client = WebClient(token=self.config['slack']['api_token'])
channel = self.config['slack']['channel']
self.notification_providers.append(
lambda appointment: client.chat_postMessage(channel=channel,
text=self.config['slack'][
'message_template'].format(
**appointment))
)
if "pushbullet" in providers:
from pushbullet import Pushbullet
pb = Pushbullet(self.config['pushbullet']['access_token'])
self.notification_providers.append(
lambda appointment: pb.push_note(title=self.config['pushbullet']['title'],
body=self.config['pushbullet'][
'message_template'].format(**appointment))
)
if "ntfy" in providers:
def ntfy_callback(appointment):
requests.post(
f"https://ntfy.sh/{self.config['ntfy']['topic']}",
data=self.config["ntfy"]["message_template"].format(**appointment),
headers={"Tags": "hospital,pill,syringe", "Title": "Nowa wizyta"},
timeout=10,
)
self.notification_providers.append(ntfy_callback)
if "gi" in providers:
import gi
gi.require_version('Notify', '0.7')
from gi.repository import Notify
# One time initialization of libnotify
Notify.init("Luxmed Sniper")
self.notification_providers.append(
lambda appointment: Notify.Notification.new(
self.config['gi']['message_template'].format(**appointment), None).show()
)
if "telegram" in providers:
from telegram_send import send as t_send
self.notification_providers.append(
lambda appointment: t_send(messages=[self.config['telegram']['message_template'].format(**appointment)], conf=self.config['telegram']['tele_conf_path'])
)
def work(config):
try:
luxmed_sniper = LuxMedSniper(config)
luxmed_sniper.check()
except LuxmedSniperError as s:
logger.error(s)
class LuxmedSniperError(Exception):
pass
class PushoverClient:
def __init__(self, user_key, api_token):
self.api_token = api_token
self.user_key = user_key
def send_message(self, message):
data = {
'token': self.api_token,
'user': self.user_key,
'message': message
}
r = requests.post('https://api.pushover.net/1/messages.json', data=data)
if r.status_code != 200:
raise Exception('Pushover error: %s' % r.text)
def setup_logging():
class InterceptHandler(logging.Handler):
def emit(self, record: logging.LogRecord) -> None:
# Get corresponding Loguru level if it exists.
level: str | int
try:
level = logger.level(record.levelname).name
except ValueError:
level = record.levelno
# Find caller from where originated the logged message.
frame, depth = inspect.currentframe(), 0
while frame and (depth == 0 or frame.f_code.co_filename == logging.__file__):
frame = frame.f_back
depth += 1
logger.opt(depth=depth, exception=record.exc_info).log(level, record.getMessage())
logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)
requests_log = logging.getLogger("urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
loguru_config = {
"handlers": [
{"sink": sys.stdout, "level": "INFO"},
{
"sink": "debug.log",
"format": "{time} - {message}",
"serialize": True,
"rotation": "1 week",
},
]
}
logger.configure(handlers=loguru_config["handlers"])
if __name__ == "__main__":
setup_logging()
logger.info("LuxMedSniper - Lux Med Appointment Sniper")
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument(
"-c", "--config",
help="Configuration file path", default=["luxmedSniper.yaml"],
nargs="*"
)
parser.add_argument(
"-d", "--delay",
type=int, help="Delay in fetching updates [s]", default=1800
)
args = parser.parse_args()
work(args.config)
schedule.every(args.delay).seconds.do(work, args.config)
while True:
schedule.run_pending()
time.sleep(1)