Squashed commit of the following: commit3ba0c9b4c6Author: Ashish D'Souza <[email protected]> Date: Sun Nov 19 05:21:27 2023 -0600 Another bugfix commit8a76bddbf1Author: Ashish D'Souza <[email protected]> Date: Sun Nov 19 05:12:40 2023 -0600 Added ntfy router to ansible scripts commit7b51846e1bAuthor: Ashish D'Souza <[email protected]> Date: Sun Nov 19 05:12:14 2023 -0600 Fixed inconsistent variable names bug commita96a14e261Author: Ashish D'Souza <[email protected]> Date: Sun Nov 19 04:53:09 2023 -0600 Added ntfy router to docker compose commitd2f30faa5eAuthor: Ashish D'Souza <[email protected]> Date: Sun Nov 19 04:52:07 2023 -0600 Added ntfy routing
22 lines
794 B
Python
22 lines
794 B
Python
import json
|
|
import asyncio
|
|
from typing import AsyncGenerator
|
|
|
|
import aiohttp
|
|
|
|
|
|
class NtfySubscriber:
|
|
def __init__(self, topics: list[str]):
|
|
self._topics = topics
|
|
|
|
def _generate_ntfy_url(self):
|
|
return 'https://ntfy.homelab.net/' + ','.join(self._topics) + '/json'
|
|
|
|
async def subscribe(self) -> AsyncGenerator[tuple[str, str, str], None]:
|
|
async with aiohttp.ClientSession(raise_for_status=True, timeout=aiohttp.ClientTimeout()) as session:
|
|
async with session.get(self._generate_ntfy_url(), verify_ssl=False) as response:
|
|
async for line in response.content:
|
|
msg = json.loads(line.decode())
|
|
if msg['event'] == 'message':
|
|
yield msg['topic'], msg['title'], msg['message']
|