-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmediscout.py
executable file
·399 lines (322 loc) · 15.7 KB
/
mediscout.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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
#!/usr/bin/python3
import base64
import hashlib
import os
import random
import string
import uuid
import argparse
from time import sleep
from urllib.parse import urlparse
import datetime
import requests
from bs4 import BeautifulSoup
from dotenv import load_dotenv
from fake_useragent import UserAgent
from future.backports.urllib.parse import parse_qs
from rich import print_json, print
from rich.console import Console
from medihunter_notifiers import telegram_notify
import sqlite3
console = Console()
# Load environment variables
load_dotenv()
class Authenticator:
def __init__(self, username, password):
self.username = username
self.password = password
self.session = None
self.headers = {
"User-Agent": UserAgent().random,
"Accept": "application/json",
"Authorization": None
}
self.tokenA = None
def generate_code_challenge(self, input):
sha256 = hashlib.sha256(input.encode("utf-8")).digest()
return base64.urlsafe_b64encode(sha256).decode("utf-8").rstrip("=")
def login(self):
self.session = requests.Session()
state = "".join(random.choices(string.ascii_lowercase + string.digits, k=32))
device_id = str(uuid.uuid4())
code_verifier = "".join(uuid.uuid4().hex for _ in range(3))
code_challenge = self.generate_code_challenge(code_verifier)
login_url = "https://login-online24.medicover.pl"
oidc_redirect = "https://online24.medicover.pl/signin-oidc"
auth_params = (
f"?client_id=web&redirect_uri={oidc_redirect}&response_type=code"
f"&scope=openid+offline_access+profile&state={state}&code_challenge={code_challenge}"
"&code_challenge_method=S256&response_mode=query&ui_locales=pl&app_version=3.2.0.482"
"&previous_app_version=3.2.0.482&device_id={device_id}&device_name=Chrome"
)
# Step 1: Initialize login
response = self.session.get(f"{login_url}/connect/authorize{auth_params}", headers=self.headers, allow_redirects=False)
next_url = response.headers.get("Location")
# Step 2: Extract CSRF token
response = self.session.get(next_url, headers=self.headers, allow_redirects=False)
soup = BeautifulSoup(response.content, "html.parser")
csrf_token = soup.find("input", {"name": "__RequestVerificationToken"}).get("value")
# Step 3: Submit login form
login_data = {
"Input.ReturnUrl": f"/connect/authorize/callback{auth_params}",
"Input.LoginType": "FullLogin",
"Input.Username": self.username,
"Input.Password": self.password,
"Input.Button": "login",
"__RequestVerificationToken": csrf_token,
}
response = self.session.post(next_url, data=login_data, headers=self.headers, allow_redirects=False)
next_url = response.headers.get("Location")
# Step 4: Fetch authorization code
response = self.session.get(f"{login_url}{next_url}", headers=self.headers, allow_redirects=False)
next_url = response.headers.get("Location")
code = parse_qs(urlparse(next_url).query)["code"][0]
# Step 5: Exchange code for tokens
token_data = {
"grant_type": "authorization_code",
"redirect_uri": oidc_redirect,
"code": code,
"code_verifier": code_verifier,
"client_id": "web",
}
response = self.session.post(f"{login_url}/connect/token", data=token_data, headers=self.headers)
tokens = response.json()
self.tokenA = tokens["access_token"]
self.headers["Authorization"] = f"Bearer {self.tokenA}"
class DB:
def __init__(self):
self.conn = sqlite3.connect('db/appointments.db')
self.cur = self.conn.cursor()
self.cur.execute("CREATE TABLE IF NOT EXISTS watch(id integer primary key, region, speciality, clinic, doctor, date)")
self.cur.execute("CREATE TABLE IF NOT EXISTS appointment(clinic, doctor, date)")
self.clear_db()
def clear_db(self):
now = datetime.datetime.now().strftime("%Y-%m-%dT%H:%M:%S")
self.cur.execute("DELETE from appointment "
"where date < (?)", (now,))
now = datetime.datetime.now() - datetime.timedelta(days=14)
now = now.strftime("%Y-%m-%d")
self.cur.execute("DELETE from watch "
"where date < (?)", (now,))
self.conn.commit()
def appointment_exists(self, clinic, doctor, date):
res = self.cur.execute("SELECT * from appointment "
"where clinic = (?)"
"AND doctor = (?)"
"AND date = (?)", (clinic, doctor, date))
return res.fetchone() is not None
def add_appointment_history(self, clinic, doctor, date):
self.cur.execute("INSERT INTO appointment VALUES (?, ?, ?)",
(clinic, doctor, date))
self.conn.commit()
def save_watch(self, region, speciality, clinic, doctor, date):
self.cur.execute("INSERT INTO watch VALUES (null, ?, ?, ?, ?, ?)",
(region, speciality, clinic, doctor, date))
self.conn.commit()
def remove_watch(self, watch_id):
self.cur.execute("DELETE from watch where id = (?)", (watch_id,))
self.conn.commit()
def get_watches(self):
watches = self.cur.execute("SELECT * from watch")
return watches.fetchall()
class AppointmentFinder:
def __init__(self, authenticator):
self.authenticator = authenticator
self.authenticator.login()
self.session = authenticator.session
self.headers = authenticator.headers
self.db = DB()
def re_auth(self):
self.authenticator.login()
self.session = self.authenticator.session
self.headers = self.authenticator.headers
def http_get(self, url, params):
response = self.authenticator.session.get(url, headers=self.headers, params=params)
if response.status_code == 200:
return response.json()
elif response.status_code == 401:
console.print(
f"{datetime.datetime.now() } | Response 401. Re-authenticating"
)
self.re_auth()
return self.http_get(url, params)
else:
console.print(
f"[bold red]Error {response.status_code}[/bold red]: {response.text}"
)
return {}
def find_appointments(self, region, specialty, clinic, start_date, doctor=None):
appointment_url = "https://api-gateway-online24.medicover.pl/appointments/api/search-appointments/slots"
params = {
"RegionIds": region,
"SpecialtyIds": specialty,
"ClinicIds": clinic,
"Page": 1,
"PageSize": 5000,
"StartTime": start_date.isoformat(),
"SlotSearchType": 0,
"VisitType": "Center",
}
if doctor:
params["DoctorIds"] = doctor
response = self.http_get(appointment_url, params)
return response.get("items", [])
def get_watches(self):
res = self.db.get_watches()
if res is None:
print("No watches found")
return res
def remove_watch(self, watch_id):
self.db.remove_watch(watch_id)
def save_watch(self, region, specialty, clinic, doctor, date):
self.db.save_watch(region, specialty, clinic, doctor, date)
def save_appointments_and_filter_old(self, appointments):
new_appointments = []
for appointment in appointments:
clinic = appointment.get("clinic", {}).get("id", "N/A")
doctor = appointment.get("doctor", {}).get("id", "N/A")
date = appointment.get("appointmentDate", "N/A")
## check if appointment exists
if not self.db.appointment_exists(clinic, doctor, date):
## Doesnt exists - Save and append
new_appointments.append(appointment)
self.db.add_appointment_history(clinic, doctor, date)
return new_appointments
def find_filters(self, region=None, specialty=None):
filters_url = "https://api-gateway-online24.medicover.pl/appointments/api/search-appointments/filters"
params = {"SlotSearchType": 0}
if region:
params["RegionIds"] = region
if specialty:
params["SpecialtyIds"] = specialty
response = self.http_get(filters_url, params)
return response
class Notifier:
@staticmethod
def format_appointments(appointments):
"""Format appointments into a human-readable string."""
if not appointments:
return "No appointments found."
messages = []
for appointment in appointments:
date = appointment.get("appointmentDate", "N/A")
clinic = appointment.get("clinic", {}).get("name", "N/A")
doctor = appointment.get("doctor", {}).get("name", "N/A")
specialty = appointment.get("specialty", {}).get("name", "N/A")
message = (
f"Date: {date}\n"
f"Clinic: {clinic}\n"
f"Doctor: {doctor}\n"
f"Specialty: {specialty}\n" + "-" * 50
)
messages.append(message)
return "\n".join(messages)
@staticmethod
def send_notification(appointments, notifier, title):
"""Send a notification with formatted appointments."""
message = Notifier.format_appointments(appointments)
if notifier == "telegram":
telegram_notify(message, title)
def display_appointments(appointments):
if not appointments:
console.print("No appointments found.")
else:
for appointment in appointments:
date = appointment.get("appointmentDate", "N/A")
clinic = appointment.get("clinic", {}).get("name", "N/A")
doctor = appointment.get("doctor", {}).get("name", "N/A")
specialty = appointment.get("specialty", {}).get("name", "N/A")
console.print(f"Date: {date}")
console.print(f" Clinic: {clinic}")
console.print(f" Doctor: {doctor}")
console.print(f" Specialty: {specialty}")
console.print("-" * 50)
def main():
parser = argparse.ArgumentParser(description="Find appointment slots.")
subparsers = parser.add_subparsers(dest="command", required=True, help="Command to execute")
find_appointment = subparsers.add_parser("find-appointment", help="Find appointment")
find_appointment.add_argument("-r", "--region", required=True, type=int, help="Region ID")
find_appointment.add_argument("-s", "--specialty", required=True, type=int, action="extend", nargs="+", help="Specialty ID",)
find_appointment.add_argument("-c", "--clinic", required=False, type=int, help="Clinic ID")
find_appointment.add_argument("-d", "--doctor", required=False, type=int, help="Doctor ID")
find_appointment.add_argument("-f", "--date", type=datetime.date.fromisoformat, default=datetime.date.today(), help="Start date in YYYY-MM-DD format")
find_appointment.add_argument("-n", "--notification", required=False, help="Notification method")
find_appointment.add_argument("-t", "--title", required=False, help="Notification title")
add_watch = subparsers.add_parser("add-watch", help="Add watch")
add_watch.add_argument("-r", "--region", required=True, type=int, help="Region ID")
add_watch.add_argument("-s", "--specialty", required=True, type=int, help="Specialty ID",)
add_watch.add_argument("-c", "--clinic", required=False, type=int, help="Clinic ID")
add_watch.add_argument("-d", "--doctor", required=False, type=int, help="Doctor ID")
add_watch.add_argument("-f", "--date", type=datetime.date.fromisoformat, default=datetime.date.today(), help="Start date in YYYY-MM-DD format")
remove_watch = subparsers.add_parser("remove-watch", help="Remove watch")
remove_watch.add_argument("-i", "--id", required=True, type=int, help="Watch Id")
start = subparsers.add_parser("start", help="start watch")
list_watches = subparsers.add_parser("list-watches", help="List watches")
list_filters = subparsers.add_parser("list-filters", help="List filters")
list_filters_subparsers = list_filters.add_subparsers(dest="filter_type", required=True, help="Type of filter to list")
regions = list_filters_subparsers.add_parser("regions", help="List available regions")
specialties = list_filters_subparsers.add_parser("specialties", help="List available specialties")
doctors = list_filters_subparsers.add_parser("doctors", help="List available doctors")
doctors.add_argument("-r", "--region", required=True, type=int, help="Region ID")
doctors.add_argument("-s", "--specialty", required=True, type=int, help="Specialty ID")
args = parser.parse_args()
refresh_time_s = os.environ.get("REFRESH_TIME_S", 60)
refresh_time_s = int(refresh_time_s)
username = os.environ.get("MEDICOVER_USER")
password = os.environ.get("MEDICOVER_PASS")
# Authenticate
auth = Authenticator(username, password)
finder = AppointmentFinder(auth)
if args.command == "find-appointment":
# Find appointments
appointments = finder.find_appointments(args.region, args.specialty, args.clinic, args.date, args.doctor)
## Filter already seen
appointments = finder.save_appointments_and_filter_old(appointments)
# Display appointments
display_appointments(appointments)
# Send notification if appointments are found
if appointments:
Notifier.send_notification(appointments, args.notification, args.title)
elif args.command == "list-filters":
if args.filter_type == "doctors":
filters = finder.find_filters(args.region, args.specialty)
else:
filters = finder.find_filters()
for r in filters[args.filter_type]:
print(f"{r['id']} - {r['value']}")
elif args.command == "add-watch":
finder.save_watch(args.region, args.specialty, args.clinic, args.doctor, args.date)
elif args.command == "remove-watch":
finder.remove_watch(args.id)
elif args.command == "list-watches":
watches = finder.get_watches()
filters = finder.find_filters()
regions = filters.get("regions", {})
specialties = filters.get("specialties", {})
for watch in watches:
region = [r.get("value") for r in regions if r.get("id") == str(watch[1])]
specialty = [r.get("value") for r in specialties if r.get("id") == str(watch[2])]
print(f"Id: {watch[0]}",region[0], f"({watch[1]})", specialty[0], f"({watch[2]})", watch[5])
elif args.command == "start":
while True:
watches = finder.get_watches()
for watch in watches:
print(f"{datetime.datetime.now() } | Running watch: {watch}")
region = watch[1]
specialty = watch[2]
clinic = watch[3]
doctor = watch[4]
date = datetime.datetime.fromisoformat(watch[5])
# Find appointments
appointments = finder.find_appointments(region, specialty, clinic, date, doctor)
## Filter already seen
appointments = finder.save_appointments_and_filter_old(appointments)
# Display appointments
display_appointments(appointments)
# Send notification if appointments are found
if appointments:
speciality = appointments[0].get("specialty", {}).get("name", "N/A")
Notifier.send_notification(appointments, "telegram", speciality)
sleep(refresh_time_s)
if __name__ == "__main__":
main()