feat: 优化映射表加载与导出功能,确保数据一致性与可视化更新
This commit is contained in:
1 parent
2358682216
commit
504410f7b9
9 files changed
+163
-150
No files matched your search
+15
-23
@@ -295,31 +295,17 @@ class fudai_analyse:
|
||||
# 运行时直播间标题和位置映射表(用于回退校验)
|
||||
# 格式:{room_pos: (room_title, room_title_raw)}
|
||||
self._room_pos_to_title_map: dict[int, tuple[str, str]] = {}
|
||||
|
||||
# 启动时尝试从持久化文件加载已有映射,便于 GUI 与重启后的可见性
|
||||
# 每次重新运行时丢弃磁盘上旧的映射表,确保只记录本次运行的结果。
|
||||
# 同时尝试删除旧的导出文件(JSON/CSV/MD),以避免 GUI 在未写入新数据前显示过期内容。
|
||||
try:
|
||||
state_path = os.path.join(os.path.dirname(__file__), "state", "room_pos_map.json")
|
||||
if os.path.exists(state_path):
|
||||
state_dir = os.path.join(os.path.dirname(__file__), "state")
|
||||
state_path = os.path.join(state_dir, "room_pos_map.json")
|
||||
csv_path = os.path.join(state_dir, "room_pos_map.csv")
|
||||
md_path = os.path.join(state_dir, "room_pos_map.md")
|
||||
for p in (state_path, csv_path, md_path):
|
||||
try:
|
||||
with open(state_path, "r", encoding="utf-8") as f:
|
||||
import json as _json
|
||||
|
||||
parsed = _json.load(f)
|
||||
# parsed 格式为 {"<pos>": [title, title_raw], ...}
|
||||
tmp_map: dict[int, tuple[str, str]] = {}
|
||||
for k, v in parsed.items():
|
||||
try:
|
||||
ik = int(k)
|
||||
except Exception:
|
||||
continue
|
||||
try:
|
||||
title = v[0] if isinstance(v, (list, tuple)) and len(v) > 0 else ""
|
||||
title_raw = v[1] if isinstance(v, (list, tuple)) and len(v) > 1 else ""
|
||||
tmp_map[ik] = (title, title_raw)
|
||||
except Exception:
|
||||
continue
|
||||
if tmp_map:
|
||||
self._room_pos_to_title_map = tmp_map
|
||||
if os.path.exists(p):
|
||||
os.remove(p)
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
@@ -1167,6 +1153,12 @@ class fudai_analyse:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 尝试记录当前直播间的标题与位置,确保所有直播间(不限福袋)都被采集到运行时映射表
|
||||
try:
|
||||
self._record_room_title_and_pos()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 直接参与标志已移除,相关旧逻辑已清理,继续按正常流程执行
|
||||
# 0. 如果正在等待开奖:不在此期间检查并抢占其他预约,避免放弃当前正在参加的直播间
|
||||
if getattr(self, "_waiting_for_lottery_result", False):
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
pos,title,title_raw
|
||||
0,哙小,哙~小
|
||||
|
@@ -1,74 +1,11 @@
|
||||
{
|
||||
"1": [
|
||||
"2",
|
||||
"2&"
|
||||
],
|
||||
"2": [
|
||||
"终火米",
|
||||
"终*火米]"
|
||||
],
|
||||
"3": [
|
||||
"隋心",
|
||||
"隋心"
|
||||
],
|
||||
"4": [
|
||||
"s",
|
||||
"/s"
|
||||
],
|
||||
"5": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"6": [
|
||||
"7L只",
|
||||
"7]L只"
|
||||
],
|
||||
"7": [
|
||||
"S",
|
||||
"S^^^"
|
||||
],
|
||||
"8": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"9": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"10": [
|
||||
"3",
|
||||
"{3/"
|
||||
],
|
||||
"0": [
|
||||
"AR黄龄",
|
||||
"AR黄龄"
|
||||
"哙小",
|
||||
"哙~小"
|
||||
],
|
||||
"11": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"12": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"13": [
|
||||
"gITc",
|
||||
"gITc"
|
||||
],
|
||||
"14": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"15": [
|
||||
"",
|
||||
""
|
||||
],
|
||||
"16": [
|
||||
"交个朋友",
|
||||
"交个朋友"
|
||||
],
|
||||
"17": [
|
||||
"演员梁燹琪",
|
||||
"演员梁燹琪"
|
||||
]
|
||||
"_meta": {
|
||||
"source": "C:\\Users\\12742\\Desktop\\douyin_guaji-main\\douyin\\core\\state\\room_pos_map.json",
|
||||
"generated_ts": 1768465088,
|
||||
"run_id": "20260115161806"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<!-- source: C:\Users\12742\Desktop\douyin_guaji-main\douyin\core\state\room_pos_map.json -->
|
||||
|
||||
| 位置 | 标题 | 原始标题 |
|
||||
|-:|:-|:-|
|
||||
| 0 | 哙小 | 哙~小 |
|
||||
@@ -761,7 +761,8 @@ def attend_choujiang(analyser) -> bool:
|
||||
except Exception:
|
||||
pass
|
||||
try:
|
||||
st, seen = analyser._diagnose_stage_from_current_screen()
|
||||
# 已在打开详情时完成阶段诊断,后续不再重复调用
|
||||
st, seen = "", ""
|
||||
except Exception:
|
||||
st, seen = "", ""
|
||||
|
||||
@@ -1128,20 +1129,18 @@ def attend_choujiang(analyser) -> bool:
|
||||
)
|
||||
|
||||
# 重新诊断阶段,确认是否还在粉丝团加入页
|
||||
try:
|
||||
analyser.operation.get_screenshot(
|
||||
analyser.device_id, use_service=False
|
||||
)
|
||||
st_after, seen_after = (
|
||||
analyser._diagnose_stage_from_current_screen()
|
||||
)
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"点击加入粉丝团后重新诊断阶段",
|
||||
stage_before=st,
|
||||
stage_after=st_after,
|
||||
seen_after=seen_after,
|
||||
)
|
||||
try:
|
||||
# 删除:避免重复阶段诊断,减少截图/adb开销
|
||||
st_after, seen_after = "", ""
|
||||
analyser._log_kv(
|
||||
"参与",
|
||||
"已跳过点击加入粉丝团后的重复阶段诊断",
|
||||
stage_before=st,
|
||||
stage_after=st_after,
|
||||
seen_after=seen_after,
|
||||
)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if st_after == "FANSCLUB_JOIN":
|
||||
# 仍在粉丝团加入页,说明可能需要再次点击或等待更长时间
|
||||
|
||||
@@ -362,6 +362,10 @@ class SwipeHandler:
|
||||
try:
|
||||
analyser._swipe_times = 0
|
||||
analyser.room_pos = 0
|
||||
try:
|
||||
analyser._record_room_title_and_pos() # type: ignore[attr-defined]
|
||||
except Exception:
|
||||
pass
|
||||
# 清除连续未找到福袋计数,避免与其他回退逻辑重复
|
||||
try:
|
||||
setattr(analyser, "_consecutive_no_fudai_count", 0)
|
||||
|
||||
@@ -15,6 +15,7 @@ import time
|
||||
import difflib
|
||||
from typing import Any, Optional, Dict
|
||||
import json
|
||||
import csv
|
||||
import os
|
||||
import path_config
|
||||
|
||||
@@ -904,7 +905,7 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
|
||||
except Exception:
|
||||
data["_meta"] = {"generated_ts": int(time.time())}
|
||||
|
||||
# 使用漂亮的缩进写入文件(先写 tmp 再替换)
|
||||
# 使用漂亮的缩进写入 JSON(先写 tmp 再替换)
|
||||
with open(tmp_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
try:
|
||||
@@ -912,6 +913,46 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
|
||||
except Exception:
|
||||
# os.replace 在某些环境可能不可用,回退到重命名
|
||||
os.rename(tmp_path, final_path)
|
||||
|
||||
# 同步导出 CSV 和 Markdown 表格(仅包含映射条目,不包含 _meta)
|
||||
try:
|
||||
csv_path = os.path.join(state_dir, "room_pos_map.csv")
|
||||
csv_tmp = csv_path + ".tmp"
|
||||
md_path = os.path.join(state_dir, "room_pos_map.md")
|
||||
md_tmp = md_path + ".tmp"
|
||||
|
||||
# 写 CSV
|
||||
with open(csv_tmp, "w", encoding="utf-8", newline="") as cf:
|
||||
writer = csv.writer(cf)
|
||||
writer.writerow(["pos", "title", "title_raw"])
|
||||
for k in sorted(filtered.keys(), key=lambda x: int(x) if x.isdigit() else x):
|
||||
v = filtered.get(k, ["", ""]) or ["", ""]
|
||||
writer.writerow([k, v[0], v[1]])
|
||||
try:
|
||||
os.replace(csv_tmp, csv_path)
|
||||
except Exception:
|
||||
os.rename(csv_tmp, csv_path)
|
||||
|
||||
# 写 Markdown(顶部注释 source)
|
||||
with open(md_tmp, "w", encoding="utf-8") as mf:
|
||||
mf.write(f"<!-- source: {final_path} -->\n\n")
|
||||
mf.write("| 位置 | 标题 | 原始标题 |\n")
|
||||
mf.write("|-:|:-|:-|\n")
|
||||
for k in sorted(filtered.keys(), key=lambda x: int(x) if x.isdigit() else x):
|
||||
v = filtered.get(k, ["", ""]) or ["", ""]
|
||||
title = v[0].replace("|", "\\|")
|
||||
title_raw = v[1].replace("|", "\\|")
|
||||
mf.write(f"| {k} | {title} | {title_raw} |\n")
|
||||
try:
|
||||
os.replace(md_tmp, md_path)
|
||||
except Exception:
|
||||
os.rename(md_tmp, md_path)
|
||||
except Exception:
|
||||
# 导出 CSV/MD 失败不影响 JSON 写入
|
||||
try:
|
||||
analyser._log_kv("预约", "[预约] 导出 CSV/MD 失败")
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
try:
|
||||
analyser._log_kv("预约", "[预约] 序列化映射表到文件失败")
|
||||
|
||||
+66
-37
@@ -383,13 +383,15 @@ def main():
|
||||
mid.add(left_wrap, weight=0) # 左侧配置区域
|
||||
mid.add(right, weight=1) # 右侧日志/预览区域
|
||||
|
||||
# 右侧 notebook(用于日志/预览/映射表)
|
||||
right_notebook = ttk.Notebook(right)
|
||||
right_notebook.pack(fill=BOTH, expand=True)
|
||||
# 右侧为日志/预览区域(保持原样)
|
||||
# right_notebook previously added here; mapping tab will attach to the left notebook
|
||||
|
||||
# 映射表标签页(右侧)
|
||||
mapping_tab = ttk.Frame(right_notebook)
|
||||
right_notebook.add(mapping_tab, text="映射表")
|
||||
# 左侧使用Notebook标签页组织配置
|
||||
left_notebook = ttk.Notebook(left_wrap)
|
||||
left_notebook.pack(fill=BOTH, expand=True)
|
||||
# 在原有左侧 Notebook 中添加映射表页(替换原先的单行显示)
|
||||
mapping_tab = ttk.Frame(left_notebook)
|
||||
left_notebook.add(mapping_tab, text="映射表")
|
||||
|
||||
# 映射信息顶部:来源路径和操作按钮
|
||||
mapping_top = ttk.Frame(mapping_tab)
|
||||
@@ -418,7 +420,7 @@ def main():
|
||||
mapping_tree.column("title_raw", width=300, anchor="w")
|
||||
mapping_tree.pack(fill=BOTH, expand=True, padx=8, pady=(4, 8))
|
||||
|
||||
# 加载/刷新函数
|
||||
# 加载/刷新函数(优先使用项目路径)
|
||||
def _find_room_map_path() -> str | None:
|
||||
try:
|
||||
import path_config as _pc
|
||||
@@ -428,9 +430,7 @@ def main():
|
||||
os.path.join(os.getcwd(), "douyin", "core", "state", "room_pos_map.json"),
|
||||
]
|
||||
except Exception:
|
||||
candidates = [
|
||||
os.path.join(os.getcwd(), "douyin", "core", "state", "room_pos_map.json"),
|
||||
]
|
||||
candidates = [os.path.join(os.getcwd(), "douyin", "core", "state", "room_pos_map.json")]
|
||||
for p in candidates:
|
||||
try:
|
||||
if os.path.exists(p):
|
||||
@@ -453,10 +453,8 @@ def main():
|
||||
mapping_tree.delete(*mapping_tree.get_children())
|
||||
return
|
||||
|
||||
# 删除元信息字段
|
||||
meta = None
|
||||
if isinstance(data, dict) and "_meta" in data:
|
||||
meta = data.pop("_meta", None)
|
||||
data.pop("_meta", None)
|
||||
|
||||
mapping_tree.delete(*mapping_tree.get_children())
|
||||
try:
|
||||
@@ -473,7 +471,6 @@ def main():
|
||||
title, title_raw = "-", "-"
|
||||
mapping_tree.insert("", "end", values=(k, title, title_raw))
|
||||
|
||||
# 绑定刷新按钮与自动刷新
|
||||
mapping_refresh_btn.config(command=_load_mapping_to_tree)
|
||||
|
||||
def _mapping_auto_refresh_loop():
|
||||
@@ -491,10 +488,6 @@ def main():
|
||||
_mapping_auto_refresh_loop()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 左侧使用Notebook标签页组织配置
|
||||
left_notebook = ttk.Notebook(left_wrap)
|
||||
left_notebook.pack(fill=BOTH, expand=True)
|
||||
needswitch_var = tk.BooleanVar(value=bool(cfg.get("needswitch", True)))
|
||||
infinite_loop_var = tk.BooleanVar(
|
||||
value=bool(cfg.get("infinite_loop_enabled", False))
|
||||
@@ -2796,13 +2789,29 @@ def main():
|
||||
).pack(side=LEFT)
|
||||
ttk.Button(map_ctrl, text="刷新映射表", width=12).pack(side=LEFT, padx=(8, 6))
|
||||
|
||||
# 映射内容显示区
|
||||
map_view = ScrolledText(mapping_tab, wrap="none", height=12)
|
||||
map_view.pack(fill=BOTH, expand=True, padx=10, pady=(6, 10))
|
||||
# 映射内容显示区(表格形式)
|
||||
mapping_source_var = tk.StringVar(value="未加载")
|
||||
ttk.Label(map_ctrl, textvariable=mapping_source_var).pack(side=LEFT, padx=(8, 0))
|
||||
|
||||
# 加载映射表的函数(尝试多个可能的路径)
|
||||
mapping_tree = ttk.Treeview(
|
||||
mapping_tab, columns=("pos", "title", "title_raw"), show="headings", height=12
|
||||
)
|
||||
mapping_tree.heading("pos", text="位置")
|
||||
mapping_tree.heading("title", text="标题")
|
||||
mapping_tree.heading("title_raw", text="原始标题")
|
||||
mapping_tree.column("pos", width=80, anchor="e")
|
||||
mapping_tree.column("title", width=340, anchor="w")
|
||||
mapping_tree.column("title_raw", width=340, anchor="w")
|
||||
mapping_tree.pack(fill=BOTH, expand=True, padx=10, pady=(6, 10))
|
||||
|
||||
# 加载映射表到表格(尝试多个可能的路径)
|
||||
def _load_mapping_to_view():
|
||||
map_view.delete("1.0", "end")
|
||||
# 清空表格
|
||||
try:
|
||||
mapping_tree.delete(*mapping_tree.get_children())
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 优先使用基于项目目录的固定路径,避免不同工作目录导致无法找到状态文件
|
||||
try:
|
||||
base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), ".."))
|
||||
@@ -2814,30 +2823,50 @@ def main():
|
||||
os.path.join(base_dir, "douyin", "state", "room_pos_map.json"),
|
||||
os.path.join(os.getcwd(), "douyin", "core", "state", "room_pos_map.json"),
|
||||
]
|
||||
|
||||
found = False
|
||||
for p in possible_paths:
|
||||
try:
|
||||
if os.path.exists(p):
|
||||
with open(p, "r", encoding="utf-8") as f:
|
||||
data = f.read()
|
||||
# 尝试格式化 JSON,否则原样显示
|
||||
raw = f.read()
|
||||
try:
|
||||
parsed = json.loads(data)
|
||||
pretty = json.dumps(parsed, ensure_ascii=False, indent=2)
|
||||
map_view.insert("end", f"# 从: {p}\n")
|
||||
map_view.insert("end", pretty)
|
||||
parsed = json.loads(raw)
|
||||
except Exception:
|
||||
map_view.insert("end", f"# 从: {p}(非JSON,原始内容)\n")
|
||||
map_view.insert("end", data)
|
||||
parsed = None
|
||||
|
||||
mapping_source_var.set(p)
|
||||
|
||||
if not parsed:
|
||||
# 非JSON或解析错误:在表格中显示原始文本的一行提示
|
||||
mapping_tree.insert("", "end", values=("-", "(非JSON,查看原始文件)", ""))
|
||||
else:
|
||||
# 移除元信息
|
||||
if isinstance(parsed, dict) and "_meta" in parsed:
|
||||
parsed.pop("_meta", None)
|
||||
# 按数字顺序插入
|
||||
try:
|
||||
keys = sorted(parsed.keys(), key=lambda x: int(x) if str(x).isdigit() else x)
|
||||
except Exception:
|
||||
keys = list(parsed.keys())
|
||||
for k in keys:
|
||||
try:
|
||||
v = parsed.get(k, ["", ""]) or ["", ""]
|
||||
title = (v[0] or "").strip() or "-"
|
||||
title_raw = (v[1] or "").strip() or "-"
|
||||
except Exception:
|
||||
title, title_raw = "-", "-"
|
||||
mapping_tree.insert("", "end", values=(k, title, title_raw))
|
||||
|
||||
found = True
|
||||
break
|
||||
except Exception as e:
|
||||
map_view.insert("end", f"读取 {p} 失败: {e}\n")
|
||||
except Exception:
|
||||
# 忽略单个路径读取错误,尝试下一个候选
|
||||
pass
|
||||
|
||||
if not found:
|
||||
map_view.insert(
|
||||
"end",
|
||||
"未找到映射表文件。可在运行端将 '_room_pos_to_title_map' 序列化到 'douyin/core/state/room_pos_map.json',然后点击刷新以查看内容。",
|
||||
)
|
||||
mapping_source_var.set("未找到 room_pos_map.json")
|
||||
mapping_tree.insert("", "end", values=("-", "未找到映射表文件", ""))
|
||||
|
||||
# 绑定刷新按钮与自动刷新
|
||||
btns = map_ctrl.winfo_children()
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
from douyin.reservation import douyin_reservation_handler as rh
|
||||
class A: pass
|
||||
|
||||
|
||||
class A:
|
||||
pass
|
||||
|
||||
|
||||
a = A()
|
||||
a._room_pos_to_title_map = {1: ('测试直播间', '测试直播间_raw')}
|
||||
a._room_pos_to_title_map = {1: ("测试直播间", "测试直播间_raw")}
|
||||
try:
|
||||
rh._dump_room_pos_map_to_state(a)
|
||||
print('DUMP_OK')
|
||||
print("DUMP_OK")
|
||||
except Exception as e:
|
||||
print('DUMP_ERR', e)
|
||||
print("DUMP_ERR", e)
|
||||
Reference in new issue
Block a user