79 lines
2.3 KiB
Python
79 lines
2.3 KiB
Python
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()
|