From d34e39ec9c29c70ef8267fb4aa513130f5153209 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 10:52:59 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20=5Fhandle=5Fin?= =?UTF-8?q?=5Ffudai=5Fdetail=20=E4=B8=AD"=E8=BF=9E=E7=BB=AD=E5=8F=82?= =?UTF-8?q?=E4=B8=8E=E5=A4=B1=E8=B4=A5=E6=AC=A1=E6=95=B0=E8=BF=87=E5=A4=9A?= =?UTF-8?q?"=E5=9D=97=E5=88=B0=20fudai=5Fdetail=5Fhelpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 49 行的"连续参与失败兜底"块抽到 fudai_modules/fudai_detail_helpers.py: handle_consecutive_failures_exceeded。逻辑: - 写日志 + 关闭详情页 - 按 needswitch 切换直播间或保持单间模式 - 重置 _consecutive_attend_failures 计数器 douyin_fudai.py: 3627 → 3588 行 (-39) 新增 fudai_detail_helpers.py: 63 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 49 ++------------- .../fudai_modules/fudai_detail_helpers.py | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 44 deletions(-) create mode 100644 douyin/core/fudai_modules/fudai_detail_helpers.py diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 2ae1fcc..eeaad1e 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -2278,50 +2278,11 @@ class fudai_analyse: consecutive_failures = getattr(self, "_consecutive_attend_failures", 0) max_consecutive_failures = 5 # 最多允许5次连续失败 if consecutive_failures >= max_consecutive_failures: - try: - self._log_kv( - "参与", - "_handle_in_fudai_detail:连续参与失败次数过多,跳过此福袋", - consecutive_failures=consecutive_failures, - max_allowed=max_consecutive_failures, - ) - except Exception: - pass - # 关闭详情页,避免卡死 - try: - douyin_fudai_detail.close_fudai_detail(self) - except Exception: - pass - # 连续失败过多时,按需切换直播间 - try: - needswitch = bool(getattr(self, "needswitch", True)) - except Exception: - needswitch = True - if needswitch: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:连续参与失败过多,切换直播间", - ) - except Exception: - pass - try: - self.swipe_up_to_next_live() - except Exception: - pass - else: - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:连续参与失败过多,单间模式不切换", - ) - except Exception: - pass - # 重置计数器,避免影响后续直播间 - try: - self._consecutive_attend_failures = 0 - except Exception: - pass + from douyin.core.fudai_modules.fudai_detail_helpers import ( + handle_consecutive_failures_exceeded, + ) + + handle_consecutive_failures_exceeded(self, max_consecutive_failures) return # 步骤1:检查是否是第二次打开福袋详情 diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py new file mode 100644 index 0000000..3585815 --- /dev/null +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -0,0 +1,63 @@ +"""_handle_in_fudai_detail 辅助函数 + +从 douyin_fudai.py:_handle_in_fudai_detail 中提取的相对独立的早返回块。 +""" +from __future__ import annotations + + +def handle_consecutive_failures_exceeded(analyser, max_consecutive_failures: int) -> None: + """连续参与失败次数过多:关闭详情页、按需切换直播间、重置计数器。 + + 调用方应在调用后立即 return(早返回)。 + """ + from douyin.fudai import douyin_fudai_detail + + consecutive_failures = getattr(analyser, "_consecutive_attend_failures", 0) + try: + analyser._log_kv( + "参与", + "_handle_in_fudai_detail:连续参与失败次数过多,跳过此福袋", + consecutive_failures=consecutive_failures, + max_allowed=max_consecutive_failures, + ) + except Exception: + pass + + # 关闭详情页,避免卡死 + try: + douyin_fudai_detail.close_fudai_detail(analyser) + except Exception: + pass + + # 连续失败过多时,按需切换直播间 + try: + needswitch = bool(getattr(analyser, "needswitch", True)) + except Exception: + needswitch = True + + if needswitch: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,切换直播间", + ) + except Exception: + pass + try: + analyser.swipe_up_to_next_live() + except Exception: + pass + else: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:连续参与失败过多,单间模式不切换", + ) + except Exception: + pass + + # 重置计数器,避免影响后续直播间 + try: + analyser._consecutive_attend_failures = 0 + except Exception: + pass