合并代码

This commit is contained in:
2025-11-24 20:39:05 +08:00
2 changed files with 23 additions and 7 deletions

View File

@@ -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)]

View File

@@ -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 = ""