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