From 5a51c951a2707c764177c97f75a4216f391338d3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 1 Jun 2026 12:33:33 +0800 Subject: [PATCH] fix(fudai): treat notice text as attended and harden attend/log parsing --- douyin/fudai/core/fudai_attend.py | 28 ++++++- .../douyin_fudai_core_pkg/button_state.py | 5 +- gui/web_gui_pkg/stats_log_parser.py | 32 ++++++-- tests/test_fudai_attend.py | 23 +++++- tests/test_fudai_button_state.py | 5 +- tests/test_web_gui_stats_log_parser.py | 82 +++++++++++++++++++ 6 files changed, 158 insertions(+), 17 deletions(-) create mode 100644 tests/test_web_gui_stats_log_parser.py diff --git a/douyin/fudai/core/fudai_attend.py b/douyin/fudai/core/fudai_attend.py index f636a38..49c9940 100644 --- a/douyin/fudai/core/fudai_attend.py +++ b/douyin/fudai/core/fudai_attend.py @@ -56,7 +56,12 @@ def attend_choujiang(analyser) -> bool: bt = str(candidate or "").strip() if bt: # 推断按钮状态(与并行OCR的判断逻辑保持一致) - if "参与成功" in bt or "等待开奖" in bt: + if ( + "参与成功" in bt + or "等待开奖" in bt + or "失去抽奖" in bt + or "否则会失去" in bt + ): bs = "已参与" elif "加入粉丝团" in bt or "粉丝团" in bt: bs = "需要加入粉丝团" @@ -174,7 +179,13 @@ def attend_choujiang(analyser) -> bool: analyser._log_kv("参与", "关闭福袋详情", action="关闭详情") return False elif ( - attend_button_text and "参与成功" in attend_button_text + attend_button_text + and ( + "参与成功" in attend_button_text + or "等待开奖" in attend_button_text + or "失去抽奖" in attend_button_text + or "否则会失去" in attend_button_text + ) ): # 如果识别到已经参与抽奖 analyser._log_kv("参与", "已参与,等待开奖", result="已参与") @@ -207,7 +218,13 @@ def attend_choujiang(analyser) -> bool: future_ts = 0.0 try: # 从 detail 中获取 future_ts(如果可用) - future_ts = getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0 + future_ts_raw = ( + getattr(analyser, "_current_fudai_future_ts", 0.0) or 0.0 + ) + try: + future_ts = float(future_ts_raw) + except Exception: + future_ts = 0.0 # 如果没有从 detail 中获取到 future_ts,尝试直接解析当前详情页的倒计时文本 if not future_ts: try: @@ -221,7 +238,10 @@ def attend_choujiang(analyser) -> bool: res = analyser.operation.check_countdown(time_text) if isinstance(res, tuple) and len(res) == 2: _, parsed_future = res - future_ts = float(parsed_future or 0.0) + try: + future_ts = float(parsed_future or 0.0) + except Exception: + future_ts = 0.0 except Exception: pass except Exception: diff --git a/douyin/fudai/douyin_fudai_core_pkg/button_state.py b/douyin/fudai/douyin_fudai_core_pkg/button_state.py index 0bac457..d1fe948 100644 --- a/douyin/fudai/douyin_fudai_core_pkg/button_state.py +++ b/douyin/fudai/douyin_fudai_core_pkg/button_state.py @@ -8,10 +8,9 @@ from typing import Any def decide_button_status(button_text: str) -> str: txt = str(button_text or "").strip() - # 参与按钮 OCR 区域偶发误读到提示文案(例如“直播间,否则会失去抽奖机会”), - # 这类文本不代表可点击状态,统一归为未知,交给后续兜底路径处理。 + # 新版按钮文案:“直播间,否则会失去抽奖机会” 实际语义等价于已参与后等待开奖。 if "失去抽奖" in txt or "否则会失去" in txt: - return "未知状态" + return "已参与" if "参与成功" in txt or "等待开奖" in txt: return "已参与" if "加入粉丝团" in txt or "粉丝团" in txt: diff --git a/gui/web_gui_pkg/stats_log_parser.py b/gui/web_gui_pkg/stats_log_parser.py index 31c1311..c41a8a8 100644 --- a/gui/web_gui_pkg/stats_log_parser.py +++ b/gui/web_gui_pkg/stats_log_parser.py @@ -83,13 +83,23 @@ def _parse_fudai_fields(runner: Any, s: str) -> None: content_extracted = False room_title = "" if "福袋内容:" in s: - runner._fudai_last["content"] = ( - s.split("福袋内容:", 1)[1].strip().split()[0] - ) + content_part = s.split("福袋内容:", 1)[1].strip() + # 只取真正的“内容值”,避免把后续“按钮文本=...”等附加字段吞进来污染统计面板 + cut_markers = (" 按钮文本", " room_title=", " room_title:", " 倒计时", " 价值=") + for marker in cut_markers: + idx = content_part.find(marker) + if idx > 0: + content_part = content_part[:idx].strip() + break + runner._fudai_last["content"] = content_part.split()[0] if content_part else "" runner._fudai_last["updated_ts"] = time.time() content_extracted = True else: - m = re.search(r"(?:字段_)?内容[=:]\s*([^\s]+)", s) + # “内容”字段必须是明确键名,避免误匹配“内容关键词过滤:通过”等普通日志文案 + m = re.search( + r"(? None: runner._fudai_last["updated_ts"] = time.time() if "倒计时时间:" in s: + countdown_part = s.split("倒计时时间:", 1)[1].strip() + for marker in (" 内容", " 按钮文本", " room_title=", " room_title:", " 价值="): + idx = countdown_part.find(marker) + if idx > 0: + countdown_part = countdown_part[:idx].strip() + break runner._fudai_last["countdown"] = ( - s.split("倒计时时间:", 1)[1].strip().split()[0] + countdown_part.split()[0] if countdown_part else "" ) runner._fudai_last["updated_ts"] = time.time() else: - m = re.search(r"(?:字段_)?倒计时[=:]?\s*([^\s]+)", s) + # 同上:限制为明确“倒计时”字段键,避免“倒计时过滤:通过”污染 countdown。 + m = re.search( + r"(? None: analyser = MagicMock() analyser._fudai_button_status = "未知状态" - analyser._fudai_button_text = "直播间,否则会失去抽奖机会" + analyser._fudai_button_text = "按钮文案发生变化" analyser._fudai_button_ocr_time = 10**12 analyser._fudai_button_room_pos = 0 analyser.room_pos = 0 @@ -24,3 +24,24 @@ def test_attend_choujiang_fallback_click_on_unknown_button_text() -> None: assert ok is True analyser.click.assert_called_once() + +def test_attend_choujiang_treat_notice_text_as_attended() -> None: + analyser = MagicMock() + analyser._fudai_button_status = "已参与" + analyser._fudai_button_text = "直播间,否则会失去抽奖机会" + analyser._fudai_button_ocr_time = 10**12 + analyser._fudai_button_room_pos = 0 + analyser.room_pos = 0 + analyser._run_id = "test-run" + analyser._current_fudai_future_ts = 0.0 + analyser._log_kv = MagicMock() + analyser.click = MagicMock() + analyser.operation = MagicMock() + analyser.operation.delay = MagicMock() + analyser.last_attend_fail_reason = "" + + ok = attend_choujiang(analyser) + + assert ok is True + # “已参与”路径会点击关闭详情 + analyser.click.assert_called_once() diff --git a/tests/test_fudai_button_state.py b/tests/test_fudai_button_state.py index 071634a..d12a4aa 100644 --- a/tests/test_fudai_button_state.py +++ b/tests/test_fudai_button_state.py @@ -3,6 +3,5 @@ from __future__ import annotations from douyin.fudai.douyin_fudai_core_pkg.button_state import decide_button_status -def test_decide_button_status_unknown_for_notice_text() -> None: - assert decide_button_status("直播间,否则会失去抽奖机会") == "未知状态" - +def test_decide_button_status_attended_for_notice_text() -> None: + assert decide_button_status("直播间,否则会失去抽奖机会") == "已参与" diff --git a/tests/test_web_gui_stats_log_parser.py b/tests/test_web_gui_stats_log_parser.py new file mode 100644 index 0000000..bff8c63 --- /dev/null +++ b/tests/test_web_gui_stats_log_parser.py @@ -0,0 +1,82 @@ +from __future__ import annotations + +from types import SimpleNamespace + +from gui.web_gui_pkg.stats_log_parser import update_stats_from_line + + +def _make_runner() -> SimpleNamespace: + r = SimpleNamespace() + r._fudai_last = { + "content": "", + "countdown": "", + "participants": None, + "copies": None, + "prize_value_yuan": None, + "updated_ts": None, + } + r._recorded_fudai_seen = set() + r._current_trace_id = "" + r._mdc_context = {} + r._counters = {} + r._events = [] + + def _inc(key: str, n: int = 1) -> None: + r._counters[key] = int(r._counters.get(key, 0)) + int(n) + + def _record_event(typ: str, meta: dict | None = None) -> None: + r._events.append({"type": typ, "meta": meta or {}}) + + def _snapshot_fudai() -> dict: + return { + "content": r._fudai_last.get("content") or "", + "countdown": r._fudai_last.get("countdown") or "", + "participants": r._fudai_last.get("participants"), + "copies": r._fudai_last.get("copies"), + "prize_value_yuan": r._fudai_last.get("prize_value_yuan"), + } + + def _record_win() -> None: + return None + + r._inc = _inc + r._record_event = _record_event + r._snapshot_fudai = _snapshot_fudai + r._record_win = _record_win + return r + + +def test_content_filter_log_should_not_override_fudai_content() -> None: + runner = _make_runner() + runner._fudai_last["content"] = "福袋内容A" + + update_stats_from_line( + runner, + "[2026-06-01 12:00:00.000] [INFO] [筛选] 内容关键词过滤:通过", + ) + + assert runner._fudai_last["content"] == "福袋内容A" + + +def test_countdown_filter_log_should_not_override_countdown() -> None: + runner = _make_runner() + runner._fudai_last["countdown"] = "02:48" + + update_stats_from_line( + runner, + "[2026-06-01 12:00:00.000] [INFO] [筛选] 倒计时过滤:通过", + ) + + assert runner._fudai_last["countdown"] == "02:48" + + +def test_fudai_content_line_with_extra_fields_extracts_only_content() -> None: + runner = _make_runner() + + update_stats_from_line( + runner, + "[2026-06-01 12:00:00.000] [DEBUG] [福袋] 福袋内容:未识别到 按钮文本=直播间,否则会失去抽", + ) + + assert runner._fudai_last["content"] == "未识别到" +