Add WebSocket announce listener to both IRC bot plugins
- Limnoria: add wsUrl, announceChannel config; __init__, die, _ws_listener - Sopel: add ws_url, announce_channel config; setup/shutdown, _ws_listener - Feature parity: subscribe to WS, receive announce msgs, send to IRC channel - Deferred websocket-client import to avoid load failure if not installed Made-with: Cursor
This commit is contained in:
@@ -5,6 +5,7 @@ from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import threading
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
@@ -121,6 +122,71 @@ _NUMBER_RE = re.compile(r"^!(\d+)$")
|
||||
class NtrPlaylist(callbacks.Plugin):
|
||||
"""Query the NtR SoundCloud Fetcher API from IRC."""
|
||||
|
||||
def __init__(self, irc):
|
||||
super().__init__(irc)
|
||||
self._irc = irc
|
||||
self._ws_stop = threading.Event()
|
||||
self._ws_thread = threading.Thread(target=self._ws_listener, daemon=True)
|
||||
self._ws_thread.start()
|
||||
|
||||
def die(self):
|
||||
self._ws_stop.set()
|
||||
super().die()
|
||||
|
||||
def _ws_listener(self):
|
||||
import websocket
|
||||
from supybot import ircmsgs
|
||||
|
||||
backoff = 5
|
||||
max_backoff = 60
|
||||
|
||||
while not self._ws_stop.is_set():
|
||||
ws_url = self.registryValue("wsUrl")
|
||||
token = self.registryValue("adminToken")
|
||||
channel = self.registryValue("announceChannel")
|
||||
|
||||
if not ws_url or not token:
|
||||
LOGGER.warning("wsUrl or adminToken not configured, WS listener sleeping")
|
||||
self._ws_stop.wait(30)
|
||||
continue
|
||||
|
||||
ws = None
|
||||
try:
|
||||
ws = websocket.WebSocket()
|
||||
ws.connect(ws_url, timeout=10)
|
||||
ws.send(json.dumps({"type": "subscribe", "token": token}))
|
||||
LOGGER.info("Connected to announce WebSocket at %s", ws_url)
|
||||
backoff = 5
|
||||
|
||||
while not self._ws_stop.is_set():
|
||||
ws.settimeout(5)
|
||||
try:
|
||||
raw = ws.recv()
|
||||
if not raw:
|
||||
break
|
||||
data = json.loads(raw)
|
||||
if data.get("type") == "announce" and "message" in data:
|
||||
msg = ircmsgs.privmsg(channel, data["message"])
|
||||
self._irc.queueMsg(msg)
|
||||
LOGGER.info("Announced to %s: %s", channel, data["message"])
|
||||
except websocket.WebSocketTimeoutException:
|
||||
continue
|
||||
except websocket.WebSocketConnectionClosedException:
|
||||
break
|
||||
except Exception:
|
||||
LOGGER.exception("WS listener error")
|
||||
finally:
|
||||
if ws:
|
||||
try:
|
||||
ws.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if not self._ws_stop.is_set():
|
||||
LOGGER.info("Reconnecting in %ds", backoff)
|
||||
self._ws_stop.wait(backoff)
|
||||
backoff = min(backoff * 2, max_backoff)
|
||||
|
||||
def _is_admin(self, nick: str) -> bool:
|
||||
admin_nicks = self.registryValue("adminNicks")
|
||||
if not admin_nicks:
|
||||
|
||||
Reference in New Issue
Block a user