-
Notifications
You must be signed in to change notification settings - Fork 0
/
checker.py
107 lines (76 loc) · 3.14 KB
/
checker.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
import json
import time
import traceback
import requests
import webbrowser
from typing import Iterable
from geopy.distance import geodesic
from python_settings import settings
import settings as local_settings
from models.vaccinespotter.location import Location
settings.configure(local_settings)
assert settings.configured
def main():
print("checking...")
while True:
try:
data = get_mn_data()
locations = get_locations_with_appointments(data)
nearby_locations = get_locations_within_threshold(locations)
locations_with_new_appointments = get_locations_with_new_appointments(nearby_locations)
alert_for_nearby_locations(locations_with_new_appointments)
except Exception as exc:
print(traceback.format_exc())
print(exc)
finally:
time.sleep(settings.CHECK_INTERVAL_SECONDS)
def get_mn_data():
response = requests.get("https://www.vaccinespotter.org/api/v0/states/" + settings.STATE + ".json")
if response.status_code != 200:
raise ConnectionError("Request failed", response)
return json.loads(response.text)
def get_locations_with_appointments(data: dict):
if data["type"] != "FeatureCollection":
raise AttributeError("Unknown data format")
features = data["features"]
locations = []
for feature in features:
if feature["type"] != "Feature":
print("Unknown feature type: " + feature["type"])
properties = feature["properties"]
if properties["appointments_available_all_doses"] is True:
if feature["geometry"]["type"] != "Point":
print("Unknown geometry type: " + feature["geometry"]["type"])
locations.append(Location(feature))
return locations
def get_locations_within_threshold(locations: [Location]):
home_point = (settings.HOME_LATITUDE, settings.HOME_LONGITUDE)
filtered_locations = []
for location in locations:
location_point = (location.latitude, location.longitude)
distance_miles = geodesic(home_point, location_point).miles
if distance_miles <= settings.DISTANCE_THRESHOLD_MILES:
location.distance = distance_miles
filtered_locations.append(location)
return filtered_locations
old_appointments = set()
def get_locations_with_new_appointments(locations: [Location]):
locations_by_id = {location.id: location for location in locations}
global old_appointments
current_appointments = set()
for location in locations:
current_appointments.update(location.appointments)
for appointment in old_appointments:
location = locations_by_id.get(appointment.location_id)
if location:
location.appointments.remove(appointment)
old_appointments = current_appointments
return filter(lambda location: len(location.appointments) > 0, locations_by_id.values())
def alert_for_nearby_locations(locations: Iterable[Location]):
for location in locations:
print("VACCINE GET")
location.print()
print("")
webbrowser.open(location.url, new=1)
if __name__ == "__main__":
main()