From 3aca02fae42b479afd043e8067a73aba0a1b56ef Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 18:37:18 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20run=5Fonce=5Fbas?= =?UTF-8?q?ic=202=20=E6=AE=B5=E7=8B=AC=E7=AB=8B=E9=80=BB=E8=BE=91=E5=88=B0?= =?UTF-8?q?=20run=5Fonce=5Fbasic=5Fhelpers=20(3233=20=E2=86=92=203174)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 2 个 helper: - handle_just_recorded_reservation:刚记录预约后强制切换直播间 - handle_skip_stage_diagnosis:0.5 跳过阶段检测分支 新增 5 个单元测试。行为保持不变。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 65 +-------------- .../fudai_modules/run_once_basic_helpers.py | 80 +++++++++++++++++++ tests/test_run_once_basic_helpers.py | 49 ++++++++++++ 3 files changed, 132 insertions(+), 62 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index af26fcc..d45cb60 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1754,74 +1754,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 _ronce_helpers.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 _ronce_helpers.handle_skip_stage_diagnosis(self, start_time): return # 1. 截图 + 阶段诊断 diff --git a/douyin/core/fudai_modules/run_once_basic_helpers.py b/douyin/core/fudai_modules/run_once_basic_helpers.py index 8b9f918..fafb0b0 100644 --- a/douyin/core/fudai_modules/run_once_basic_helpers.py +++ b/douyin/core/fudai_modules/run_once_basic_helpers.py @@ -891,3 +891,83 @@ def _handle_fansclub_join(self: "fudai_analyse", start_time: float) -> None: "run_once_basic完成(处理粉丝团)", elapsed_seconds=round(elapsed, 2), ) + + +def handle_just_recorded_reservation(self: "fudai_analyse", start_time: float) -> bool: + """检查 _just_recorded_reservation 标志,若刚记录了预约则强制切换直播间。 + + 返回 True 表示已切换并应当 `return`;False 表示未触发,继续后续步骤。 + """ + try: + just_recorded = getattr(self, "_just_recorded_reservation", False) + if not just_recorded: + return False + + 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 True + except Exception: + return False + + +def handle_skip_stage_diagnosis(self: "fudai_analyse", start_time: float) -> bool: + """0.5 跳过阶段检测:点击参与抽奖后直接查找福袋。 + + 返回 True 表示已处理并应当 `return`;False 表示标志未设置,继续后续步骤。 + """ + skip_stage_diagnosis = getattr( + self, "_skip_stage_diagnosis_and_find_fudai", False + ) + if not skip_stage_diagnosis: + return False + + 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), + ) + return True diff --git a/tests/test_run_once_basic_helpers.py b/tests/test_run_once_basic_helpers.py index 12333f5..8781c37 100644 --- a/tests/test_run_once_basic_helpers.py +++ b/tests/test_run_once_basic_helpers.py @@ -195,3 +195,52 @@ class TestDispatchStageBranch: s._handle_in_fudai_detail = MagicMock(return_value=None) ronce.dispatch_stage_branch(s, "FUDai_DETAIL", {}, 0.0) s._handle_in_fudai_detail.assert_called_once() + + +# ============================================================ +# handle_just_recorded_reservation +# ============================================================ + +class TestHandleJustRecordedReservation: + def test_flag_not_set_returns_false(self): + s = _mk_self() + s._just_recorded_reservation = False + assert ronce.handle_just_recorded_reservation(s, 0.0) is False + + def test_flag_set_with_needswitch_swipes(self): + s = _mk_self() + s._just_recorded_reservation = True + s.needswitch = True + s.swipe_up_to_next_live = MagicMock(return_value=None) + assert ronce.handle_just_recorded_reservation(s, 0.0) is True + # 标志应被清除 + assert s._just_recorded_reservation is False + s.swipe_up_to_next_live.assert_called_once() + + def test_flag_set_without_needswitch_no_swipe(self): + s = _mk_self() + s._just_recorded_reservation = True + s.needswitch = False + s.swipe_up_to_next_live = MagicMock(return_value=None) + assert ronce.handle_just_recorded_reservation(s, 0.0) is True + s.swipe_up_to_next_live.assert_not_called() + + +# ============================================================ +# handle_skip_stage_diagnosis +# ============================================================ + +class TestHandleSkipStageDiagnosis: + def test_flag_not_set_returns_false(self): + s = _mk_self() + s._skip_stage_diagnosis_and_find_fudai = False + assert ronce.handle_skip_stage_diagnosis(s, 0.0) is False + + def test_flag_set_finds_fudai_directly(self): + s = _mk_self() + s._skip_stage_diagnosis_and_find_fudai = True + s._handle_live_room_has_or_not_fudai = MagicMock(return_value=None) + assert ronce.handle_skip_stage_diagnosis(s, 0.0) is True + # 标志应被清除 + assert s._skip_stage_diagnosis_and_find_fudai is False + s._handle_live_room_has_or_not_fudai.assert_called_once()