refactor: fake room routing moved to connect-time, fully decoupled from production

- server.py / main.py no longer reference fake_room at all (no import,
  no --fake-room flag, no _apply_fake_room)
- Routing decision happens at connect time in DanmakuClient.start():
  room_id == 1 dynamically imports fake_room.py and patches blivedm;
  any other room id returns immediately without touching fake_room
- Switching back from room 1 to another room unpatches to real Bilibili
- Production (packaged, no fake_room.py) is completely unaffected for
  normal room ids; room 1 without fake_room.py raises a clear error
- Rebuild portable zip (83.9 MB)
This commit is contained in:
2026-08-08 17:59:38 +08:00
parent d4de39f294
commit 4f7168a0e3
3 changed files with 23 additions and 37 deletions
+23
View File
@@ -7,6 +7,7 @@ Rules come from event_rules.json (see event_rules.py).
import asyncio
import logging
import sys
from typing import Optional
import blivedm
@@ -20,6 +21,27 @@ from event_rules import (
logger = logging.getLogger(__name__)
FAKE_ROOM_PORT = 8081 # fake_room.py default = bot web UI port (8080) + 1
def _maybe_apply_fake_room(room_id: int) -> None:
"""Connect-time routing: room_id == 1 uses the local fake_room.py (dev only).
fake_room.py is gitignored and never packaged. For any other room id this
returns immediately without ever importing fake_room, so production
(which has no fake_room.py) is completely unaffected.
"""
fake = sys.modules.get("fake_room")
if room_id == 1:
if fake is None:
import importlib
fake = importlib.import_module("fake_room")
fake.patch_blivedm(FAKE_ROOM_PORT)
logger.info("[dev] room 1 -> fake room on port %d", FAKE_ROOM_PORT)
elif fake is not None:
# previously routed to fake room; switch back to real Bilibili
fake.unpatch_blivedm()
INTERACT_TYPE_NAMES = {
3: "\u5206\u4eab", 4: "\u7279\u522b\u5173\u6ce8", 5: "\u4e92\u7c89",
}
@@ -178,6 +200,7 @@ class DanmakuClient:
async def start(self) -> None:
import aiohttp
_maybe_apply_fake_room(self._room_id)
self._session = await build_danmaku_session(self._cookies)
self._client = blivedm.BLiveClient(self._room_id, session=self._session)
handler = DanmakuHandler(self._queue, self._rules)