-
Notifications
You must be signed in to change notification settings - Fork 1
/
checkall.py
83 lines (61 loc) · 2.69 KB
/
checkall.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
import requests
import webbrowser
import json
import os
from datetime import date
from collections import defaultdict
appointment_link = "https://www.doctolib.de/institut/berlin/ciz-berlin-berlin?pid=practice-{practice}"
availability_url = "https://www.doctolib.de/availabilities.json?start_date={today}&visit_motive_ids={motive}&agenda_ids={agenda}&insurance_sector=public&practice_ids={practice}&destroy_temporary=true&limit=4"
agendas_url = "https://www.doctolib.de/booking/ciz-berlin-berlin.json"
slack_webhook_url = os.environ['SLACK_WEBHOOK']
request_headers = {
'User-Agent': 'Impferator',
}
def slack_notification(amount, url):
msg = "{amount} vaccination appointments are available here: {url}"
msg_formatted = {'text': msg.format(amount=amount, url=url)}
response = requests.post(
slack_webhook_url, data=json.dumps(msg_formatted),
headers={'Content-Type': 'application/json'}
)
if response.status_code != 200:
raise ValueError(
'Request to slack returned an error %s, the response is:\n%s'
% (response.status_code, response.text)
)
def generate_availability_url(practice_id, motive_id, agendas_string):
today = date.today().strftime("%Y-%m-%d")
url = availability_url.format(
today=today,
motive=motive_id,
agenda=agendas_string,
practice=practice_id
)
return url
def group_agendas(agendas):
agenda_ids = defaultdict(lambda: defaultdict(list))
for agenda in agendas:
if agenda['booking_disabled'] or agenda['booking_temporary_disabled']:
continue
if not agenda['visit_motive_ids']:
continue
practice_id = agenda['practice_id']
motive_id = agenda['visit_motive_ids'][0]
agenda_ids[practice_id][motive_id].append(str(agenda['id']))
return agenda_ids
def check():
agendas_json = requests.get(agendas_url).json()
agendas_by_practice = group_agendas(agendas_json['data']['agendas'])
for practice_id, agendas_by_motives in agendas_by_practice.items():
for motive_id, agendas in agendas_by_motives.items():
agendas_string = "-".join(agendas)
availability_url = generate_availability_url(practice_id, motive_id, agendas_string)
#print(availability_url)
availability = requests.get(availability_url, headers=request_headers).json()
if availability['total'] and availability['total'] < 100:
#print(availability['total'])
url = appointment_link.format(practice=practice_id)
slack_notification(availability['total'], url)
#webbrowser.open_new(url)
if __name__ == "__main__":
check()