-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathNotifier.py
More file actions
47 lines (35 loc) · 1.32 KB
/
Copy pathNotifier.py
File metadata and controls
47 lines (35 loc) · 1.32 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
from typing import Any, Optional
import requests
class Notifier:
"""Sends error notifications to an ntfy topic.
Configured via the ``ntfy`` section of ``config/settings.yml``::
ntfy:
host: ntfy.sh
topic: instapaper-errors
Both ``host`` and ``topic`` are optional. If either is missing, the
notifier is disabled and ``notify_error`` becomes a no-op.
"""
def __init__(self, host: Optional[str] = None, topic: Optional[str] = None) -> None:
self._host = host
self._topic = topic
@classmethod
def from_settings(cls, settings: dict[str, Any]) -> "Notifier":
ntfy_settings = settings.get("ntfy") or {}
if not isinstance(ntfy_settings, dict):
ntfy_settings = {}
return cls(
host=ntfy_settings.get("host"),
topic=ntfy_settings.get("topic"),
)
@property
def enabled(self) -> bool:
return bool(self._host and self._topic)
def notify_error(self, message: str) -> None:
if not self.enabled:
return
url = f"https://{self._host.rstrip('/')}/{self._topic.lstrip('/')}"
try:
requests.post(url, data=message.encode("utf-8"), timeout=10)
except Exception:
# Notification failures must never break the main run
pass