feat: Auto connect or disconnect live room when reload user and only connect necessary room config was enabled

This commit is contained in:
LWR
2023-03-03 00:16:12 +08:00
parent d85c18a62a
commit d897580e0e
3 changed files with 66 additions and 45 deletions
+2
View File
@@ -382,6 +382,8 @@ class MySQLDataSource(DataSource):
super().format_data() super().format_data()
logger.success(f"已成功重载 {up.uname} (UID: {up.uid}, 房间号: {up.room_id}) 的推送配置") logger.success(f"已成功重载 {up.uname} (UID: {up.uid}, 房间号: {up.room_id}) 的推送配置")
await up.auto_reload_connect()
async def load_new(self, uid: int): async def load_new(self, uid: int):
""" """
从 MySQL 中追加读取指定 UID 的用户 从 MySQL 中追加读取指定 UID 的用户
+3 -3
View File
@@ -648,7 +648,7 @@ class LiveDanmaku(AsyncEvent):
raise LiveException('尚未连接服务器') raise LiveException('尚未连接服务器')
self.__status = self.STATUS_CLOSING self.__status = self.STATUS_CLOSING
logger.info(f'正在关闭直播间 {self.room_display_id} 的连接') logger.debug(f'正在关闭直播间 {self.room_display_id} 的连接')
# 取消所有任务 # 取消所有任务
while len(self.__tasks) > 0: while len(self.__tasks) > 0:
@@ -657,7 +657,7 @@ class LiveDanmaku(AsyncEvent):
self.__status = self.STATUS_CLOSED self.__status = self.STATUS_CLOSED
await self.__ws.close() await self.__ws.close()
logger.success(f'直播间 {self.room_display_id} 的连接已关闭') logger.debug(f'直播间 {self.room_display_id} 的连接已关闭')
async def __main(self): async def __main(self):
""" """
@@ -737,7 +737,7 @@ class LiveDanmaku(AsyncEvent):
self.__status = self.STATUS_CLOSING self.__status = self.STATUS_CLOSING
elif msg.type == aiohttp.WSMsgType.CLOSED: elif msg.type == aiohttp.WSMsgType.CLOSED:
logger.info(f'直播间 {self.room_display_id} 的连接已关闭') logger.debug(f'直播间 {self.room_display_id} 的连接已关闭')
self.__status = self.STATUS_CLOSED self.__status = self.STATUS_CLOSED
# 正常断开情况下跳出循环 # 正常断开情况下跳出循环
+61 -42
View File
@@ -94,9 +94,6 @@ class Up(BaseModel):
return any([self.__any_live_report_item_enabled(a) for a in attribute]) return any([self.__any_live_report_item_enabled(a) for a in attribute])
return any(map(lambda t: t.live_report.enabled and t.live_report.__getattribute__(attribute), self.targets)) return any(map(lambda t: t.live_report.enabled and t.live_report.__getattribute__(attribute), self.targets))
def __any_dynamic_update_enabled(self):
return any(map(lambda conf: conf.enabled, map(lambda group: group.dynamic_update, self.targets)))
async def connect(self): async def connect(self):
""" """
连接直播间 连接直播间
@@ -343,48 +340,70 @@ class Up(BaseModel):
await redis.incr_room_guard_time(self.room_id, int(time.time()), month) await redis.incr_room_guard_time(self.room_id, int(time.time()), month)
if self.__any_dynamic_update_enabled(): @self.__room.on("DYNAMIC_UPDATE")
@self.__room.on("DYNAMIC_UPDATE") async def dynamic_update(event):
async def dynamic_update(event): """
""" 动态更新事件
动态更新事件 """
""" logger.debug(f"{self.uname} (DYNAMIC_UPDATE): {event}")
logger.debug(f"{self.uname} (DYNAMIC_UPDATE): {event}")
dynamic_id = event["desc"]["dynamic_id"] dynamic_id = event["desc"]["dynamic_id"]
dynamic_type = event["desc"]["type"] dynamic_type = event["desc"]["type"]
bvid = event['desc']['bvid'] if dynamic_type == 8 else "" bvid = event['desc']['bvid'] if dynamic_type == 8 else ""
rid = event['desc']['rid'] if dynamic_type in (64, 256) else "" rid = event['desc']['rid'] if dynamic_type in (64, 256) else ""
action_map = { action_map = {
1: "转发了动态", 1: "转发了动态",
2: "发表了新动态", 2: "发表了新动态",
4: "发表了新动态", 4: "发表了新动态",
8: "投稿了新视频", 8: "投稿了新视频",
64: "投稿了新专栏", 64: "投稿了新专栏",
256: "投稿了新音频", 256: "投稿了新音频",
2048: "发表了新动态" 2048: "发表了新动态"
} }
url_map = { url_map = {
1: f"https://t.bilibili.com/{dynamic_id}", 1: f"https://t.bilibili.com/{dynamic_id}",
2: f"https://t.bilibili.com/{dynamic_id}", 2: f"https://t.bilibili.com/{dynamic_id}",
4: f"https://t.bilibili.com/{dynamic_id}", 4: f"https://t.bilibili.com/{dynamic_id}",
8: f"https://www.bilibili.com/video/{bvid}", 8: f"https://www.bilibili.com/video/{bvid}",
64: f"https://www.bilibili.com/read/cv{rid}", 64: f"https://www.bilibili.com/read/cv{rid}",
256: f"https://www.bilibili.com/audio/au{rid}", 256: f"https://www.bilibili.com/audio/au{rid}",
2048: f"https://t.bilibili.com/{dynamic_id}" 2048: f"https://t.bilibili.com/{dynamic_id}"
} }
base64str = await DynamicPicGenerator.generate(event) base64str = await DynamicPicGenerator.generate(event)
# 推送动态消息 # 推送动态消息
dynamic_update_args = { dynamic_update_args = {
"{uname}": self.uname, "{uname}": self.uname,
"{action}": action_map.get(dynamic_type, "发表了新动态"), "{action}": action_map.get(dynamic_type, "发表了新动态"),
"{url}": url_map.get(dynamic_type, f"https://t.bilibili.com/{dynamic_id}"), "{url}": url_map.get(dynamic_type, f"https://t.bilibili.com/{dynamic_id}"),
"{picture}": "".join(["{base64pic=", base64str, "}"]) "{picture}": "".join(["{base64pic=", base64str, "}"])
} }
await self.__bot.send_dynamic_at(self) await self.__bot.send_dynamic_at(self)
await self.__bot.send_dynamic_update(self, dynamic_update_args) await self.__bot.send_dynamic_update(self, dynamic_update_args)
async def disconnect(self):
"""
断开连接直播间
"""
if self.__room is not None and self.__room.get_status() == 2:
await self.__room.disconnect()
self.__is_reconnect = False
logger.success(f"已断开连接 {self.uname} 的直播间 {self.room_id}")
await self.accumulate_and_reset_data()
async def auto_reload_connect(self):
"""
自动判断仅连接必要的直播间开启时,重载配置时自动处理直播间连接状态
"""
if config.get("ONLY_CONNECT_NECESSARY_ROOM"):
if any([self.__any_live_on_enabled(), self.__any_live_off_enabled(), self.__any_live_report_enabled()]):
if self.__room is None or self.__room.get_status() != 2:
await self.connect()
else:
if self.__room is not None and self.__room.get_status() == 2:
await self.disconnect()
async def __generate_live_report_param(self): async def __generate_live_report_param(self):
""" """