Created
July 26, 2026 07:01
-
-
Save wyf9/0b09ed81986a439262da9d89ba93b518 to your computer and use it in GitHub Desktop.
Sleepy Events Listener (for Sleepy v5)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Sleepy Events Listener by wyf9 (for Sleepy v5) | |
| Dependencies: | |
| $ uv add requests sseclient-py deepdiff rich | |
| Or run it directly: | |
| $ uv run --with httpx-sse --with deepdiff --with rich sleepy-v5-listener.py http://sleepy.host # REPLACE HOST | |
| ''' | |
| import asyncio | |
| import json | |
| from datetime import datetime | |
| from sys import argv | |
| import httpx | |
| from deepdiff import DeepDiff | |
| from httpx_sse import aconnect_sse | |
| from rich.console import Console | |
| try: | |
| HOST = argv[1] | |
| except Exception: | |
| HOST = input('Input Host: ') | |
| if not HOST.startswith(('http://', 'https://')): | |
| print('Invaild host!') | |
| exit(1) | |
| HOST = HOST.rstrip('/') | |
| console = Console() | |
| old_event = None | |
| async def main(): | |
| delay = 1 | |
| async with httpx.AsyncClient(timeout=None) as client: | |
| while True: | |
| try: | |
| async with aconnect_sse( | |
| client, | |
| "GET", | |
| f"{HOST}/api/status/events", | |
| ) as event_source: | |
| delay = 1 | |
| async for event in event_source.aiter_sse(): | |
| if event.event != "update": | |
| continue | |
| data = json.loads(event.data) | |
| print( | |
| f"\n========== [{datetime.now().isoformat()}] ==========" | |
| ) | |
| print(f"========== Event #{event.id or '?'} ==========") | |
| global old_event | |
| if old_event is None: | |
| console.print_json(data=data) | |
| else: | |
| diff = DeepDiff( | |
| old_event, | |
| data, | |
| ignore_order=True, | |
| ) | |
| console.print_json(data=diff.to_dict()) | |
| old_event = data | |
| except KeyboardInterrupt: | |
| break | |
| except Exception as e: | |
| print(f"Disconnected: {e}") | |
| print(f"Retry in {delay}s...") | |
| await asyncio.sleep(delay) | |
| delay = min(delay * 2, 30) | |
| asyncio.run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment