diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index ac0423d..7428d11 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1633,74 +1633,15 @@ class fudai_analyse: pass # 检查是否刚刚记录了预约,如果是,强制切换直播间,避免在同一直播间循环 - try: - just_recorded = getattr(self, "_just_recorded_reservation", False) - if just_recorded: - self._just_recorded_reservation = False # 清除标志 - try: - self._log_kv( - "预约", - "[预约] 刚刚记录预约,继续在新直播间搜索福袋", - ) - except Exception: - pass - try: - needswitch_cfg = bool(getattr(self, "needswitch", True)) - except Exception: - needswitch_cfg = True - if needswitch_cfg: - try: - self.swipe_up_to_next_live() - except Exception as exc: - try: - self._log_kv( - "预约", - "[预约] 执行上滑切换失败", - error=str(exc), - error_type=type(exc).__name__, - ) - except Exception: - pass - else: - try: - self._log_kv( - "预约", - "[预约] just_recorded_reservation 触发,但 needswitch=False,未切换", - ) - except Exception: - pass - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(记录预约后切换直播间)", - elapsed_seconds=round(elapsed, 2), - ) - return - except Exception: - pass + if fudai_run_blocks.handle_just_recorded_reservation(self, start_time): + return # 预约检查耗时记录 reservation_elapsed = time.time() - reservation_check_start self._log_kv("预约", f"预约检查完成,耗时: {reservation_elapsed:.3f}s") # 0.5. 检查是否需要跳过阶段检测,直接查找福袋(点击参与抽奖后) - skip_stage_diagnosis = getattr( - self, "_skip_stage_diagnosis_and_find_fudai", False - ) - if skip_stage_diagnosis: - try: - self._skip_stage_diagnosis_and_find_fudai = False # 清除标志 - self._log_kv("流程", "点击参与抽奖后,跳过阶段检测,直接查找福袋") - except Exception: - pass - # 直接查找福袋,跳过阶段检测 - self._handle_live_room_has_or_not_fudai() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(跳过阶段检测)", - elapsed_seconds=round(elapsed, 2), - ) + if fudai_run_blocks.handle_skip_stage_diagnosis(self, start_time): return # 1. 截图 + 阶段诊断 diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 6435123..3feb787 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -655,6 +655,92 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: ) +def handle_just_recorded_reservation(analyser: Any, start_time: float) -> bool: + """如果刚刚记录了预约,按 needswitch 决定是否上滑切换直播间,然后 return。 + + 返回 True 表示触发了该分支且主流程应 return。 + """ + try: + just_recorded = getattr(analyser, "_just_recorded_reservation", False) + if not just_recorded: + return False + + analyser._just_recorded_reservation = False + try: + analyser._log_kv( + "预约", + "[预约] 刚刚记录预约,继续在新直播间搜索福袋", + ) + except Exception: + pass + try: + needswitch_cfg = bool(getattr(analyser, "needswitch", True)) + except Exception: + needswitch_cfg = True + if needswitch_cfg: + try: + analyser.swipe_up_to_next_live() + except Exception as exc: + try: + analyser._log_kv( + "预约", + "[预约] 执行上滑切换失败", + error=str(exc), + error_type=type(exc).__name__, + ) + except Exception: + pass + else: + try: + analyser._log_kv( + "预约", + "[预约] just_recorded_reservation 触发,但 needswitch=False,未切换", + ) + except Exception: + pass + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(记录预约后切换直播间)", + elapsed_seconds=round(elapsed, 2), + ) + return True + except Exception: + return False + + +def handle_skip_stage_diagnosis(analyser: Any, start_time: float) -> bool: + """如果设置了跳过阶段检测标志,直接查找福袋后 return。 + + 返回 True 表示已触发跳过路径,主流程应 return。 + """ + skip_stage_diagnosis = getattr( + analyser, "_skip_stage_diagnosis_and_find_fudai", False + ) + if not skip_stage_diagnosis: + return False + + try: + analyser._skip_stage_diagnosis_and_find_fudai = False + analyser._log_kv("流程", "点击参与抽奖后,跳过阶段检测,直接查找福袋") + except Exception: + pass + try: + analyser._handle_live_room_has_or_not_fudai() + except Exception: + pass + try: + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(跳过阶段检测)", + elapsed_seconds=round(elapsed, 2), + ) + except Exception: + pass + return True + + def maybe_clear_reservation_queue_on_first_run(analyser: Any) -> None: """如果是首次运行,清空预约队列并把状态文件也写空。 diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 94de1fc..b6a08b7 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -465,3 +465,60 @@ def test_clear_queue_swallows_log_exceptions(): # 应不抛错 fudai_run_blocks.maybe_clear_reservation_queue_on_first_run(a) assert a.reservations == [] + + +# ---- handle_just_recorded_reservation ---- # + +def test_just_recorded_not_set_returns_false(): + a = _make_analyser() + a._just_recorded_reservation = False + assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is False + + +def test_just_recorded_with_switch_swipes_up(): + a = _make_analyser() + a._just_recorded_reservation = True + a.needswitch = True + assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True + a.swipe_up_to_next_live.assert_called_once() + assert a._just_recorded_reservation is False + + +def test_just_recorded_no_switch_skips_swipe(): + a = _make_analyser() + a._just_recorded_reservation = True + a.needswitch = False + assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True + a.swipe_up_to_next_live.assert_not_called() + + +def test_just_recorded_swipe_failure_still_returns_true(): + a = _make_analyser() + a._just_recorded_reservation = True + a.needswitch = True + a.swipe_up_to_next_live.side_effect = RuntimeError("boom") + assert fudai_run_blocks.handle_just_recorded_reservation(a, time.time()) is True + + +# ---- handle_skip_stage_diagnosis ---- # + +def test_skip_stage_diag_not_set_returns_false(): + a = _make_analyser() + a._skip_stage_diagnosis_and_find_fudai = False + assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is False + + +def test_skip_stage_diag_set_calls_handler_and_clears_flag(): + a = _make_analyser() + a._skip_stage_diagnosis_and_find_fudai = True + a._handle_live_room_has_or_not_fudai = MagicMock() + assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is True + a._handle_live_room_has_or_not_fudai.assert_called_once() + assert a._skip_stage_diagnosis_and_find_fudai is False + + +def test_skip_stage_diag_handler_failure_still_returns_true(): + a = _make_analyser() + a._skip_stage_diagnosis_and_find_fudai = True + a._handle_live_room_has_or_not_fudai = MagicMock(side_effect=RuntimeError("boom")) + assert fudai_run_blocks.handle_skip_stage_diagnosis(a, time.time()) is True