new spectra format (#1302)
This commit is contained in:
@@ -25,12 +25,12 @@ import time
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Any, Callable, Awaitable, List, Dict, Optional
|
from typing import Any, Callable, Awaitable, List, Dict, Optional
|
||||||
|
|
||||||
import zstandard as zstd
|
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from websockets.asyncio.client import connect as wsconnect
|
from websockets.asyncio.client import connect as wsconnect
|
||||||
|
|
||||||
from BOT.storage import insert_match, insert_player_games
|
from BOT.storage import insert_match, insert_player_games
|
||||||
from BOT.autologging import process_game as autolog_process_game
|
from BOT.autologging import process_game as autolog_process_game
|
||||||
|
from spectra_ws_payload import SpectraPayloadError, decode_spectra_ws_payload
|
||||||
|
|
||||||
_HERE = Path(__file__).resolve().parent
|
_HERE = Path(__file__).resolve().parent
|
||||||
load_dotenv(dotenv_path=_HERE / ".env")
|
load_dotenv(dotenv_path=_HERE / ".env")
|
||||||
@@ -113,7 +113,6 @@ async def listen(
|
|||||||
return
|
return
|
||||||
|
|
||||||
headers = {"Authorization": _auth_header()}
|
headers = {"Authorization": _auth_header()}
|
||||||
decompressor = zstd.ZstdDecompressor()
|
|
||||||
reconnect_delay = 1
|
reconnect_delay = 1
|
||||||
received = 0
|
received = 0
|
||||||
|
|
||||||
@@ -128,16 +127,10 @@ async def listen(
|
|||||||
log.info("Connected.")
|
log.info("Connected.")
|
||||||
reconnect_delay = 1 # reset on successful connect
|
reconnect_delay = 1 # reset on successful connect
|
||||||
async for message in ws:
|
async for message in ws:
|
||||||
raw = message.encode("utf-8") if isinstance(message, str) else bytes(message)
|
|
||||||
try:
|
try:
|
||||||
text = decompressor.decompress(raw, max_output_size=64 * 1024 * 1024).decode("utf-8")
|
data = decode_spectra_ws_payload(message)
|
||||||
except zstd.ZstdError:
|
except SpectraPayloadError as exc:
|
||||||
text = raw.decode("utf-8")
|
log.warning("Bad Spectra WS frame, skipping: %s", exc)
|
||||||
|
|
||||||
try:
|
|
||||||
data = json.loads(text)
|
|
||||||
except json.JSONDecodeError:
|
|
||||||
log.warning("Bad JSON frame, skipping")
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
games = normalize(data)
|
games = normalize(data)
|
||||||
|
|||||||
Reference in New Issue
Block a user