diff --git a/starbot/core/bot.py b/starbot/core/bot.py index 61a6707..2a2d0a0 100644 --- a/starbot/core/bot.py +++ b/starbot/core/bot.py @@ -89,6 +89,10 @@ class StarBot: await up.connect() except LiveException as ex: logger.error(ex.msg) + try: + await asyncio.wait_for(self.__datasource.wait_for_connects(), config.get("WAIT_FOR_ALL_CONNECTION_TIMEOUT")) + except asyncio.exceptions.TimeoutError: + logger.warning("等待连接所有直播间超时, 请检查是否存在未连接成功的直播间") # 启动动态推送模块 asyncio.get_event_loop().create_task(dynamic_spider(self.__datasource)) diff --git a/starbot/core/datasource.py b/starbot/core/datasource.py index ef56645..56e51cf 100644 --- a/starbot/core/datasource.py +++ b/starbot/core/datasource.py @@ -127,6 +127,17 @@ class DataSource(metaclass=abc.ABCMeta): raise DataSourceException(f"不存在的推送 key: {key}") return bot + async def wait_for_connects(self): + """ + 等待所有 Up 实例连接直播间完毕 + """ + while True: + await asyncio.sleep(1) + + flags = [u.is_connecting() for u in self.__up_list] + if not any(flags): + break + class DictDataSource(DataSource): """ diff --git a/starbot/core/room.py b/starbot/core/room.py index 645e0cd..f0d63f3 100644 --- a/starbot/core/room.py +++ b/starbot/core/room.py @@ -75,6 +75,9 @@ class Up(BaseModel): def dispatch(self, name, data): self.__room.dispatch(name, data) + def is_connecting(self): + return (self.__room is not None) and (self.__room.get_status() != 2) + def __any_live_on_enabled(self): return any(map(lambda conf: conf.enabled, map(lambda group: group.live_on, self.targets))) @@ -104,15 +107,17 @@ class Up(BaseModel): if user_info["live_room"] is None: raise LiveException(f"UP 主 {self.uname} ( UID: {self.uid} ) 还未开通直播间") self.room_id = user_info["live_room"]["roomid"] - self.__live_room = LiveRoom(self.room_id, get_credential()) - self.__room = LiveDanmaku(self.room_id, credential=get_credential()) # 开播推送开关和下播推送开关均处于关闭状态时跳过连接直播间,以节省性能 if config.get("ONLY_CONNECT_NECESSARY_ROOM"): - if not any([self.__any_live_on_enabled(), self.__any_live_off_enabled(), self.__any_live_report_enabled()]): + if not any([self.__any_live_on_enabled(), self.__any_live_off_enabled(), + self.__any_live_report_enabled()]): logger.warning(f"{self.uname} 的开播, 下播和直播报告开关均处于关闭状态, 跳过连接直播间") return + self.__live_room = LiveRoom(self.room_id, get_credential()) + self.__room = LiveDanmaku(self.room_id, credential=get_credential()) + logger.opt(colors=True).info(f"准备连接到 {self.uname} 的直播间 {self.room_id}") self.__loop.create_task(self.__room.connect()) diff --git a/starbot/utils/config.py b/starbot/utils/config.py index 50ec6cd..c92f7f9 100644 --- a/starbot/utils/config.py +++ b/starbot/utils/config.py @@ -36,6 +36,9 @@ SIMPLE_CONFIG = { "BILI_JCT": None, "BUVID3": None, + # 成功连接所有主播直播间的最大等待时长,可使得日志输出顺序更加易读,一般无需修改此处,单位:秒 + "WAIT_FOR_ALL_CONNECTION_TIMEOUT": 60, + # 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能 "ONLY_CONNECT_NECESSARY_ROOM": False, # 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能 @@ -128,6 +131,9 @@ FULL_CONFIG = { "BILI_JCT": None, "BUVID3": None, + # 成功连接所有主播直播间的最大等待时长,可使得日志输出顺序更加易读,一般无需修改此处,单位:秒 + "WAIT_FOR_ALL_CONNECTION_TIMEOUT": 60, + # 是否自动判断仅连接必要的直播间,即当某直播间的开播、下播、直播报告开关均未开启时,自动跳过连接直播间,以节省性能 "ONLY_CONNECT_NECESSARY_ROOM": False, # 是否自动判断仅处理必要的直播事件,例如当某直播间的下播推送和直播报告中均不包含弹幕相关功能,则不再处理此直播间的弹幕事件,以节省性能