-
Notifications
You must be signed in to change notification settings - Fork 142
/
doctoshotgun.py
executable file
·952 lines (805 loc) · 38.6 KB
/
doctoshotgun.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
#!/usr/bin/env python3
import sys
import os
import re
import logging
import tempfile
from time import sleep
import json
from urllib.parse import urlparse
import datetime
import argparse
from pathlib import Path
import getpass
import unicodedata
from dateutil.parser import parse as parse_date
from dateutil.relativedelta import relativedelta
import calendar
import cloudscraper
import colorama
from requests.adapters import ReadTimeout, ConnectionError
from termcolor import colored
from urllib import parse
from urllib3.exceptions import NewConnectionError
from woob.browser.exceptions import ClientError, ServerError, HTTPNotFound
from woob.browser.browsers import LoginBrowser, StatesMixin
from woob.browser.url import URL
from woob.browser.pages import JsonPage, HTMLPage
from woob.tools.log import createColoredFormatter
from woob.tools.misc import get_backtrace
SLEEP_INTERVAL_AFTER_CONNECTION_ERROR = 5
SLEEP_INTERVAL_AFTER_LOGIN_ERROR = 10
SLEEP_INTERVAL_AFTER_CENTER = 1
SLEEP_INTERVAL_AFTER_RUN = 5
try:
from playsound import playsound as _playsound, PlaysoundException
def playsound(*args):
try:
return _playsound(*args)
except (PlaysoundException, ModuleNotFoundError):
pass # do not crash if, for one reason or another, something wrong happens
except ImportError:
def playsound(*args):
pass
def log(text, *args, **kwargs):
args = (colored(arg, 'yellow') for arg in args)
if 'color' in kwargs:
text = colored(text, kwargs.pop('color'))
text = text % tuple(args)
print(text, **kwargs)
def log_ts(text=None, *args, **kwargs):
''' Log with timestamp'''
now = datetime.datetime.now()
print("[%s]" % now.isoformat(" ", "seconds"))
if text:
log(text, *args, **kwargs)
class Session(cloudscraper.CloudScraper):
def send(self, *args, **kwargs):
callback = kwargs.pop('callback', lambda future, response: response)
is_async = kwargs.pop('is_async', False)
if is_async:
raise ValueError('Async requests are not supported')
resp = super().send(*args, **kwargs)
return callback(self, resp)
class LoginPage(JsonPage):
def redirect(self):
return self.doc['redirection']
class SendAuthCodePage(JsonPage):
def build_doc(self, content):
return "" # Do not choke on empty response from server
class ChallengePage(JsonPage):
def build_doc(self, content):
return "" # Do not choke on empty response from server
class CentersPage(HTMLPage):
def on_load(self):
try:
v = self.doc.xpath('//input[@id="wait-time-value"]')[0]
except IndexError:
return
raise WaitingInQueue(int(v.attrib['value']))
def iter_centers_ids(self):
for div in self.doc.xpath('//div[@class="js-dl-search-results-calendar"]'):
data = json.loads(div.attrib['data-props'])
yield data['searchResultId']
def get_next_page(self):
# French doctolib uses data-u attribute of span-element to create the link when user hovers span
for span in self.doc.xpath('//div[contains(@class, "next")]/span'):
if not span.attrib.has_key('data-u'):
continue
# How to find the corresponding javascript-code:
# Press F12 to open dev-tools, select elements-tab, find div.next, right click on element and enable break on substructure change
# Hover "Next" element and follow callstack upwards
# JavaScript:
# var t = (e = r()(e)).data("u")
# , n = atob(t.replace(/\s/g, '').split('').reverse().join(''));
import base64
href = base64.urlsafe_b64decode(''.join(span.attrib['data-u'].split())[::-1]).decode()
query = dict(parse.parse_qsl(parse.urlsplit(href).query))
if 'page' in query:
return int(query['page'])
for a in self.doc.xpath('//div[contains(@class, "next")]/a'):
href = a.attrib['href']
query = dict(parse.parse_qsl(parse.urlsplit(href).query))
if 'page' in query:
return int(query['page'])
return None
class CenterResultPage(JsonPage):
pass
class CenterPage(HTMLPage):
pass
class CenterBookingPage(JsonPage):
def find_motive(self, regex, singleShot=False):
for s in self.doc['data']['visit_motives']:
# ignore case as some doctors use their own spelling
if re.search(regex, s['name'], re.IGNORECASE):
if s['allow_new_patients'] == False:
log('Motive %s not allowed for new patients at this center. Skipping vaccine...',
s['name'], flush=True)
continue
if not singleShot and not s['first_shot_motive']:
log('Skipping second shot motive %s...',
s['name'], flush=True)
continue
return s['id']
return None
def get_motives(self):
return [s['name'] for s in self.doc['data']['visit_motives']]
def get_places(self):
return self.doc['data']['places']
def get_practice(self):
return self.doc['data']['places'][0]['practice_ids'][0]
def get_agenda_ids(self, motive_id, practice_id=None):
agenda_ids = []
for a in self.doc['data']['agendas']:
if motive_id in a['visit_motive_ids'] and \
not a['booking_disabled'] and \
(not practice_id or a['practice_id'] == practice_id):
agenda_ids.append(str(a['id']))
return agenda_ids
def get_profile_id(self):
return self.doc['data']['profile']['id']
class AvailabilitiesPage(JsonPage):
def find_best_slot(self, start_date=None, end_date=None, excluded_weekdays=[]):
for a in self.doc['availabilities']:
date = parse_date(a['date']).date()
if start_date and date < start_date or end_date and date > end_date:
continue
if date.weekday() in excluded_weekdays:
continue
if len(a['slots']) == 0:
continue
return a['slots'][-1]
class AppointmentPage(JsonPage):
def get_error(self):
return self.doc['error']
def is_error(self):
return 'error' in self.doc
class AppointmentEditPage(JsonPage):
def get_custom_fields(self):
for field in self.doc['appointment']['custom_fields']:
if field['required']:
yield field
class AppointmentPostPage(JsonPage):
pass
class MasterPatientPage(JsonPage):
def get_patients(self):
return self.doc
def get_name(self):
return '%s %s' % (self.doc[0]['first_name'], self.doc[0]['last_name'])
class WaitingInQueue(Exception):
pass
class CityNotFound(Exception):
pass
class Doctolib(LoginBrowser, StatesMixin):
# individual properties for each country. To be defined in subclasses
BASEURL = ""
vaccine_motives = {}
centers = URL('')
center = URL('')
# common properties
login = URL('/login.json', LoginPage)
send_auth_code = URL('/api/accounts/send_auth_code', SendAuthCodePage)
challenge = URL('/login/challenge', ChallengePage)
center_result = URL(r'/search_results/(?P<id>\d+).json', CenterResultPage)
center_booking = URL(r'/booking/(?P<center_id>.+).json', CenterBookingPage)
availabilities = URL(r'/availabilities.json', AvailabilitiesPage)
second_shot_availabilities = URL(
r'/second_shot_availabilities.json', AvailabilitiesPage)
appointment = URL(r'/appointments.json', AppointmentPage)
appointment_edit = URL(
r'/appointments/(?P<id>.+)/edit.json', AppointmentEditPage)
appointment_post = URL(
r'/appointments/(?P<id>.+).json', AppointmentPostPage)
master_patient = URL(r'/account/master_patients.json', MasterPatientPage)
def _setup_session(self, profile):
session = Session()
session.hooks['response'].append(self.set_normalized_url)
if self.responses_dirname is not None:
session.hooks['response'].append(self.save_response)
self.session = session
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.session.headers['sec-fetch-dest'] = 'document'
self.session.headers['sec-fetch-mode'] = 'navigate'
self.session.headers['sec-fetch-site'] = 'same-origin'
self.session.headers['User-Agent'] = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36'
self.patient = None
def locate_browser(self, state):
# When loading state, do not locate browser on the last url.
pass
def do_login(self, code):
try:
self.open(self.BASEURL + '/sessions/new')
except ServerError as e:
if e.response.status_code in [503] \
and 'text/html' in e.response.headers['Content-Type'] \
and ('cloudflare' in e.response.text or 'Checking your browser before accessing' in e .response.text):
log('Request blocked by CloudFlare', color='red')
if e.response.status_code in [520]:
log('Cloudflare is unable to connect to Doctolib server. Please retry later.', color='red')
raise
try:
self.login.go(json={'kind': 'patient',
'username': self.username,
'password': self.password,
'remember': True,
'remember_username': True})
except ClientError:
print('Wrong login/password')
return False
if self.page.redirect() == "/sessions/two-factor":
print("Requesting 2fa code...")
if not code:
if not sys.__stdin__.isatty():
log("Auth Code input required, but no interactive terminal available. Please provide it via command line argument '--code'.", color='red')
return False
self.send_auth_code.go(
json={'two_factor_auth_method': 'email'}, method="POST")
code = input("Enter auth code: ")
try:
self.challenge.go(
json={'auth_code': code, 'two_factor_auth_method': 'email'}, method="POST")
except HTTPNotFound:
print("Invalid auth code")
return False
return True
def find_centers(self, where, motives=None, page=1):
if motives is None:
motives = self.vaccine_motives.keys()
for city in where:
try:
self.centers.go(where=city, params={
'ref_visit_motive_ids[]': motives, 'page': page})
except ServerError as e:
if e.response.status_code in [503]:
if 'text/html' in e.response.headers['Content-Type'] \
and ('cloudflare' in e.response.text or
'Checking your browser before accessing' in e .response.text):
log('Request blocked by CloudFlare', color='red')
return
if e.response.status_code in [520]:
log('Cloudflare is unable to connect to Doctolib server. Please retry later.', color='red')
return
raise
except HTTPNotFound as e:
raise CityNotFound(city) from e
next_page = self.page.get_next_page()
for i in self.page.iter_centers_ids():
page = self.center_result.open(
id=i,
params={
'limit': '4',
'ref_visit_motive_ids[]': motives,
'speciality_id': '5494',
'search_result_format': 'json'
}
)
try:
yield page.doc['search_result']
except KeyError:
pass
if next_page:
for center in self.find_centers(where, motives, next_page):
yield center
def get_patients(self):
self.master_patient.go()
return self.page.get_patients()
@classmethod
def normalize(cls, string):
nfkd = unicodedata.normalize('NFKD', string)
normalized = u"".join(
[c for c in nfkd if not unicodedata.combining(c)])
normalized = re.sub(r'\W', '-', normalized)
return normalized.lower()
def try_to_book(self, center, vaccine_list, start_date, end_date, excluded_weekdays, only_second, only_third, dry_run=False, confirm=False):
try:
self.open(center['url'])
except ClientError as e:
# Sometimes there are referenced centers which are not available anymore (410 Gone)
log('Error: %s', e, color='red')
return False
p = urlparse(center['url'])
center_id = p.path.split('/')[-1]
center_page = self.center_booking.go(center_id=center_id)
profile_id = self.page.get_profile_id()
# extract motive ids based on the vaccine names
motives_id = dict()
for vaccine in vaccine_list:
motives_id[vaccine] = self.page.find_motive(
r'.*({})'.format(vaccine), singleShot=(vaccine == self.vaccine_motives[self.KEY_JANSSEN] or only_second or only_third))
motives_id = dict((k, v)
for k, v in motives_id.items() if v is not None)
if len(motives_id.values()) == 0:
log('Unable to find requested vaccines in motives', color='red')
log('Motives: %s', ', '.join(self.page.get_motives()), color='red')
return False
for place in self.page.get_places():
if place['name']:
log('– %s...', place['name'])
practice_id = place['practice_ids'][0]
for vac_name, motive_id in motives_id.items():
log(' Vaccine %s...', vac_name, end=' ', flush=True)
agenda_ids = center_page.get_agenda_ids(motive_id, practice_id)
if len(agenda_ids) == 0:
# do not filter to give a chance
agenda_ids = center_page.get_agenda_ids(motive_id)
if self.try_to_book_place(profile_id, motive_id, practice_id, agenda_ids, vac_name.lower(), start_date, end_date, excluded_weekdays, only_second, only_third, dry_run, confirm):
return True
return False
def try_to_book_place(self, profile_id, motive_id, practice_id, agenda_ids, vac_name, start_date, end_date, excluded_weekdays, only_second, only_third, dry_run=False, confirm=False):
date = start_date.strftime('%Y-%m-%d')
while date is not None:
self.availabilities.go(
params={'start_date': date,
'visit_motive_ids': motive_id,
'agenda_ids': '-'.join(agenda_ids),
'insurance_sector': 'public',
'practice_ids': practice_id,
'destroy_temporary': 'true',
'limit': 3})
if 'next_slot' in self.page.doc:
date = self.page.doc['next_slot']
else:
date = None
if len(self.page.doc['availabilities']) == 0:
log('no availabilities', color='red')
return False
slot = self.page.find_best_slot(start_date, end_date)
if not slot:
if only_second == False and only_third == False:
log('First slot not found :(', color='red')
else:
log('Slot not found :(', color='red')
return False
# depending on the country, the slot is returned in a different format. Go figure...
if isinstance(slot, dict) and 'start_date' in slot:
slot_date_first = slot['start_date']
if vac_name != "janssen":
slot_date_second = slot['steps'][1]['start_date']
elif isinstance(slot, str):
if vac_name != "janssen" and not only_second and not only_third:
log('Only one slot for multi-shot vaccination found')
# should be for Janssen, second or third shots only, otherwise it is a list
slot_date_first = slot
elif isinstance(slot, list):
slot_date_first = slot[0]
if vac_name != "janssen": # maybe redundant?
slot_date_second = slot[1]
else:
log('Error while fetching first slot.', color='red')
return False
if vac_name != "janssen" and not only_second and not only_third:
assert slot_date_second
log('found!', color='green')
log(' ├╴ Best slot found: %s', parse_date(
slot_date_first).strftime('%c'))
appointment = {'profile_id': profile_id,
'source_action': 'profile',
'start_date': slot_date_first,
'visit_motive_ids': str(motive_id),
}
data = {'agenda_ids': '-'.join(agenda_ids),
'appointment': appointment,
'practice_ids': [practice_id]}
headers = {
'content-type': 'application/json',
}
self.appointment.go(data=json.dumps(data), headers=headers)
if self.page.is_error():
log(' └╴ Appointment not available anymore :( %s', self.page.get_error())
return False
playsound('ding.mp3')
if vac_name != "janssen" and not only_second and not only_third: # janssen has only one shot
self.second_shot_availabilities.go(
params={'start_date': slot_date_second.split('T')[0],
'visit_motive_ids': motive_id,
'agenda_ids': '-'.join(agenda_ids),
'first_slot': slot_date_first,
'insurance_sector': 'public',
'practice_ids': practice_id,
'limit': 3})
second_slot = self.page.find_best_slot()
if not second_slot:
log(' └╴ No second shot found')
return False
# in theory we could use the stored slot_date_second result from above,
# but we refresh with the new results to play safe
if isinstance(second_slot, dict) and 'start_date' in second_slot:
slot_date_second = second_slot['start_date']
elif isinstance(slot, str):
slot_date_second = second_slot
# TODO: is this else needed?
# elif isinstance(slot, list):
# slot_date_second = second_slot[1]
else:
log('Error while fetching second slot.', color='red')
return False
log(' ├╴ Second shot: %s', parse_date(
slot_date_second).strftime('%c'))
data['second_slot'] = slot_date_second
self.appointment.go(data=json.dumps(data), headers=headers)
if self.page.is_error():
log(' └╴ Appointment not available anymore :( %s',
self.page.get_error())
return False
a_id = self.page.doc['id']
self.appointment_edit.go(id=a_id)
log(' ├╴ Booking for %(first_name)s %(last_name)s...' % self.patient)
self.appointment_edit.go(
id=a_id, params={'master_patient_id': self.patient['id']})
custom_fields = {}
for field in self.page.get_custom_fields():
if field['id'] == 'cov19':
value = 'Non'
elif field['placeholder']:
value = field['placeholder']
else:
for key, value in field.get('options', []):
print(' │ %s %s' % (colored(key, 'green'), colored(value, 'yellow')))
print(' ├╴ %s%s:' % (field['label'], (' (%s)' % field['placeholder']) if field['placeholder'] else ''),
end=' ', flush=True)
value = sys.stdin.readline().strip()
custom_fields[field['id']] = value
if dry_run:
log(' └╴ Booking status: %s', 'fake')
return True
if confirm:
print(' ├╴ Do you want to book it? (y/N)', end=' ', flush=True)
if sys.stdin.readline().strip().lower() != 'y':
log(' └╴ Skipped')
return False
data = {'appointment': {'custom_fields_values': custom_fields,
'new_patient': True,
'qualification_answers': {},
'referrer_id': None,
},
'bypass_mandatory_relative_contact_info': False,
'email': None,
'master_patient': self.patient,
'new_patient': True,
'patient': None,
'phone_number': None,
}
self.appointment_post.go(id=a_id, data=json.dumps(
data), headers=headers, method='PUT')
if 'redirection' in self.page.doc and not 'confirmed-appointment' in self.page.doc['redirection']:
log(' ├╴ Open %s to complete', self.BASEURL +
self.page.doc['redirection'])
self.appointment_post.go(id=a_id)
log(' └╴ Booking status: %s', self.page.doc['confirmed'])
return self.page.doc['confirmed']
class DoctolibDE(Doctolib):
BASEURL = 'https://www.doctolib.de'
KEY_PFIZER = '6768'
KEY_PFIZER_SECOND = '6769'
KEY_PFIZER_THIRD = '9039'
KEY_MODERNA = '6936'
KEY_MODERNA_SECOND = '6937'
KEY_MODERNA_THIRD = '9040'
KEY_JANSSEN = '7978'
KEY_ASTRAZENECA = '7109'
KEY_ASTRAZENECA_SECOND = '7110'
vaccine_motives = {
KEY_PFIZER: 'Pfizer',
KEY_PFIZER_SECOND: 'Zweit.*Pfizer|Pfizer.*Zweit',
KEY_PFIZER_THIRD: 'Auffrischung.*Pfizer|Pfizer.*Auffrischung|Dritt.*Pfizer|Booster.*Pfizer',
KEY_MODERNA: 'Moderna',
KEY_MODERNA_SECOND: 'Zweit.*Moderna|Moderna.*Zweit',
KEY_MODERNA_THIRD: 'Auffrischung.*Moderna|Moderna.*Auffrischung|Dritt.*Moderna|Booster.*Moderna',
KEY_JANSSEN: 'Janssen',
KEY_ASTRAZENECA: 'AstraZeneca',
KEY_ASTRAZENECA_SECOND: 'Zweit.*AstraZeneca|AstraZeneca.*Zweit',
}
centers = URL(r'/impfung-covid-19-corona/(?P<where>\w+)', CentersPage)
center = URL(r'/praxis/.*', CenterPage)
class DoctolibFR(Doctolib):
BASEURL = 'https://www.doctolib.fr'
KEY_PFIZER = '6970'
KEY_PFIZER_SECOND = '6971'
KEY_PFIZER_THIRD = '8192'
KEY_MODERNA = '7005'
KEY_MODERNA_SECOND = '7004'
KEY_MODERNA_THIRD = '8193'
KEY_JANSSEN = '7945'
KEY_ASTRAZENECA = '7107'
KEY_ASTRAZENECA_SECOND = '7108'
vaccine_motives = {
KEY_PFIZER: 'Pfizer',
KEY_PFIZER_SECOND: '2de.*Pfizer',
KEY_PFIZER_THIRD: '3e.*Pfizer',
KEY_MODERNA: 'Moderna',
KEY_MODERNA_SECOND: '2de.*Moderna',
KEY_MODERNA_THIRD: '3e.*Moderna',
KEY_JANSSEN: 'Janssen',
KEY_ASTRAZENECA: 'AstraZeneca',
KEY_ASTRAZENECA_SECOND: '2de.*AstraZeneca',
}
centers = URL(r'/vaccination-covid-19/(?P<where>\w+)', CentersPage)
center = URL(r'/centre-de-sante/.*', CenterPage)
class Application:
DATA_DIRNAME = (Path(os.environ.get("XDG_DATA_HOME") or Path.home() / ".local" / "share")) / 'doctoshotgun'
STATE_FILENAME = DATA_DIRNAME / 'state.json'
@classmethod
def create_default_logger(cls):
# stderr logger
format = '%(asctime)s:%(levelname)s:%(name)s:' \
':%(filename)s:%(lineno)d:%(funcName)s %(message)s'
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(createColoredFormatter(sys.stderr, format))
return handler
def setup_loggers(self, level):
logging.root.handlers = []
logging.root.setLevel(level)
logging.root.addHandler(self.create_default_logger())
def load_state(self):
try:
with open(self.STATE_FILENAME, 'r') as fp:
state = json.load(fp)
except IOError:
return {}
else:
return state
def save_state(self, state):
if not os.path.exists(self.DATA_DIRNAME):
os.makedirs(self.DATA_DIRNAME)
with open(self.STATE_FILENAME, 'w') as fp:
json.dump(state, fp)
def main(self, cli_args=None):
colorama.init() # needed for windows
doctolib_map = {
"fr": DoctolibFR,
"de": DoctolibDE
}
parser = argparse.ArgumentParser(
description="Book a vaccine slot on Doctolib")
parser.add_argument('--debug', '-d', action='store_true',
help='show debug information')
parser.add_argument('--pfizer', '-z', action='store_true',
help='select only Pfizer vaccine')
parser.add_argument('--moderna', '-m', action='store_true',
help='select only Moderna vaccine')
parser.add_argument('--janssen', '-j', action='store_true',
help='select only Janssen vaccine')
parser.add_argument('--astrazeneca', '-a', action='store_true',
help='select only AstraZeneca vaccine')
parser.add_argument('--only-second', '-2',
action='store_true', help='select only second dose')
parser.add_argument('--only-third', '-3',
action='store_true', help='select only third dose')
parser.add_argument('--patient', '-p', type=int,
default=-1, help='give patient ID')
parser.add_argument('--time-window', '-t', type=int, default=7,
help='set how many next days the script look for slots (default = 7)')
parser.add_argument(
'--center', '-c', action='append', help='filter centers')
parser.add_argument(
'--zipcode', action='append', help='filter centers by zipcode (e.g. 76012)', type=str)
parser.add_argument('--center-regex',
action='append', help='filter centers by regex')
parser.add_argument('--center-exclude', '-x',
action='append', help='exclude centers')
parser.add_argument('--center-exclude-regex',
action='append', help='exclude centers by regex')
parser.add_argument(
'--include-neighbor-city', '-n', action='store_true', help='include neighboring cities')
parser.add_argument('--start-date', type=str, default=None,
help='first date on which you want to book the first slot (format should be DD/MM/YYYY)')
parser.add_argument('--end-date', type=str, default=None,
help='last date on which you want to book the first slot (format should be DD/MM/YYYY)')
parser.add_argument('--weekday-exclude', '-w', nargs='*', type=str, default=[], action='append',
help='Exclude specific weekdays, e.g. "tuesday Wednesday FRIDAY"')
parser.add_argument('--dry-run', action='store_true',
help='do not really book the slot')
parser.add_argument('--confirm', action='store_true',
help='prompt to confirm before booking')
parser.add_argument(
'country', help='country where to book', choices=list(doctolib_map.keys()))
parser.add_argument('city', help='city where to book')
parser.add_argument('username', help='Doctolib username')
parser.add_argument('password', nargs='?', help='Doctolib password')
parser.add_argument('--code', type=str, default=None, help='2FA code')
args = parser.parse_args(cli_args if cli_args else sys.argv[1:])
if args.debug:
responses_dirname = tempfile.mkdtemp(prefix='woob_session_')
self.setup_loggers(logging.DEBUG)
else:
responses_dirname = None
self.setup_loggers(logging.WARNING)
if not args.password:
args.password = getpass.getpass()
docto = doctolib_map[args.country](
args.username, args.password, responses_dirname=responses_dirname)
docto.load_state(self.load_state())
try:
if not docto.do_login(args.code):
return 1
patients = docto.get_patients()
if len(patients) == 0:
print("It seems that you don't have any Patient registered in your Doctolib account. Please fill your Patient data on Doctolib Website.")
return 1
if args.patient >= 0 and args.patient < len(patients):
docto.patient = patients[args.patient]
elif len(patients) > 1:
print('Available patients are:')
for i, patient in enumerate(patients):
print('* [%s] %s %s' %
(i, patient['first_name'], patient['last_name']))
while True:
print('For which patient do you want to book a slot?',
end=' ', flush=True)
try:
docto.patient = patients[int(sys.stdin.readline().strip())]
except (ValueError, IndexError):
continue
else:
break
else:
docto.patient = patients[0]
motives = []
if not args.pfizer and not args.moderna and not args.janssen and not args.astrazeneca:
if args.only_second:
motives.append(docto.KEY_PFIZER_SECOND)
motives.append(docto.KEY_MODERNA_SECOND)
# motives.append(docto.KEY_ASTRAZENECA_SECOND) #do not add AstraZeneca by default
elif args.only_third:
if not docto.KEY_PFIZER_THIRD and not docto.KEY_MODERNA_THIRD:
print('Invalid args: No third shot vaccinations in this country')
return 1
motives.append(docto.KEY_PFIZER_THIRD)
motives.append(docto.KEY_MODERNA_THIRD)
else:
motives.append(docto.KEY_PFIZER)
motives.append(docto.KEY_MODERNA)
motives.append(docto.KEY_JANSSEN)
# motives.append(docto.KEY_ASTRAZENECA) #do not add AstraZeneca by default
if args.pfizer:
if args.only_second:
motives.append(docto.KEY_PFIZER_SECOND)
elif args.only_third:
if not docto.KEY_PFIZER_THIRD: # not available in all countries
print('Invalid args: Pfizer has no third shot in this country')
return 1
motives.append(docto.KEY_PFIZER_THIRD)
else:
motives.append(docto.KEY_PFIZER)
if args.moderna:
if args.only_second:
motives.append(docto.KEY_MODERNA_SECOND)
elif args.only_third:
if not docto.KEY_MODERNA_THIRD: # not available in all countries
print('Invalid args: Moderna has no third shot in this country')
return 1
motives.append(docto.KEY_MODERNA_THIRD)
else:
motives.append(docto.KEY_MODERNA)
if args.janssen:
if args.only_second or args.only_third:
print('Invalid args: Janssen has no second or third shot')
return 1
else:
motives.append(docto.KEY_JANSSEN)
if args.astrazeneca:
if args.only_second:
motives.append(docto.KEY_ASTRAZENECA_SECOND)
elif args.only_third:
print('Invalid args: AstraZeneca has no third shot')
return 1
else:
motives.append(docto.KEY_ASTRAZENECA)
vaccine_list = [docto.vaccine_motives[motive] for motive in motives]
if args.start_date:
try:
start_date = datetime.datetime.strptime(
args.start_date, '%d/%m/%Y').date()
except ValueError as e:
print('Invalid value for --start-date: %s' % e)
return 1
else:
start_date = datetime.date.today()
if args.end_date:
try:
end_date = datetime.datetime.strptime(
args.end_date, '%d/%m/%Y').date()
except ValueError as e:
print('Invalid value for --end-date: %s' % e)
return 1
else:
end_date = start_date + relativedelta(days=args.time_window)
_day_names = dict((name.lower(), k) for k, name in enumerate(calendar.day_name))
try:
excluded_weekdays = [d for days in args.weekday_exclude for d in days]
excluded_weekdays = set(sorted([_day_names[d.lower()] for d in excluded_weekdays]))
except KeyError as e:
print('Invalid element value for --excluded-weekday: %s' % e)
return 1
log('Starting to look for vaccine slots for %s %s between %s and %s...',
docto.patient['first_name'], docto.patient['last_name'], start_date, end_date)
if len(excluded_weekdays) != 0:
log('Excluded weekdays: %s', ', '.join([calendar.day_name[d] for d in excluded_weekdays]))
log('Vaccines: %s', ', '.join(vaccine_list))
log('Country: %s ', args.country)
log('This may take a few minutes/hours, be patient!')
cities = [docto.normalize(city) for city in args.city.split(',')]
while True:
log_ts()
try:
for center in docto.find_centers(cities, motives):
if not args.include_neighbor_city and not docto.normalize(center['city']).startswith(tuple(cities)):
logging.debug("Skipping city '%(city)s' %(name_with_title)s" % center)
continue
if args.center:
if center['name_with_title'] not in args.center:
logging.debug("Skipping center '%s'" %
center['name_with_title'])
continue
if args.zipcode:
center_matched = False
for zipcode in args.zipcode:
if center['zipcode'] == zipcode:
center_matched = True
if not center_matched:
logging.debug("Skipping center '%(name_with_title)s' ['%(zipcode)s']" % center)
continue
if args.center_regex:
center_matched = False
for center_regex in args.center_regex:
if re.match(center_regex, center['name_with_title']):
center_matched = True
else:
logging.debug(
"Skipping center '%(name_with_title)s'" % center)
if not center_matched:
continue
if args.center_exclude:
if center['name_with_title'] in args.center_exclude:
logging.debug(
"Skipping center '%(name_with_title)s' because it's excluded" % center)
continue
if args.center_exclude_regex:
center_excluded = False
for center_exclude_regex in args.center_exclude_regex:
if re.match(center_exclude_regex, center['name_with_title']):
logging.debug(
"Skipping center '%(name_with_title)s' because it's excluded" % center)
center_excluded = True
if center_excluded:
continue
log('')
log('Center %(name_with_title)s (%(city)s):' % center)
if docto.try_to_book(center, vaccine_list, start_date, end_date, excluded_weekdays, args.only_second, args.only_third, args.dry_run, args.confirm):
log('')
log('💉 %s Congratulations.' %
colored('Booked!', 'green', attrs=('bold',)))
return 0
sleep(SLEEP_INTERVAL_AFTER_CENTER)
log('')
log('No free slots found at selected centers. Trying another round in %s sec...', SLEEP_INTERVAL_AFTER_RUN)
sleep(SLEEP_INTERVAL_AFTER_RUN)
except CityNotFound as e:
print('\n%s: City %s not found. Make sure you selected a city from the available countries.' % (
colored('Error', 'red'), colored(e, 'yellow')))
return 1
except WaitingInQueue as waiting_time:
log('Within the queue, estimated waiting time %s minutes', waiting_time)
sleep(30)
except (ReadTimeout, ConnectionError, NewConnectionError) as e:
print('\n%s' % (colored(
'Connection error. Check your internet connection. Retrying ...', 'red')))
print(str(e))
sleep(SLEEP_INTERVAL_AFTER_CONNECTION_ERROR)
except Exception as e:
template = "An unexpected exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(e).__name__, e.args)
print(message)
if args.debug:
print(get_backtrace())
return 1
return 0
finally:
self.save_state(docto.dump_state())
if __name__ == '__main__':
try:
sys.exit(Application().main())
except KeyboardInterrupt:
print('Abort.')
sys.exit(1)