From 77d7113ca140ba2919068a052219230f5f6f9a0c Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 12:12:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20=5Fhandle=5Fin?= =?UTF-8?q?=5Ffudai=5Fdetail=20=E4=B8=AD"=E9=AB=98=E4=BB=B7=E5=80=BC?= =?UTF-8?q?=E8=B7=B3=E8=BF=87=E5=86=B3=E7=AD=96"=E5=9D=97=E5=88=B0=20check?= =?UTF-8?q?=5Fhigh=5Fvalue=5Fskip=5Fdecision?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将筛选不通过后的"高价值福袋决策"逻辑(约 91 行深嵌套)抽到 helper: - 检查是否高价值福袋(启用 + 价值达阈值 + 倒计时有效) - 倒计时大于阈值 + 剩余秒数在 join_ahead 内 → 返回 "attend"(取消 skip) - 倒计时大于阈值 + 剩余秒数 > join_ahead → 调用 handle_high_value_record_reservation 后返回 "recorded" - 其他:返回 "skip" 让调用方走筛选不通过分支 返回值约定: - "attend":调用方将 skip 设为 False 继续立即参与 - "recorded":已记录预约(调用方应直接 return) - "skip":保持 skip=True 走筛选不通过分支 douyin_fudai.py: 1804 → 1715 行 (-89) fudai_detail_helpers.py: 2083 → 2185 行 douyin_fudai.py 跌破 1800 行(4121 → 1715,本会话累计削减 2406 行 = -58%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 111 ++---------------- .../fudai_modules/fudai_detail_helpers.py | 102 ++++++++++++++++ 2 files changed, 113 insertions(+), 100 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 6983070..5c36272 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1300,107 +1300,18 @@ class fudai_analyse: ) if skip: # 检查是否是高价值福袋,如果是则记录预约或立即参与 - try: - # 提取价值信息 - prize_value_yuan = 0.0 - countdown_tuple = None - if isinstance(detail, tuple) and len(detail) >= 6: - # 使用解包避免索引告警 - _, _, _, _, prize_value_yuan, countdown_tuple = detail + from douyin.core.fudai_modules.fudai_detail_helpers import ( + check_high_value_skip_decision, + ) - # 检查是否启用高价值预约 - high_value_enabled = bool( - getattr(self, "high_value_fudai_enabled", False) - ) - high_value_threshold = float( - getattr(self, "high_value_fudai_threshold_yuan", 500.0) - or 500.0 - ) - - # 检查价值是否达到高价值阈值 - is_high_value = ( - high_value_enabled - and prize_value_yuan > 0 - and prize_value_yuan >= high_value_threshold - and isinstance(countdown_tuple, (tuple, list)) - and len(countdown_tuple) >= 2 - and float(countdown_tuple[1] or 0.0) > 0.0 - ) - - # 如果被倒计时过滤筛掉(倒计时大于阈值),但价值达到高价值阈值 - # 需要检查剩余秒数是否在"预约参与阈值"内,如果在阈值内则立即参与,否则记录预约 - if is_high_value and skip_reason.startswith( - "countdown_gt_" - ): - # 计算剩余秒数 - remain_seconds = None - if ( - isinstance(countdown_tuple, (tuple, list)) - and len(countdown_tuple) >= 2 - ): - try: - now = time.time() - cd_start = float(countdown_tuple[0] or 0.0) - cd_end = float(countdown_tuple[1] or 0.0) - if cd_start > 0 and cd_end > cd_start: - remain_seconds = float(cd_end - now) - except Exception: - pass - - # 获取"预约参与阈值"配置 - join_ahead_seconds = int( - getattr( - self, "high_value_fudai_join_ahead_seconds", 100 - ) - or 120 - ) - - # 如果剩余秒数在"预约参与阈值"内,立即参与而不是预约 - if ( - remain_seconds is not None - and remain_seconds <= join_ahead_seconds - ): - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:倒计时大于阈值,但剩余秒数在预约参与阈值内,立即参与", - reason=skip_reason, - value_yuan=prize_value_yuan, - threshold=high_value_threshold, - remain_seconds=int(remain_seconds), - join_ahead_threshold=join_ahead_seconds, - ) - except Exception: - pass - # 不关闭详情,继续走立即参与逻辑(保存详情数据,标记为已读取) - skip = False - else: - # 剩余秒数大于"预约参与阈值",记录预约 - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_high_value_record_reservation, - ) - - handle_high_value_record_reservation( - self, - detail, - skip_reason, - prize_value_yuan, - high_value_threshold, - remain_seconds, - join_ahead_seconds, - start_time, - ) - return - except Exception as e: - # 高价值检查异常,记录日志但继续流程 - try: - self._log_kv( - "流程", - "_handle_in_fudai_detail:高价值检查异常", - error=str(e), - ) - except Exception: - pass + decision = check_high_value_skip_decision( + self, detail, skip_reason, start_time + ) + if decision == "recorded": + return + elif decision == "attend": + # 不关闭详情,继续走立即参与逻辑(保存详情数据,标记为已读取) + skip = False # 如果不是高价值福袋,或者不是倒计时原因,筛选不通过,直接跳过 if skip: diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 1f9fc53..40f2efc 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -42,6 +42,108 @@ def log_screenshot_failed( pass +def check_high_value_skip_decision( + analyser, detail, skip_reason: str, start_time: float +): + """_handle_in_fudai_detail 步骤3:高价值福袋的过滤决策。 + + 被 should_skip_fudai_by_config 判定为 skip 后,再检查是否是高价值福袋: + - 倒计时大于阈值 + 价值达到高价值 + 剩余秒数在 join_ahead 内 → 立即参与(取消 skip) + - 倒计时大于阈值 + 价值达到高价值 + 剩余秒数 > join_ahead → 记录预约(直接 return) + - 其他:保持 skip=True 让调用方走"筛选不通过"分支 + + 返回: + - "attend":取消 skip,继续走立即参与逻辑(调用方应将 skip 设为 False) + - "recorded":已记录预约(调用方应直接 return) + - "skip":保持 skip=True,调用方继续走筛选不通过分支 + """ + try: + # 提取价值信息 + prize_value_yuan = 0.0 + countdown_tuple = None + if isinstance(detail, tuple) and len(detail) >= 6: + _, _, _, _, prize_value_yuan, countdown_tuple = detail + + # 检查是否启用高价值预约 + high_value_enabled = bool( + getattr(analyser, "high_value_fudai_enabled", False) + ) + high_value_threshold = float( + getattr(analyser, "high_value_fudai_threshold_yuan", 500.0) or 500.0 + ) + + # 检查价值是否达到高价值阈值 + is_high_value = ( + high_value_enabled + and prize_value_yuan > 0 + and prize_value_yuan >= high_value_threshold + and isinstance(countdown_tuple, (tuple, list)) + and len(countdown_tuple) >= 2 + and float(countdown_tuple[1] or 0.0) > 0.0 + ) + + if not (is_high_value and skip_reason.startswith("countdown_gt_")): + return "skip" + + # 计算剩余秒数 + remain_seconds = None + if ( + isinstance(countdown_tuple, (tuple, list)) + and len(countdown_tuple) >= 2 + ): + try: + now = time.time() + cd_start = float(countdown_tuple[0] or 0.0) + cd_end = float(countdown_tuple[1] or 0.0) + if cd_start > 0 and cd_end > cd_start: + remain_seconds = float(cd_end - now) + except Exception: + pass + + join_ahead_seconds = int( + getattr(analyser, "high_value_fudai_join_ahead_seconds", 100) or 120 + ) + + # 如果剩余秒数在"预约参与阈值"内,立即参与 + if remain_seconds is not None and remain_seconds <= join_ahead_seconds: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:倒计时大于阈值,但剩余秒数在预约参与阈值内,立即参与", + reason=skip_reason, + value_yuan=prize_value_yuan, + threshold=high_value_threshold, + remain_seconds=int(remain_seconds), + join_ahead_threshold=join_ahead_seconds, + ) + except Exception: + pass + return "attend" + + # 剩余秒数大于"预约参与阈值",记录预约 + handle_high_value_record_reservation( + analyser, + detail, + skip_reason, + prize_value_yuan, + high_value_threshold, + remain_seconds, + join_ahead_seconds, + start_time, + ) + return "recorded" + except Exception as e: + try: + analyser._log_kv( + "流程", + "_handle_in_fudai_detail:高价值检查异常", + error=str(e), + ) + except Exception: + pass + return "skip" + + def reread_fudai_detail_fallback(analyser): """_handle_in_fudai_detail 步骤1:未找到保存的详情数据时的兜底重读逻辑。