From df8942e7b8d3835bbec34fa00da09e6ece3c3123 Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 13:43:35 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20=5Fhandle=5Fin?= =?UTF-8?q?=5Ffudai=5Fdetail=20=E6=AD=A5=E9=AA=A41/2=20=E5=88=86=E5=8F=91?= =?UTF-8?q?=E5=88=B0=20step1=5For=5Fstep2=5Fdispatcher?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将"is_second_open True/False 分发"块(约 44 行)整段抽到 helper: - True:检查保存的详情,有效则直接参与;无则 reread_fudai_detail_fallback 兜底 - False:调用 handle_first_open_step2 完整流程 返回 (detail, skip, skip_reason) 或 None(已早返回)。 douyin_fudai.py: 809 → 775 行 (-34) fudai_detail_helpers.py: 3174 → 3213 行 douyin_fudai.py 跌破 800 行(4121 → 775,本会话累计削减 3346 行 = -81%) _handle_in_fudai_detail 从 131 → 96 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 52 ++++--------------- .../fudai_modules/fudai_detail_helpers.py | 39 ++++++++++++++ 2 files changed, 48 insertions(+), 43 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index c7bee0b..0e8542d 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -655,50 +655,16 @@ class fudai_analyse: handle_consecutive_failures_exceeded(self, max_consecutive_failures) return - # 步骤1:检查是否是第二次打开福袋详情 - step1_start = time.time() - is_second_open = getattr(self, "_fudai_detail_first_read", False) - # 如果是第二次打开,直接点击参与,不需要验证,使用第一次的倒计时 - is_second_open = getattr(self, "_fudai_detail_first_read", False) - if is_second_open: - # 第二次打开:直接点击参与,不进行验证,使用第一次的倒计时 - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:第二次打开,直接点击参与,跳过验证和内容识别", - ) - except Exception: - pass + # 步骤1(第二次打开)vs 步骤2(第一次打开)的分发 + from douyin.core.fudai_modules.fudai_detail_helpers import ( + step1_or_step2_dispatcher, + ) - # 获取第一次读取的详情数据 - detail = getattr(self, "_last_fudai_detail_tuple", None) - if detail is None or not isinstance(detail, tuple) or len(detail) < 6: - # 如果没有保存的详情数据,需要重新读取(兜底逻辑) - from douyin.core.fudai_modules.fudai_detail_helpers import ( - reread_fudai_detail_fallback, - ) - - detail = reread_fudai_detail_fallback(self) - else: - # 使用第一次保存的详情数据:直接参与(不进行验证) - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_second_open_direct_attend, - ) - - if handle_second_open_direct_attend(self, start_time, step1_start): - return - # 异常时继续正常流程 - else: - # 步骤2:读取福袋详情 + 数据验证 + 筛选检查 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_first_open_step2, - ) - - step2_result = handle_first_open_step2(self, start_time) - if step2_result is None: - return - detail, skip, skip_reason = step2_result - step3_start = time.time() # 兼容后续 step3_elapsed 日志 + result = step1_or_step2_dispatcher(self, start_time) + if result is None: + return + detail, skip, skip_reason = result + step3_start = time.time() # 兼容后续 step3_elapsed 日志 # 检查是否是优惠券弹窗、观看任务或识别错误:根据 _last_fudai_detail_status 分发到对应处理 from douyin.core.fudai_modules.fudai_detail_helpers import ( diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 77d17cb..47e98c0 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -273,6 +273,45 @@ def handle_no_fudai_in_live_room(analyser, room_pos_ref, start_time: float) -> N ) +def step1_or_step2_dispatcher(analyser, start_time: float): + """步骤1(第二次打开)vs 步骤2(第一次打开)的分发。 + + - is_second_open=True:检查保存的详情,有效则直接参与;无则重新读取兜底 + - is_second_open=False:调用 handle_first_open_step2 完整流程 + + 返回: + - (detail, skip, skip_reason) 三元组:流程通过,调用方继续后续步骤 + - None:已早返回(参与成功 / 验证失败 / 筛选不通过 等) + """ + step1_start = time.time() + is_second_open = getattr(analyser, "_fudai_detail_first_read", False) + + if is_second_open: + # 第二次打开 + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:第二次打开,直接点击参与,跳过验证和内容识别", + ) + except Exception: + pass + + detail = getattr(analyser, "_last_fudai_detail_tuple", None) + if detail is None or not isinstance(detail, tuple) or len(detail) < 6: + # 未保存详情:兜底重读 + detail = reread_fudai_detail_fallback(analyser) + else: + # 使用保存的详情:直接参与 + if handle_second_open_direct_attend(analyser, start_time, step1_start): + return None + # 异常时继续正常流程 + # 第二次打开分支不返回 (skip, skip_reason);用空值占位 + return (detail, False, "") + else: + # 第一次打开:完整流程 + return handle_first_open_step2(analyser, start_time) + + def handle_first_open_step2(analyser, start_time: float): """步骤2:第一次打开福袋详情。