wyzely-detect/set-detect-notify/utils/notify.py

33 lines
962 B
Python
Raw Normal View History

2023-10-02 01:56:40 +01:00
import datetime
import httpx
def construct_ntfy_headers(
2023-10-05 03:03:11 +01:00
title: str = "Object/Person Detected",
tag="rotating_light", # https://docs.ntfy.sh/publish/#tags-emojis
priority="default", # https://docs.ntfy.sh/publish/#message-priority
) -> dict:
return {"Title": title, "Priority": priority, "Tags": tag}
2023-10-02 01:56:40 +01:00
2023-10-05 03:03:11 +01:00
def send_notification(data: str, headers: dict, url: str):
2023-10-02 01:56:40 +01:00
if url is None or data is None:
2023-10-05 03:03:11 +01:00
raise ValueError("url and data cannot be None")
httpx.post(url, data=data.encode("utf-8"), headers=headers)
2023-10-02 01:56:40 +01:00
def check_last_seen(last_seen: datetime.datetime, seconds: int = 15):
2023-10-05 03:03:11 +01:00
"""
Check if a time is older than a given number of seconds
If it is, return True
If last_seen is empty/null, return True
"""
if (
datetime.datetime.now() - last_seen > datetime.timedelta(seconds=seconds)
or last_seen == ""
or last_seen is None
):
return True
else:
return False