This repository was archived by the owner on Nov 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
61 lines (55 loc) · 2 KB
/
main.py
File metadata and controls
61 lines (55 loc) · 2 KB
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
import os
import requests
import logging
from dotenv import load_dotenv
from time import sleep
from pushnotify import PushMessage
load_dotenv()
YEARS = os.environ.get('YEARS').split(',')
YEARS.sort()
URL = "https://user-api.coronatest.nl/vaccinatie/programma/bepaalbaar/YEAR/NEE/NEE"
GOTIFY_KEY = os.environ.get("GOTIFY_KEY")
GOTIFY_URL = os.environ.get("GOTIFY_URL")
def send_push_message(year: int):
msg_title= "Corona vaccination available!"
msg_content = f"The Corona vaccination for {str(year)} is available!"
push = PushMessage(GOTIFY_URL, GOTIFY_KEY)
push.post_msg(msg_title, msg_content, 10)
logger.info(f"Year {str(year)} available, notification send ")
def main():
for year in YEARS:
if year == None or year == "":
return False
r_URL = URL.replace("YEAR", str(year))
logger.debug(f"Trying {r_URL} for year {str(year)}")
response = requests.get(r_URL)
if response.status_code == 200:
data = response.json()
logger.debug(f"Response JSON: {data}")
if data['success'] == True:
send_push_message(year)
# Remove first item from sorted YEARS-list to prevent sending
# of multiple notifications
YEARS.pop(0)
return True
else:
logger.debug(f"Year {str(year)} not available")
return True
else:
logger.error(response.status_code, response.text)
return False
if __name__ == "__main__":
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
try:
while True:
logger.info("Starting program")
if not main():
break
sleep(120)
except KeyboardInterrupt:
logger.info("Exiting program")