feat: 优化福袋详情记录逻辑,提升代码可读性

This commit is contained in:
Your Name committed 2026-01-16 03:44:53 +08:00
1 parent df65adfe4e
commit 3723c39a58
5 files changed
+312 -92

No files matched your search

+2 -2
View File
@@ -12,7 +12,7 @@
"auto_join_fansclub": true,
"fansclub_fail_action": "switch",
"high_value_fudai_enabled": true,
"high_value_fudai_threshold_yuan": 1000.0,
"high_value_fudai_threshold_yuan": 500.0,
"high_value_fudai_join_ahead_seconds": 100,
"high_value_fudai_max_swipe_back": 50,
"high_value_fudai_max_reservations": 9999,
@@ -192,7 +192,7 @@
"monitor_critical_mb": 60.0
},
"debug_captures": {
"enabled": true,
"enabled": false,
"keep_days": 3,
"max_files": 2000,
"auto_cleanup": true,
+25
View File
@@ -3115,6 +3115,31 @@ class fudai_analyse:
try:
self._last_fudai_detail_tuple = detail
self._fudai_detail_first_read = True
# 成功识别福袋详情,立即记录到映射表
try:
from douyin.reservation import douyin_reservation_handler
douyin_reservation_handler.ReservationHandler.record_room_title_and_pos(
self
)
self._log_kv(
"流程",
"_handle_in_fudai_detail:福袋详情识别成功,已记录到映射表",
fudai_content=(detail[0] if len(detail) > 0 else "")[:50],
prize_value_yuan=float(
detail[4] if len(detail) > 4 else 0.0
),
)
except Exception as e:
try:
self._log_kv(
"流程",
"_handle_in_fudai_detail:记录映射表失败",
error=str(e),
)
except Exception:
pass
except Exception:
pass
else:
+120 -68
View File
@@ -4528,6 +4528,85 @@ def basic_join_decision(
and future_ts > 0.0
)
# 添加调试日志
try:
analyser._log_kv(
"参与",
"basic_join_decision:高价值检查结果",
prize_value_yuan=prize_value_yuan,
high_value_threshold=high_value_threshold,
high_value_enabled=high_value_enabled,
future_ts=future_ts,
is_high_value=is_high_value,
content=content[:50] if content else "",
)
except Exception:
pass
# 如果是高价值福袋,在进行任何可能切换房间的操作之前,先读取当前直播间标题
# 这样可以确保记录的是原始的高价值直播间标题,而不是切换后的标题
current_room_title = ""
current_room_title_raw = ""
if is_high_value:
try:
revisit_enabled = bool(
getattr(analyser, "high_value_room_revisit_enabled", False)
)
if revisit_enabled:
# 每次记录预约时,都重新读取直播间标题,不使用缓存
# 这样可以确保每个预约记录的是当时的真实直播间标题
try:
# 在函数开头导入,避免作用域问题
from douyin.fudai import (
douyin_fudai_detail as dfd_module,
)
y_pianyi_val = int(getattr(analyser, "y_pianyi", 0) or 0)
current_room_title, current_room_title_raw = (
dfd_module.read_live_room_title_core(analyser, y_pianyi_val)
)
# 更新 analyser 的属性,用于状态文件显示当前直播间
try:
analyser.current_room_title = current_room_title
analyser.current_room_title_raw = current_room_title_raw
except Exception:
pass
except Exception as e_inner:
try:
analyser._log_kv(
"预约",
"读取直播间标题失败,尝试使用缓存",
error=str(e_inner),
error_type=type(e_inner).__name__,
)
except Exception:
pass
# 如果读取失败,尝试使用缓存(作为兜底)
current_room_title = str(
getattr(analyser, "current_room_title", "") or ""
)
current_room_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
)
else:
# 高价值房间回访未开启时,不读取标题以避免不必要的截图
current_room_title = str(
getattr(analyser, "current_room_title", "") or ""
)
current_room_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
)
except Exception as e:
try:
analyser._log_kv(
"预约",
"读取当前直播间标题失败",
error=str(e),
error_type=type(e).__name__,
)
except Exception:
pass
# 执行筛选检查
if skip_result is not None:
skip, reason = skip_result
@@ -4858,6 +4937,18 @@ def basic_join_decision(
# 注意:content, prize_value_yuan, countdown_tuple, future_ts, high_value_enabled, high_value_threshold, is_high_value 已在前面提取
# 仅当启用高价值预约、识别到价值且未来时间合理时,才考虑记入预约队列
if is_high_value:
# 添加调试日志:进入高价值处理逻辑
try:
analyser._log_kv(
"预约",
"basic_join_decision:进入高价值处理逻辑",
prize_value_yuan=prize_value_yuan,
future_ts=future_ts,
content=content[:50] if content else "",
)
except Exception:
pass
# 价值达到高价值阈值:检查剩余秒数,如果在"预约参与阈值"内则立即参与,否则记录预约
# 计算剩余秒数
remain_seconds = None
@@ -4871,6 +4962,18 @@ def basic_join_decision(
except Exception:
pass
# 添加调试日志:剩余秒数计算结果
try:
analyser._log_kv(
"预约",
"basic_join_decision:剩余秒数计算结果",
countdown_tuple=countdown_tuple,
remain_seconds=remain_seconds,
now=int(time.time()),
)
except Exception:
pass
# 获取"预约参与阈值"配置(high_value_fudai_join_ahead_seconds)
# 如果剩余秒数 <= 这个阈值,立即参与;否则记录预约
try:
@@ -4900,6 +5003,18 @@ def basic_join_decision(
else:
# 剩余秒数大于"预约参与阈值",记录预约
try:
analyser._log_kv(
"预约",
"basic_join_decision:价值达到高价值阈值,剩余秒数大于预约参与阈值,准备记录预约",
value_yuan=prize_value_yuan,
threshold=high_value_threshold,
remain_seconds=(
int(remain_seconds) if remain_seconds is not None else None
),
join_ahead_threshold=join_ahead_seconds,
)
except Exception:
pass
analyser._log_kv(
"参与",
"basic_join_decision:价值达到高价值阈值,但剩余秒数大于预约参与阈值,记录预约",
@@ -5142,64 +5257,9 @@ def basic_join_decision(
except Exception:
room_pos = 0
# 获取当前直播间标题(如果可用)
room_title = ""
room_title_raw = ""
try:
revisit_enabled = bool(
getattr(analyser, "high_value_room_revisit_enabled", False)
)
if revisit_enabled:
# 每次记录预约时,都重新读取直播间标题,不使用缓存
# 这样可以确保每个预约记录的是当时的真实直播间标题
try:
# 在函数开头导入,避免作用域问题
from douyin.fudai import (
douyin_fudai_detail as dfd_module,
)
y_pianyi_val = int(
getattr(analyser, "y_pianyi", 0) or 0
)
room_title, room_title_raw = (
dfd_module.read_live_room_title_core(
analyser, y_pianyi_val
)
)
# 更新 analyser 的属性,用于状态文件显示当前直播间
try:
analyser.current_room_title = room_title
analyser.current_room_title_raw = room_title_raw
except Exception:
pass
except Exception as e_inner:
try:
analyser._log_kv(
"预约",
"读取直播间标题失败,尝试使用缓存",
error=str(e_inner),
error_type=type(e_inner).__name__,
)
except Exception:
pass
# 如果读取失败,尝试使用缓存(作为兜底)
room_title = str(
getattr(analyser, "current_room_title", "") or ""
)
room_title_raw = str(
getattr(analyser, "current_room_title_raw", "")
or ""
)
else:
# 高价值房间回访未开启时,不读取标题以避免不必要的截图
room_title = str(
getattr(analyser, "current_room_title", "") or ""
)
room_title_raw = str(
getattr(analyser, "current_room_title_raw", "") or ""
)
except Exception:
pass
# 使用预先读取的直播间标题(避免在可能切换房间后读取到错误的标题)
room_title = current_room_title
room_title_raw = current_room_title_raw
# 记录高价值预约(仅负责入队和写状态,不立即改变当前直播间)
# 使用全局导入的模块(已在文件顶部导入)
@@ -5351,18 +5411,10 @@ def basic_join_decision(
except Exception:
pass
# 确保位置信息已更新到运行时映射表(仅当高价值房间回访启用时)
# 记录高价值预约后,立即记录房间标题到映射表,确保一个直播间只记录一次
try:
revisit_enabled = bool(
getattr(analyser, "high_value_room_revisit_enabled", False)
)
if revisit_enabled and hasattr(
analyser, "_record_room_title_and_pos"
):
if hasattr(analyser, "_record_room_title_and_pos"):
analyser._record_room_title_and_pos()
elif not revisit_enabled:
# 回访禁用时,跳过记录以避免不必要的截图
pass
else:
# 如果没有这个方法,手动记录
try:
@@ -91,7 +91,7 @@ class ReservationHandler:
else:
# 兼容旧格式
recorded_title, recorded_title_raw = room_info
if not recorded_title and not recorded_title_raw:
continue
@@ -138,7 +138,7 @@ class ReservationHandler:
recorded_title = room_info.get("title", "")
else:
recorded_title = room_info[0] if room_info else ""
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] _find_room_pos_by_title:在映射表中找到匹配位置",
@@ -199,7 +199,24 @@ class ReservationHandler:
back_verify_enabled = bool(
getattr(analyser, "reservation_back_verify_enabled", True)
)
# 添加调试日志
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 检查映射表记录条件",
mapping_enabled=mapping_enabled,
revisit_enabled=revisit_enabled,
back_verify_enabled=back_verify_enabled,
condition_result=not mapping_enabled
or (not revisit_enabled and not back_verify_enabled),
)
if not mapping_enabled or (not revisit_enabled and not back_verify_enabled):
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 跳过映射表记录",
reason="config_disabled",
)
return
except Exception:
pass
@@ -230,6 +247,19 @@ class ReservationHandler:
fudai_content = str(getattr(analyser, "_current_fudai_content", ""))
fudai_detail = getattr(analyser, "_last_fudai_detail_tuple", None)
# 添加调试日志
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 准备记录映射表",
room_pos=room_pos,
room_title=room_title[:30] if room_title else "",
has_fudai=has_fudai,
fudai_content=fudai_content[:30] if fudai_content else "",
current_map_size=len(
getattr(analyser, "_room_pos_to_title_map", {})
),
)
room_info = {
"title": room_title,
"title_raw": room_title_raw,
@@ -239,7 +269,22 @@ class ReservationHandler:
"timestamp": time.time(),
}
if room_pos not in getattr(analyser, "_room_pos_to_title_map", {}):
# 检查是否应该记录(位置不存在)
should_record = room_pos not in getattr(
analyser, "_room_pos_to_title_map", {}
)
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 检查记录条件",
room_pos=room_pos,
room_pos_in_map=not should_record,
should_record=should_record,
has_fudai=has_fudai,
fudai_content=fudai_content[:30] if fudai_content else "",
)
if should_record:
analyser._room_pos_to_title_map[room_pos] = room_info
try:
analyser._log_kv( # type: ignore[attr-defined]
@@ -267,6 +312,16 @@ class ReservationHandler:
except Exception:
pass
else:
analyser._log_kv( # type: ignore[attr-defined]
"预约",
"[预约] 跳过记录:位置已存在",
room_pos=room_pos,
has_fudai=has_fudai,
fudai_content=fudai_content[:30] if fudai_content else "",
current_map_size=len(
getattr(analyser, "_room_pos_to_title_map", {})
),
)
# 如果已存在,检查是否需要更新
old_info = analyser._room_pos_to_title_map[room_pos]
needs_update = (
@@ -987,7 +1042,9 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
# 写 CSV
with open(csv_tmp, "w", encoding="utf-8", newline="") as cf:
writer = csv.writer(cf)
writer.writerow(["pos", "title", "title_raw", "has_fudai", "fudai_content"])
writer.writerow(
["pos", "title", "title_raw", "has_fudai", "fudai_content"]
)
for k in sorted(
filtered.keys(), key=lambda x: int(x) if x.isdigit() else x
):
@@ -1003,7 +1060,9 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
title_raw = v[1] if len(v) > 1 else ""
has_fudai = False
fudai_content = ""
writer.writerow([k, title, title_raw, has_fudai, fudai_content[:100]])
writer.writerow(
[k, title, title_raw, has_fudai, fudai_content[:100]]
)
try:
os.replace(csv_tmp, csv_path)
except Exception:
@@ -1022,14 +1081,20 @@ def _dump_room_pos_map_to_state(analyser: Any) -> None:
title = v.get("title", "").replace("|", "\\|")
title_raw = v.get("title_raw", "").replace("|", "\\|")
has_fudai = "✓" if v.get("has_fudai", False) else ""
fudai_content = (v.get("fudai_content", "")[:50] + "..." if len(v.get("fudai_content", "")) > 50 else v.get("fudai_content", "")).replace("|", "\\|")
fudai_content = (
v.get("fudai_content", "")[:50] + "..."
if len(v.get("fudai_content", "")) > 50
else v.get("fudai_content", "")
).replace("|", "\\|")
else:
# 兼容旧格式
title = (v[0] if len(v) > 0 else "").replace("|", "\\|")
title_raw = (v[1] if len(v) > 1 else "").replace("|", "\\|")
has_fudai = ""
fudai_content = ""
mf.write(f"| {k} | {title} | {title_raw} | {has_fudai} | {fudai_content} |\n")
mf.write(
f"| {k} | {title} | {title_raw} | {has_fudai} | {fudai_content} |\n"
)
try:
os.replace(md_tmp, md_path)
except Exception:
+93 -15
View File
@@ -2697,7 +2697,21 @@ def main():
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", "has_fudai", "fudai_content", "fudai_time", "fudai_value", "participants", "copies", "is_reserved"), show="headings", height=12
mapping_tab,
columns=(
"pos",
"title",
"title_raw",
"has_fudai",
"fudai_content",
"fudai_time",
"fudai_value",
"participants",
"copies",
"is_reserved",
),
show="headings",
height=12,
)
mapping_tree.heading("pos", text="位置")
mapping_tree.heading("title", text="标题")
@@ -2736,7 +2750,9 @@ def main():
reservations_paths = [
os.path.join(base_dir, "douyin", "core", "state", "reservations.json"),
os.path.join(base_dir, "douyin", "state", "reservations.json"),
os.path.join(os.getcwd(), "douyin", "core", "state", "reservations.json"),
os.path.join(
os.getcwd(), "douyin", "core", "state", "reservations.json"
),
]
for res_path in reservations_paths:
try:
@@ -2784,7 +2800,20 @@ def main():
if not parsed:
# 非JSON或解析错误:在表格中显示原始文本的一行提示
mapping_tree.insert(
"", "end", values=("-", "(非JSON,查看原始文件)", "", "", "", "", "", "", "", "")
"",
"end",
values=(
"-",
"(非JSON,查看原始文件)",
"",
"",
"",
"",
"",
"",
"",
"",
),
)
else:
# 移除元信息
@@ -2811,24 +2840,42 @@ def main():
fudai_content = (
v.get("fudai_content", "") or ""
).strip() or "-"
# 提取福袋详细信息
fudai_detail = v.get("fudai_detail")
if fudai_detail and isinstance(fudai_detail, (tuple, list)) and len(fudai_detail) >= 5:
if (
fudai_detail
and isinstance(fudai_detail, (tuple, list))
and len(fudai_detail) >= 5
):
# fudai_detail结构: (content, time_text, participants, copies, prize_value_yuan, countdown_tuple)
fudai_time = fudai_detail[1] or "-" # time_text
fudai_value = f"{fudai_detail[4] or 0:.2f}" if fudai_detail[4] else "-" # prize_value_yuan
participants = str(fudai_detail[2] or 0) if fudai_detail[2] else "-" # participants
copies = str(fudai_detail[3] or 0) if fudai_detail[3] else "-" # copies
fudai_value = (
f"{fudai_detail[4] or 0:.2f}"
if fudai_detail[4]
else "-"
) # prize_value_yuan
participants = (
str(fudai_detail[2] or 0)
if fudai_detail[2]
else "-"
) # participants
copies = (
str(fudai_detail[3] or 0)
if fudai_detail[3]
else "-"
) # copies
else:
fudai_time = "-"
fudai_value = "-"
participants = "-"
copies = "-"
# 检查是否已预约
is_reserved = "✓" if int(k) in reservations_data else ""
is_reserved = (
"✓" if int(k) in reservations_data else ""
)
else:
# 兼容旧格式:元组
title = (v[0] or "").strip() or "-"
@@ -2839,10 +2886,37 @@ def main():
fudai_value = "-"
participants = "-"
copies = "-"
is_reserved = "✓" if int(k) in reservations_data else ""
is_reserved = (
"✓" if int(k) in reservations_data else ""
)
except Exception:
title, title_raw, has_fudai, fudai_content, fudai_time, fudai_value, participants, copies, is_reserved = "-", "-", "", "-", "-", "-", "-", "-", "-"
mapping_tree.insert("", "end", values=(k, title, title_raw, has_fudai, fudai_content, fudai_time, fudai_value, participants, copies, is_reserved))
(
title,
title_raw,
has_fudai,
fudai_content,
fudai_time,
fudai_value,
participants,
copies,
is_reserved,
) = ("-", "-", "", "-", "-", "-", "-", "-", "-")
mapping_tree.insert(
"",
"end",
values=(
k,
title,
title_raw,
has_fudai,
fudai_content,
fudai_time,
fudai_value,
participants,
copies,
is_reserved,
),
)
found = True
break
@@ -2852,7 +2926,11 @@ def main():
if not found:
mapping_source_var.set("未找到 room_pos_map.json")
mapping_tree.insert("", "end", values=("-", "未找到映射表文件", "", "", "", "", "", "", "", ""))
mapping_tree.insert(
"",
"end",
values=("-", "未找到映射表文件", "", "", "", "", "", "", "", ""),
)
# 绑定刷新按钮与自动刷新
btns = map_ctrl.winfo_children()