commit 1235be6935b4d8c199e4de5013ae10cf31f675d0 Author: Erick Ruiz de Chavez <953140+eruizdechavez@users.noreply.github.com> Date: Mon Dec 16 07:14:52 2024 -0500 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4576abf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.venv +.env +since_id diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..2319e9c --- /dev/null +++ b/Dockerfile @@ -0,0 +1,7 @@ +FROM python:3.13-alpine +WORKDIR /app +COPY requirements.txt . +RUN pip install -r requirements.txt && touch since_id +COPY main.py . +ENTRYPOINT [ "python3" ] +CMD [ "main.py" ] diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8402240 --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ +all: build-quiet run + +build: + @docker build . -t notifications_pushover + +build-quiet: + @docker build . -t notifications_pushover -q 2>&1 >/dev/null + +run: + @docker run --rm --env-file .env -v $$PWD/since_id:/app/since_id notifications_pushover diff --git a/main.py b/main.py new file mode 100644 index 0000000..8b9817e --- /dev/null +++ b/main.py @@ -0,0 +1,79 @@ +from os import environ + +import requests + + +def get_since_id() -> str: + with open("since_id", mode="r", encoding="utf8") as since_file: + since_id = since_file.read() + return since_id + + +def set_since_id(since_id: str) -> None: + with open("since_id", mode="w", encoding="utf8") as since_file: + since_file.write(since_id) + + +def fetch_notifications(): + since_id = get_since_id() + since_param = "" + + if since_id != "": + since_param = f"&since_id={since_id}" + + token: str = environ.get("NOTIFICATIONS_PUSHOVER_GTS_TOKEN", "") + notifications_url: str = environ.get("NOTIFICATIONS_PUSHOVER_GTS_URL", "") + headers = {"Authorization": f"Bearer {token}"} + + response: requests.Response = requests.get( + f"{notifications_url}?types[]=mention{since_param}", + headers=headers, + timeout=10, + ) + return response.json() + + +def push_notification(notification: dict) -> None: + token: str = environ.get("NOTIFICATIONS_PUSHOVER_PO_TOKEN", "") + user: str = environ.get("NOTIFICATIONS_PUSHOVER_PO_USER", "") + url: str = environ.get( + "NOTIFICATIONS_PUSHOVER_PO_URL", "shortcuts://run-shortcut?name=Open in Mona" + ) + url_title: str = environ.get("NOTIFICATIONS_PUSHOVER_PO_URL_TITLE", "Open in Mona") + + requests.post( + "https://api.pushover.net/1/messages.json", + params={ + "token": token, + "user": user, + "title": "New Mention", + "message": f"
From {notification['account']['display_name']} (@{notification['account']['acct']}):
{notification['status']['content']}", + "html": 1, + "url": f"{url}&text={notification['status']['url']}", + "url_title": url_title, + }, + timeout=10, + ) + + +def main() -> None: + notifications = fetch_notifications() + + if len(notifications) == 0: + print("No new notifications") + return + + print(f"Found {len(notifications)} new notifications") + + while notifications: + notification = notifications.pop() + print( + f"From {notification['account']['display_name']} (@{notification['account']['acct']}): {notification['status']['content']}\n" + ) + push_notification(notification) + + set_since_id(notification["id"]) + + +if __name__ == "__main__": + main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..64824f4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,17 @@ +astroid==3.3.6 +black==24.10.0 +certifi==2024.12.14 +charset-normalizer==3.4.0 +click==8.1.7 +dill==0.3.9 +idna==3.10 +isort==5.13.2 +mccabe==0.7.0 +mypy-extensions==1.0.0 +packaging==24.2 +pathspec==0.12.1 +platformdirs==4.3.6 +pylint==3.3.2 +requests==2.32.3 +tomlkit==0.13.2 +urllib3==2.2.3