From 2ba2168665c0a3a82fc42a8b68a3909778fb653d Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 12:33:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20=5Fhandle=5Flive?= =?UTF-8?q?=5Froom=5Fhas=5For=5Fnot=5Ffudai=20=E4=B8=AD=E4=B8=A4=E5=9D=97?= =?UTF-8?q?=E5=88=B0=20helper?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - try_skip_to_in_fudai_detail_when_verified(约 22 行):已验证过详情页则直接处理详情 - handle_no_fudai_in_live_room(约 35 行):无福袋时调用 flow_control 处理 + 写完成日志 douyin_fudai.py: 1404 → 1355 行 (-49) fudai_detail_helpers.py: 2659 → 2742 行 douyin_fudai.py 跌破 1400 行(4121 → 1355,本会话累计削减 2766 行 = -67%) _handle_live_room_has_or_not_fudai 从 90 行 → 41 行 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 73 +++------------- .../fudai_modules/fudai_detail_helpers.py | 83 +++++++++++++++++++ 2 files changed, 95 insertions(+), 61 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 0479c34..77e7dca 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1317,31 +1317,14 @@ class fudai_analyse: start_time = time.time() # 设置当前参与的福袋类型为高价值福袋(非预约) self._current_attending_reservation_fudai = False + # 先检查是否已经在福袋详情页(可能阶段诊断没有正确识别) - # 如果已验证过详情页但任务数为0,说明详情页正在加载,不应该再次查找福袋 - try: - already_verified = bool( - getattr(self, "_fudai_detail_verified", False) or False - ) - if already_verified: - try: - self._log_kv( - "流程", - "_handle_live_room_has_or_not_fudai:已验证过详情页,直接处理详情,不查找福袋", - ) - except Exception: - pass - # 直接调用详情处理逻辑,不查找福袋 - self._handle_in_fudai_detail() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_live_room_has_or_not_fudai完成(已验证详情页)", - elapsed_seconds=round(elapsed, 2), - ) - return - except Exception: - pass + from douyin.core.fudai_modules.fudai_detail_helpers import ( + try_skip_to_in_fudai_detail_when_verified, + ) + + if try_skip_to_in_fudai_detail_when_verified(self, start_time): + return # 检查是否在锁定期内,且时间窗口未到,避免重复打开福袋详情 from douyin.core.fudai_modules.fudai_detail_helpers import ( @@ -1354,44 +1337,12 @@ class fudai_analyse: room_pos_ref: List[int] = [0] opened = douyin_flow_control.find_and_open_fudai_detail(self, room_pos_ref) if not opened: - # 无福袋,交给 flow_control 的“无福袋处理”逻辑 - # 根据配置决定是否允许切换直播间(needswitch=False 时只等待不切房) - try: - need_switch_config = bool(getattr(self, "needswitch", True)) - except Exception: - need_switch_config = True + # 无福袋,交给 flow_control 的"无福袋处理"逻辑 + from douyin.core.fudai_modules.fudai_detail_helpers import ( + handle_no_fudai_in_live_room, + ) - wait_times_ref = [0] - swipe_times_ref = [0] - reservation_focus_until_ts_ref = [0.0] - reset_to_list_after_reservations_ref = [False] - douyin_flow_control.handle_no_fudai_situation( - self, - x=0, - swipe_times=0, - wait_times_ref=wait_times_ref, - needswitch=need_switch_config, - reservation_focus_until_ts=0.0, - reservations=[], - room_pos=room_pos_ref[0], - post_switch_delay_s=1.5, - swipe_up_switch_callback=self.swipe_up_to_next_live, - swipe_down_back_callback=lambda: None, - sort_reservations_callback=lambda: None, - clear_reservations_callback=lambda: None, - write_state_callback=lambda: None, - back_to_list_callback=lambda: None, - into_list_callback=lambda: None, - reservation_focus_until_ts_ref=reservation_focus_until_ts_ref, - reset_to_list_after_reservations_ref=reset_to_list_after_reservations_ref, - swipe_times_ref=swipe_times_ref, - ) - elapsed = time.time() - start_time - self._log_kv( - "流程", - "_handle_live_room_has_or_not_fudai完成(无福袋)", - elapsed_seconds=round(elapsed, 2), - ) + handle_no_fudai_in_live_room(self, room_pos_ref, start_time) return # 成功打开详情:调用同一套详情处理逻辑 diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index 116b84f..e10f525 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -42,6 +42,89 @@ def log_screenshot_failed( pass +def try_skip_to_in_fudai_detail_when_verified(analyser, start_time: float) -> bool: + """_handle_live_room_has_or_not_fudai 入口:如果已验证过详情页,直接处理详情。 + + 返回: + True:已转到详情处理(调用方应直接 return) + False:未验证或异常,调用方继续查找福袋 + """ + try: + already_verified = bool( + getattr(analyser, "_fudai_detail_verified", False) or False + ) + if not already_verified: + return False + + try: + analyser._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai:已验证过详情页,直接处理详情,不查找福袋", + ) + except Exception: + pass + + analyser._handle_in_fudai_detail() + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai完成(已验证详情页)", + elapsed_seconds=round(elapsed, 2), + ) + return True + except Exception: + pass + return False + + +def handle_no_fudai_in_live_room(analyser, room_pos_ref, start_time: float) -> None: + """_handle_live_room_has_or_not_fudai:无福袋时调用 flow_control 处理。 + + 流程: + - 按 needswitch 决定是否允许切换直播间 + - 调用 douyin_flow_control.handle_no_fudai_situation + - 写完成日志 + """ + from douyin.detection import douyin_flow_control + + try: + need_switch_config = bool(getattr(analyser, "needswitch", True)) + except Exception: + need_switch_config = True + + wait_times_ref = [0] + swipe_times_ref = [0] + reservation_focus_until_ts_ref = [0.0] + reset_to_list_after_reservations_ref = [False] + douyin_flow_control.handle_no_fudai_situation( + analyser, + x=0, + swipe_times=0, + wait_times_ref=wait_times_ref, + needswitch=need_switch_config, + reservation_focus_until_ts=0.0, + reservations=[], + room_pos=room_pos_ref[0], + post_switch_delay_s=1.5, + swipe_up_switch_callback=analyser.swipe_up_to_next_live, + swipe_down_back_callback=lambda: None, + sort_reservations_callback=lambda: None, + clear_reservations_callback=lambda: None, + write_state_callback=lambda: None, + back_to_list_callback=lambda: None, + into_list_callback=lambda: None, + reservation_focus_until_ts_ref=reservation_focus_until_ts_ref, + reset_to_list_after_reservations_ref=reset_to_list_after_reservations_ref, + swipe_times_ref=swipe_times_ref, + ) + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "_handle_live_room_has_or_not_fudai完成(无福袋)", + elapsed_seconds=round(elapsed, 2), + ) + + def record_room_title_if_not_just_switched(analyser) -> None: """run_once_basic 入口:尝试记录当前直播间的标题与位置。