From 849160304efe75bd2a801b4aded4d20b1f8cf95a Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 11:16:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20run=5Fonce=5Fbas?= =?UTF-8?q?ic=20=E4=B8=AD"=E5=88=9A=E8=AE=B0=E5=BD=95=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E5=90=8E=E5=88=87=E6=8D=A2=E7=9B=B4=E6=92=AD=E9=97=B4"?= =?UTF-8?q?=E5=9D=97=E5=88=B0=20try=5Fjust=5Frecorded=5Freservation=5Fswit?= =?UTF-8?q?ch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 run_once_basic 步骤"检查是否刚刚记录了预约,如果是则强制切换直播间" 独立块(约 47 行)抽到 fudai_detail_helpers.try_just_recorded_reservation_switch。 返回值约定: - True:刚记录预约,已尝试切换并写完成日志(调用方应直接 return) - False:未触发,调用方继续正常流程 douyin_fudai.py: 3022 → 2983 行 (-39) fudai_detail_helpers.py: 657 → 716 行 douyin_fudai.py 跌破 3000 行(4121 → 2983,本会话累计削减 1138 行) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 51 ++-------------- .../fudai_modules/fudai_detail_helpers.py | 59 +++++++++++++++++++ 2 files changed, 65 insertions(+), 45 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 03fdad8..dd0c536 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1483,51 +1483,12 @@ 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 + from douyin.core.fudai_modules.fudai_detail_helpers import ( + try_just_recorded_reservation_switch, + ) + + if try_just_recorded_reservation_switch(self, start_time): + return # 预约检查耗时记录 reservation_elapsed = time.time() - reservation_check_start diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 334e738..ecc8340 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -42,6 +42,65 @@ def log_screenshot_failed( pass +def try_just_recorded_reservation_switch(analyser, start_time: float) -> bool: + """run_once_basic 步骤:检查是否刚刚记录了预约,如果是则强制切换直播间。 + + 返回: + True:刚记录预约,已尝试切换并写完成日志(调用方应直接 return) + False:未触发,调用方继续正常流程 + """ + 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_coupon_popup(analyser, start_time: float) -> None: """run_once_basic stage == "COUPON_POPUP" 分支:按返回键关闭优惠券弹窗。""" analyser._log_kv("弹窗处理", "检测到优惠券弹窗,按返回键关闭")