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)
-13
View File
@@ -33,8 +33,6 @@ def parse_args() -> argparse.Namespace:
parser.add_argument("--no-numbers", action="store_true")
parser.add_argument("--list-devices", action="store_true")
parser.add_argument("--debug", "-d", action="store_true")
parser.add_argument("--fake-room", type=int, default=0, metavar="PORT",
help="connect to local fake_room.py instead of real Bilibili (dev/testing only)")
return parser.parse_args()
@@ -60,17 +58,6 @@ async def tts_worker(queue: asyncio.Queue, bridge: TTSBridge,
async def main_async(args: argparse.Namespace) -> int:
if args.fake_room:
import importlib
fake = importlib.import_module("fake_room")
fake.patch_blivedm(args.fake_room)
logger.info("[dev] connected to fake room on port %d", args.fake_room)
elif args.room_id == 1:
import importlib
fake = importlib.import_module("fake_room")
fake.patch_blivedm(8081)
logger.info("[dev] room 1 -> fake room on port 8081")
if args.list_devices:
devices = get_output_devices()
print("Available audio output devices:")
-24
View File
@@ -151,21 +151,6 @@ async def index_handler(request: web.Request) -> web.Response:
return web.FileResponse(STATIC_DIR / "index.html")
FAKE_ROOM_PORT = 8081 # fake_room.py default = bot web UI port (8080) + 1
def _apply_fake_room(room_id: int) -> None:
"""room_id == 1 routes to the local fake room (dev/testing); otherwise restore real Bilibili."""
import importlib
fake = importlib.import_module("fake_room")
if room_id == 1:
fake.patch_blivedm(FAKE_ROOM_PORT)
logger.info("[dev] room 1 -> fake room on port %d", FAKE_ROOM_PORT)
else:
fake.unpatch_blivedm()
async def api_start(request: web.Request) -> web.Response:
try:
config = await request.json()
@@ -188,7 +173,6 @@ async def api_start(request: web.Request) -> web.Response:
await tts_service.stop()
except Exception:
pass
_apply_fake_room(room_id)
tts_service.set_cookies(login_session.cookies if login_session.is_logged_in else None)
try:
await tts_service.start(config)
@@ -316,8 +300,6 @@ def parse_args() -> argparse.Namespace:
parser.add_argument("--port", "-p", type=int, default=8080)
parser.add_argument("--host", type=str, default="127.0.0.1")
parser.add_argument("--debug", "-d", action="store_true")
parser.add_argument("--fake-room", type=int, default=0, metavar="PORT",
help="connect to local fake_room.py instead of real Bilibili (dev/testing only)")
return parser.parse_args()
@@ -330,12 +312,6 @@ def main() -> int:
)
logging.getLogger("aiohttp.access").setLevel(logging.WARNING)
if args.fake_room:
import importlib
fake = importlib.import_module("fake_room")
fake.patch_blivedm(args.fake_room)
print(f" [dev] connected to fake room on port {args.fake_room}")
app = create_app()
print(f"\n Bilibili Live → Yukkuri TTS Web UI")
print(f" Open: http://{args.host}:{args.port}\n")