From 412624045e6141faddea150583427a05809bc358 Mon Sep 17 00:00:00 2001 From: zhangkai <2403741920@qq.com> Date: Mon, 24 Nov 2025 20:22:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BF=BB=E8=AF=91=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E7=9A=84=E9=87=8D=E8=AF=95=E6=AC=A1=E6=95=B0=EF=BC=8C?= =?UTF-8?q?=E9=98=B2=E6=AD=A2=E8=AF=BB=E5=8F=96=E4=BF=A1=E6=81=AF=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Utils/JsonUtils.py | 24 ++++++++++++++++++------ script/ScriptManager.py | 6 +++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Utils/JsonUtils.py b/Utils/JsonUtils.py index cd5ec13..b1917a6 100644 --- a/Utils/JsonUtils.py +++ b/Utils/JsonUtils.py @@ -203,18 +203,30 @@ class JsonUtils: @classmethod - def query_all_json_items(cls, filename="log/last_message.json") -> list: + def query_all_json_items(cls, filename="log/last_message.json"): """ - 查询 JSON 文件(数组)中的所有项,并剔除 sender 为空的记录 - :param filename: JSON 文件路径 - :return: list,可能为空 + 读取 JSON 数组文件,过滤掉 sender 或 text 为空的记录 + :param filename: 文件路径 + :return: 有效记录列表,可能为空 """ file_path = Path(filename) data = cls._read_json_list(file_path) if not isinstance(data, list): return [] - # 过滤 sender 和 text 为空字符串的项 - return [item for item in data if isinstance(item, dict) and item.get("sender", "").strip() and item.get("text", "").strip()] + + def _is_valid(d): + if not isinstance(d, dict): + return False + sender = d.get("sender") or "" + text = d.get("text") or "" + return ( + isinstance(sender, str) + and isinstance(text, str) + and sender.strip() != "" + and text.strip() != "" + ) + + return [item for item in data if _is_valid(item)] diff --git a/script/ScriptManager.py b/script/ScriptManager.py index b7216ac..7078a70 100644 --- a/script/ScriptManager.py +++ b/script/ScriptManager.py @@ -1740,8 +1740,12 @@ class ScriptManager(): LogManager.method_info(f"获取主播的名称:{anchor_name}", "检测消息", udid) LogManager.method_info(f"获取主播最后发送的消息 即将翻译:{last_in}", "检测消息", udid) + chinese_last_msg_text = "" if last_in is not None: - chinese_last_msg_text = Requester.translationToChinese(last_in) + for attempt in range(3): + chinese_last_msg_text = Requester.translationToChinese(last_in) + if chinese_last_msg_text: # 非空则跳出循环 + break else: chinese_last_msg_text = ""