Initial commit
This commit is contained in:
commit
1235be6935
5 changed files with 116 additions and 0 deletions
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
.venv
|
||||||
|
.env
|
||||||
|
since_id
|
7
Dockerfile
Normal file
7
Dockerfile
Normal file
|
@ -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" ]
|
10
Makefile
Normal file
10
Makefile
Normal file
|
@ -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
|
79
main.py
Normal file
79
main.py
Normal file
|
@ -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"<p>From {notification['account']['display_name']} (@{notification['account']['acct']}):</p>{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()
|
17
requirements.txt
Normal file
17
requirements.txt
Normal file
|
@ -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
|
Loading…
Reference in a new issue