diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 326d8f7..d9718f0 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1140,37 +1140,8 @@ class fudai_analyse: if not ok: return - # 结构化预日志,记录缓存的阶段诊断结果(若存在),便于排查为何后续没复用首次诊断结果 - try: - self._log_kv( - "阶段诊断", - "调用阶段诊断-前置检查", - trigger_reason="run_once_basic_initial", - fudai_detail_task_count_from_stage=getattr( - self, "_fudai_detail_task_count_from_stage", None - ), - screenshot_ready_for_ocr=bool( - getattr(self, "_screenshot_ready_for_ocr", False) - ), - ) - except Exception: - pass - - stage, seen = self._diagnose_stage_from_current_screen( - trigger_reason="run_once_basic_initial" - ) - self._last_diag_stage = stage - self._last_diag_seen = seen - self._last_diag_ts = time.time() - # 简洁输出阶段诊断结果(不额外添加分隔符,避免日志噪音) - elapsed = time.time() - start_time - self._log_kv( - "截图", - "截图+阶段诊断", - stage=stage, - seen=seen, - elapsed_seconds=round(elapsed, 2), - screenshot_elapsed_seconds=round(screenshot_elapsed, 2), + stage, seen = fudai_run_blocks.diagnose_stage_with_logging( + self, start_time, screenshot_elapsed ) # 1.5. 检测是否意外返回桌面(不在抖音前台),若是则尝试恢复 @@ -1183,60 +1154,9 @@ class fudai_analyse: if fudai_run_blocks.handle_high_value_room_revisit(self, start_time): return - # 处理弹窗阶段 - if stage == "COUPON_POPUP": - fudai_run_blocks.handle_coupon_popup_stage(self, start_time) + # 处理 stage 分发 + if fudai_run_blocks.dispatch_stage(self, stage, seen, start_time): return - elif stage == "SHOPPING_REDPACKET_POPUP": - fudai_run_blocks.handle_shopping_redpacket_popup_stage(self, start_time) - return - elif stage == "FANSCLUB_JOIN": - # 粉丝团加入阶段:先点击参与按钮,再点击两次预设位置,最后关闭详情页 - fudai_run_blocks.handle_fansclub_join_stage(self, start_time) - return - elif stage == "FUDai_DETAIL": - # 已经在详情页:直接按正常详情流程处理(已移除预约回退直接参与标志) - try: - self._log_kv( - "预约", - "[预约] 检测到福袋详情页,按正常详情流程处理", - stage=stage, - ) - except Exception: - pass - self._handle_in_fudai_detail() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理福袋详情)", - elapsed_seconds=round(elapsed, 2), - ) - elif stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): - # 在直播间:按正常直播间流程处理(已移除预约回退直接参与标志) - fudai_run_blocks.handle_in_live_room_stage(self, stage, start_time) - elif stage in ("RECOMMEND_PAGE", "FOLLOW_LIST", "LIVE_LIST"): - # 处理推荐/关注/直播列表页:回到直播间或切换 - if stage == "RECOMMEND_PAGE": - fudai_run_blocks.handle_recommend_page_stage(self, stage, seen) - else: - # 其他列表页:使用原有逻辑 - douyin_live_room.reflash_zhibo(self) - elif stage == "FUDai_RESULT": - # 抽奖结果弹窗:处理开奖结果(使用OCR识别,不使用阶段诊断) - fudai_run_blocks.handle_lottery_result_stage(self, start_time) - return - elif stage in ("LIVE_CLOSED",): - # 直播结束:下滑/切下一个 - fudai_run_blocks.handle_live_closed_stage(self, start_time) - else: - # 未识别出特殊阶段:尽量保持原地,交给上层循环 - elapsed = time.time() - start_time - self._log_kv( - "阶段", - "未匹配到特定阶段", - stage=stage, - elapsed_seconds=round(elapsed, 2), - ) def _check_lottery_result_by_ocr(self) -> Optional[str]: """通过OCR识别开奖结果按钮区域,返回识别到的文本类型。""" diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 6c2fe4e..c3e8d6f 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -1466,6 +1466,117 @@ def handle_recommend_page_stage(analyser: Any, stage: str, seen: Any) -> None: pass +def diagnose_stage_with_logging( + analyser: Any, start_time: float, screenshot_elapsed: float +) -> tuple[str, str]: + """前置预日志 + 调用 _diagnose_stage_from_current_screen + 写结果日志。 + + 返回 (stage, seen)。同时把诊断结果保存到 _last_diag_* 字段。 + """ + try: + analyser._log_kv( + "阶段诊断", + "调用阶段诊断-前置检查", + trigger_reason="run_once_basic_initial", + fudai_detail_task_count_from_stage=getattr( + analyser, "_fudai_detail_task_count_from_stage", None + ), + screenshot_ready_for_ocr=bool( + getattr(analyser, "_screenshot_ready_for_ocr", False) + ), + ) + except Exception: + pass + + stage, seen = analyser._diagnose_stage_from_current_screen( + trigger_reason="run_once_basic_initial" + ) + analyser._last_diag_stage = stage + analyser._last_diag_seen = seen + analyser._last_diag_ts = time.time() + + try: + elapsed = time.time() - start_time + analyser._log_kv( + "截图", + "截图+阶段诊断", + stage=stage, + seen=seen, + elapsed_seconds=round(elapsed, 2), + screenshot_elapsed_seconds=round(screenshot_elapsed, 2), + ) + except Exception: + pass + return stage, seen + + +def dispatch_stage(analyser: Any, stage: str, seen: str, start_time: float) -> bool: + """根据 stage 分发到对应的 stage handler。 + + 返回 True 表示主流程应 return(已处理且结束)。 + 返回 False 表示当前 stage 不需要 return(继续走主循环)。 + """ + if stage == "COUPON_POPUP": + handle_coupon_popup_stage(analyser, start_time) + return True + if stage == "SHOPPING_REDPACKET_POPUP": + handle_shopping_redpacket_popup_stage(analyser, start_time) + return True + if stage == "FANSCLUB_JOIN": + handle_fansclub_join_stage(analyser, start_time) + return True + if stage == "FUDai_DETAIL": + try: + analyser._log_kv( + "预约", + "[预约] 检测到福袋详情页,按正常详情流程处理", + stage=stage, + ) + except Exception: + pass + analyser._handle_in_fudai_detail() + try: + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理福袋详情)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass + return False # 注意:原代码这里没有 return,让循环继续 + if stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): + handle_in_live_room_stage(analyser, stage, start_time) + return False + if stage in ("RECOMMEND_PAGE", "FOLLOW_LIST", "LIVE_LIST"): + if stage == "RECOMMEND_PAGE": + handle_recommend_page_stage(analyser, stage, seen) + else: + from douyin.other import douyin_live_room + + douyin_live_room.reflash_zhibo(analyser) + return False + if stage == "FUDai_RESULT": + handle_lottery_result_stage(analyser, start_time) + return True + if stage == "LIVE_CLOSED": + handle_live_closed_stage(analyser, start_time) + return False + + # 未识别出特殊阶段:尽量保持原地,交给上层循环 + try: + elapsed = time.time() - start_time + analyser._log_kv( + "阶段", + "未匹配到特定阶段", + stage=stage, + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass + return False + + def handle_queue_full_wait_at_earliest( analyser: Any, start_time: float, diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 81a2a17..bc0a9a9 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -957,6 +957,112 @@ def test_log_skip_normal_state_no_log(): a._log_kv.assert_not_called() +# ---- diagnose_stage_with_logging ---- # + +def test_diagnose_stage_logs_and_caches_result(): + a = _make_analyser() + a._diagnose_stage_from_current_screen = MagicMock( + return_value=("IN_LIVE_ROOM", "seen_text") + ) + stage, seen = fudai_run_blocks.diagnose_stage_with_logging( + a, time.time(), screenshot_elapsed=0.5 + ) + assert stage == "IN_LIVE_ROOM" + assert seen == "seen_text" + assert a._last_diag_stage == "IN_LIVE_ROOM" + assert a._last_diag_seen == "seen_text" + assert a._last_diag_ts > 0 + + +def test_diagnose_stage_swallows_log_exceptions(): + a = _make_analyser() + a._diagnose_stage_from_current_screen = MagicMock( + return_value=("LIVE_CLOSED", "") + ) + a._log_kv.side_effect = RuntimeError("boom") + stage, seen = fudai_run_blocks.diagnose_stage_with_logging( + a, time.time(), screenshot_elapsed=0.1 + ) + assert stage == "LIVE_CLOSED" + + +# ---- dispatch_stage ---- # + +def test_dispatch_coupon_popup_returns_true(): + a = _make_analyser() + result = fudai_run_blocks.dispatch_stage(a, "COUPON_POPUP", "", time.time()) + assert result is True + + +def test_dispatch_shopping_redpacket_returns_true(): + a = _make_analyser() + a.resolution_ratio_x = 1080 + a.resolution_ratio_y = 2400 + result = fudai_run_blocks.dispatch_stage( + a, "SHOPPING_REDPACKET_POPUP", "", time.time() + ) + assert result is True + + +def test_dispatch_fudai_detail_calls_handler_returns_false(): + a = _make_analyser() + a._handle_in_fudai_detail = MagicMock() + result = fudai_run_blocks.dispatch_stage(a, "FUDai_DETAIL", "", time.time()) + assert result is False + a._handle_in_fudai_detail.assert_called_once() + + +def test_dispatch_in_live_room_returns_false(): + a = _make_analyser() + a._just_switched_room = False + a._handle_live_room_has_or_not_fudai = MagicMock() + result = fudai_run_blocks.dispatch_stage(a, "IN_LIVE_ROOM", "", time.time()) + assert result is False + + +def test_dispatch_recommend_page_returns_false(): + a = _make_analyser() + a.resolution_ratio_x = 1080 + a.resolution_ratio_y = 2400 + a.FOLLOW_TAB_CLICK_DELAY = 0.5 + a.ENTER_LIVE_CLICK_DELAY = 0.6 + a.ENTER_LIVE_FINAL_DELAY = 1.0 + a.RETURN_STEP_DELAY_SECONDS = 0.8 + result = fudai_run_blocks.dispatch_stage(a, "RECOMMEND_PAGE", "", time.time()) + assert result is False + + +def test_dispatch_follow_list_calls_reflash_returns_false(): + a = _make_analyser() + from douyin.other import douyin_live_room + + with patch.object(douyin_live_room, "reflash_zhibo") as fake: + result = fudai_run_blocks.dispatch_stage(a, "FOLLOW_LIST", "", time.time()) + assert result is False + fake.assert_called_once_with(a) + + +def test_dispatch_lottery_result_returns_true(): + a = _make_analyser() + a._check_lottery_result_by_ocr.return_value = None + result = fudai_run_blocks.dispatch_stage(a, "FUDai_RESULT", "", time.time()) + assert result is True + + +def test_dispatch_live_closed_returns_false(): + a = _make_analyser() + result = fudai_run_blocks.dispatch_stage(a, "LIVE_CLOSED", "", time.time()) + assert result is False + + +def test_dispatch_unknown_stage_logs_and_returns_false(): + a = _make_analyser() + result = fudai_run_blocks.dispatch_stage(a, "UNKNOWN_X", "", time.time()) + assert result is False + msgs = [c.args[1] for c in a._log_kv.call_args_list if len(c.args) >= 2] + assert any("未匹配到特定阶段" in m for m in msgs) + + # ---- execute_reservation_attend_or_cleanup ---- # def _make_reservation_analyser():