修复一些问题
This commit is contained in:
@@ -686,15 +686,16 @@ class AiUtils(object):
|
||||
def save_aclist_flat_append(cls, acList, filename="log/acList.json"):
|
||||
"""
|
||||
将 anchor 对象数组平铺追加到 JSON 文件(数组)中。
|
||||
期望 acList 形如:
|
||||
[
|
||||
{"anchorId": "ldn327_", "country": ""},
|
||||
{"anchorId": "tianliang30", "country": ""}
|
||||
]
|
||||
文件固定写到 项目根目录/log/acList.json
|
||||
"""
|
||||
|
||||
# 找到当前文件所在目录,回退到项目根目录
|
||||
root_dir = Path(__file__).resolve().parent.parent # 根据实际层级调整
|
||||
log_dir = root_dir
|
||||
log_dir.mkdir(parents=True, exist_ok=True) # 确保 log 目录存在
|
||||
|
||||
file_path = log_dir / filename
|
||||
|
||||
file_path = Path(filename)
|
||||
data = cls._read_json_list(file_path)
|
||||
|
||||
# 规范化输入,确保都是 {anchorId, country}
|
||||
@@ -705,30 +706,12 @@ class AiUtils(object):
|
||||
|
||||
data.extend(to_add)
|
||||
cls._write_json_list(file_path, data)
|
||||
LogManager.method_info(f"写入的路径是:{file_path}", "写入数据")
|
||||
LogManager.info(f"[acList] 已追加 {len(to_add)} 条,当前总数={len(data)} -> {file_path}")
|
||||
|
||||
# -------- 弹出(取一个删一个) --------
|
||||
# @classmethod
|
||||
# def pop_aclist_first(cls, filename="log/acList.json"):
|
||||
# """
|
||||
# 从 JSON 数组中取出第一个 anchor 对象,并删除它;为空或文件不存在返回 None。
|
||||
# 返回形如:{"anchorId": "...", "country": "..."}
|
||||
# """
|
||||
# file_path = Path(filename)
|
||||
# data = cls._read_json_list(file_path)
|
||||
# if not data:
|
||||
# return None
|
||||
#
|
||||
# first = data.pop(0)
|
||||
# # 兜底保证结构
|
||||
# norm = cls._normalize_anchor_items(first)
|
||||
# first = norm[0] if norm else None
|
||||
#
|
||||
# cls._write_json_list(file_path, data)
|
||||
# return first
|
||||
|
||||
@classmethod
|
||||
def pop_aclist_first(cls, filename="Module/log/acList.json", mode="pop"):
|
||||
def pop_aclist_first(cls, filename="log/acList.json", mode="pop"):
|
||||
"""
|
||||
从 JSON 数组/对象(anchorList)中取出第一个 anchor 对象。
|
||||
- mode="pop" : 取出并删除
|
||||
@@ -867,12 +850,16 @@ class AiUtils(object):
|
||||
@classmethod
|
||||
def delete_anchors_by_ids(cls, ids: list[str], filename="log/acList.json") -> int:
|
||||
"""
|
||||
根据 anchorId 列表从 JSON 文件中删除匹配的 anchor。
|
||||
根据 anchorId 列表从根目录/log/acList.json 中删除匹配的 anchor。
|
||||
返回删除数量。
|
||||
"""
|
||||
file_path = Path(filename)
|
||||
# 确保路径固定在根目录下的 log 文件夹
|
||||
root_dir = Path(__file__).resolve().parent.parent
|
||||
file_path = root_dir / "log" / filename
|
||||
|
||||
if not file_path.exists():
|
||||
return 0
|
||||
|
||||
try:
|
||||
with open(file_path, "r", encoding="utf-8") as f:
|
||||
data = json.load(f)
|
||||
@@ -881,15 +868,19 @@ class AiUtils(object):
|
||||
except Exception as e:
|
||||
LogManager.error(f"[delete_anchors_by_ids] 读取失败: {e}")
|
||||
return 0
|
||||
|
||||
before = len(data)
|
||||
# 保留不在 ids 里的对象
|
||||
data = [d for d in data if isinstance(d, dict) and d.get("anchorId") not in ids]
|
||||
deleted = before - len(data)
|
||||
|
||||
try:
|
||||
file_path.parent.mkdir(parents=True, exist_ok=True)
|
||||
with open(file_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
except Exception as e:
|
||||
LogManager.error(f"[delete_anchors_by_ids] 写入失败: {e}")
|
||||
|
||||
return deleted
|
||||
|
||||
# -------- 查看第一个(取出但不删除) --------
|
||||
@@ -903,7 +894,7 @@ class AiUtils(object):
|
||||
return (base / p).resolve()
|
||||
|
||||
@classmethod
|
||||
def peek_aclist_first(cls, filename="Module/log/acList.json"):
|
||||
def peek_aclist_first(cls, filename="log/acList.json"):
|
||||
file_path = cls._resolve_path(filename)
|
||||
if not file_path.exists():
|
||||
print(f"[peek] 文件不存在: {file_path}")
|
||||
|
||||
Reference in New Issue
Block a user