20250918-新增主播库功能
This commit is contained in:
@@ -708,23 +708,69 @@ class AiUtils(object):
|
||||
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="log/acList.json"):
|
||||
def pop_aclist_first(cls, filename="Module/log/acList.json", mode="pop"):
|
||||
"""
|
||||
从 JSON 数组中取出第一个 anchor 对象,并删除它;为空或文件不存在返回 None。
|
||||
返回形如:{"anchorId": "...", "country": "..."}
|
||||
从 JSON 数组/对象(anchorList)中取出第一个 anchor 对象。
|
||||
- mode="pop" : 取出并删除
|
||||
- mode="move" : 取出并放到列表尾部
|
||||
返回形如:{"anchorId": "...", "country": "...", ...}
|
||||
"""
|
||||
file_path = Path(filename)
|
||||
data = cls._read_json_list(file_path)
|
||||
file_path = cls._resolve_path(filename)
|
||||
if not file_path.exists():
|
||||
return None
|
||||
|
||||
try:
|
||||
raw = json.loads(file_path.read_text(encoding="utf-8-sig"))
|
||||
except Exception as e:
|
||||
LogManager.error(f"[pop_aclist_first] 读取失败: {e}")
|
||||
return None
|
||||
|
||||
# 支持两种格式:list 或 dict{anchorList:[...]}
|
||||
if isinstance(raw, list):
|
||||
data, wrapper = raw, None
|
||||
elif isinstance(raw, dict) and isinstance(raw.get("anchorList"), list):
|
||||
data, wrapper = raw["anchorList"], raw
|
||||
else:
|
||||
return None
|
||||
|
||||
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)
|
||||
if first and mode == "move":
|
||||
# 放到尾部
|
||||
data.append(first)
|
||||
|
||||
# 写回
|
||||
to_write = wrapper if wrapper is not None else data
|
||||
file_path.write_text(
|
||||
json.dumps(to_write, ensure_ascii=False, indent=2),
|
||||
encoding="utf-8"
|
||||
)
|
||||
return first
|
||||
|
||||
@classmethod
|
||||
|
||||
Reference in New Issue
Block a user