2023-02-09 12:05:49 -05:00
|
|
|
import urequests
|
|
|
|
|
|
|
|
from . import logging
|
|
|
|
from .config import config
|
|
|
|
from .config import secrets
|
|
|
|
|
|
|
|
|
|
|
|
class Ntfy:
|
|
|
|
def __init__(self):
|
|
|
|
self.hostname = config['hostname']
|
|
|
|
|
|
|
|
host = secrets['ntfy_host']
|
|
|
|
topic = secrets['ntfy_topic']
|
2023-07-03 14:11:10 -04:00
|
|
|
auth = secrets['ntfy_auth']
|
|
|
|
self.url = f'https://{host}/{topic}?auth={auth}'
|
2023-02-09 12:05:49 -05:00
|
|
|
|
|
|
|
def message(self, message):
|
|
|
|
result = self.sendMessage(message)
|
|
|
|
|
|
|
|
try:
|
|
|
|
if result.status_code != 200:
|
|
|
|
self.logError(result)
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
finally:
|
|
|
|
result.close()
|
|
|
|
|
|
|
|
def sendMessage(self, message):
|
|
|
|
return urequests.post(self.url, data=f"{self.hostname} - {message}")
|
|
|
|
|
|
|
|
def logError(self, result):
|
|
|
|
status = result.status_code
|
2023-02-10 10:52:14 -05:00
|
|
|
reason = result.reason.decode('utf-8')
|
2023-02-09 12:05:49 -05:00
|
|
|
|
|
|
|
logging.debug(f'ntfy upload issue: {status} {reason}')
|