合并代码

This commit is contained in:
2025-10-29 16:58:02 +08:00
5 changed files with 59 additions and 37 deletions

View File

@@ -1096,10 +1096,10 @@ class AiUtils(object):
# -------- 追加(对象数组平铺追加) --------
@classmethod
def save_aclist_flat_append(cls, acList, filename="log/acList.json"):
def save_aclist_flat_append(cls, acList, filename="data/acList.json"):
"""
将 anchor 对象数组平铺追加到 JSON 文件(数组)中。
文件固定写到 项目根目录/log/acList.json
文件固定写到 项目根目录/data/acList.json
"""
# 找到当前文件所在目录,回退到项目根目录
@@ -1123,7 +1123,7 @@ class AiUtils(object):
LogManager.info(f"[acList] 已追加 {len(to_add)} 条,当前总数={len(data)} -> {file_path}")
@classmethod
def pop_aclist_first(cls, filename="log/acList.json", mode="pop"):
def pop_aclist_first(cls, filename="data/acList.json", mode="pop"):
"""
从 JSON 数组/对象(anchorList)中取出第一个 anchor 对象。
- mode="pop" : 取出并删除
@@ -1131,6 +1131,8 @@ class AiUtils(object):
返回形如:{"anchorId": "...", "country": "...", ...}
"""
file_path = cls._resolve_path(filename)
if not file_path.exists():
return None
@@ -1169,7 +1171,7 @@ class AiUtils(object):
return first
@classmethod
def bulk_update_anchors(cls, updates, filename="log/acList.json", case_insensitive=False):
def bulk_update_anchors(cls, updates, filename="data/acList.json", case_insensitive=False):
"""
批量修改(文件根必须是数组,沿用 _read_json_list 的约定)
- updates:
@@ -1257,7 +1259,7 @@ class AiUtils(object):
@classmethod
def delete_anchors_by_ids(cls, ids: list[str], filename: str = "acList.json") -> int:
"""
根据 anchorId 列表,从项目根目录/log/acList.json 中删除匹配的 anchor。
根据 anchorId 列表,从项目根目录/data/acList.json 中删除匹配的 anchor。
- ids: 要删除的 anchorId 列表
- filename: 默认为 acList.json可以传文件名或绝对路径
返回:删除数量
@@ -1267,7 +1269,7 @@ class AiUtils(object):
if Path(filename).is_absolute():
file_path = Path(filename)
else:
file_path = root_dir / "log" / filename
file_path = root_dir / "data" / filename
if not file_path.exists():
return 0
@@ -1309,7 +1311,7 @@ class AiUtils(object):
return (base / p).resolve()
@classmethod
def peek_aclist_first(cls, filename="log/acList.json"):
def peek_aclist_first(cls, filename="data/acList.json"):
file_path = cls._resolve_path(filename)
if not file_path.exists():
print(f"[peek] 文件不存在: {file_path}")

View File

@@ -200,10 +200,13 @@ class JsonUtils:
return updated
@classmethod
def query_all_json_items(cls, filename="log/last_message.json") -> list:
"""
查询 JSON 文件(数组)中的所有项,并剔除 sender 和 text 为空的记录
查询 JSON 文件(数组)中的所有项,并剔除 sender 为空的记录
:param filename: JSON 文件路径
:return: list可能为空
"""
@@ -212,8 +215,8 @@ class JsonUtils:
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()]
return [item for item in data if isinstance(item, dict) and item.get("sender", "").strip() and item.get("text", "").strip()]
@classmethod