-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
106 lines (81 loc) · 3.45 KB
/
main.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
#!/usr/bin/python
import sys
import requests
import threading
from plyer import notification
from prettytable import PrettyTable
def filterCity(response, date):
# Cities used to filter result
cities = ['Essunga', 'Falköping', 'Grästorp', 'Gullspång', 'Götene', 'Hjo', 'Karlsborg',
'Lidköping', 'Mariestad', 'Skara', 'Skövde', 'Tibro', 'Tidaholm', 'Töreboda', 'Vara']
result = []
# Append clinique to result list if all requirements are valid
for clinique in response:
if clinique['hasWebTimebook'] == True and clinique['city'] in cities and filterApointmentType(clinique, date):
result.append(clinique)
return result
def filterApointmentType(clinique, date):
# Covid catagory ids
cat_ids = ['2596', '6898', '1348', '6680'] # TODO: Check if there is an enpoint for retriving all categories with ids
# Appointment types endpoint
appointmentTypes = requests.get(
f'https://booking-api.mittvaccin.se/clinique/{clinique["id"]}/appointmentTypes').json()
for type in appointmentTypes:
if type['categoryId'] in cat_ids and checkSlots(clinique, type, date):
return True
return False
def checkSlots(clinique, type, date):
# Appointment endpoint
appointments = requests.get(
f'https://booking-api.mittvaccin.se/clinique/{clinique["id"]}/appointments/{type["id"]}/slots/{date}').json()
# Checks if there are any appointmens available
for appointment in appointments:
if len(appointment['slots']) > 0:
for slot in appointment['slots']:
if slot['available'] == True:
return True
return False
def main(minutes, date):
try:
# Create console table for output
table = PrettyTable()
table.field_names = ["ID", "City", "Name"]
# Clinique endpoint
response = requests.get(
'https://booking-api.mittvaccin.se/clinique/').json()
cliniques = filterCity(response, date)
# If there are any cliniques add and display them in the console table
if len(cliniques) != 0:
for clinique in cliniques:
table.add_row(
[clinique['id'], clinique['city'], clinique['name']])
# Creates a notification when an appointment is found
title = 'Appointment found!'
message = f"An appointment for Covid-19 was found check the console"
notification.notify(title=title,
message=message,
timeout=10,
toast=False)
# Print result in the console
print(f"{title}\r\n{table}")
else:
print(f"No available appointments. (Retrying in {str(minutes)} minute(s))")
except Exception as ex:
print(f"Exception raised: {ex}")
sys.exit()
def test_main():
# Test case for the main function
response = requests.get(
'https://booking-api.mittvaccin.se/clinique/').json()
cliniques = filterCity(response, '10719-210725')
assert cliniques is not None
if __name__ == "__main__":
if len(sys.argv) > 1:
# Defines the input parameters
minutes = int(sys.argv[1])
dateRange = sys.argv[2]
seconds = 60 * minutes
# Starts the threading timer
threading.Timer(seconds, main(minutes, dateRange)).start()
else:
print("Error: no arguments passed")