From 6d5b2cfb15ce560111a9cc9feda353c5b0e86e9f Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 13:27:19 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8A=BD=20run=5Fonce=5Fbas?= =?UTF-8?q?ic=20=E4=B8=AD"=E9=98=B6=E6=AE=B5=E5=88=86=E5=8F=91"=E5=9D=97?= =?UTF-8?q?=E5=88=B0=20dispatch=5Fstage?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将 100 行的 if/elif stage 分发链条整段抽到 fudai_detail_helpers.dispatch_stage: - COUPON_POPUP → handle_coupon_popup - SHOPPING_REDPACKET_POPUP → handle_shopping_redpacket_popup - FANSCLUB_JOIN → handle_fansclub_join - FUDai_DETAIL → analyser._handle_in_fudai_detail - IN_LIVE_ROOM/LIVE_OR_UNKNOWN → handle_in_live_room_stage - RECOMMEND_PAGE → handle_recommend_page - FOLLOW_LIST/LIVE_LIST → reflash_zhibo - FUDai_RESULT → handle_lottery_result_stage - LIVE_CLOSED → swipe_up_to_next_live - 其他 → 写"未匹配到特定阶段"日志 douyin_fudai.py: 1228 → 1132 行 (-96) fudai_detail_helpers.py: 2890 → 2975 行 run_once_basic 函数从 220 → 124 行,主体只剩入口逻辑 + 7 个 try_xxx 早返回 + 1 个 dispatch_stage。 douyin_fudai.py 跌破 1200 行(4121 → 1132,本会话累计削减 2989 行 = -73%) 验证: - pytest 1013 passed - compile OK Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 102 +----------------- .../fudai_modules/fudai_detail_helpers.py | 85 +++++++++++++++ 2 files changed, 88 insertions(+), 99 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 104911f..3b28ee4 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -797,106 +797,10 @@ class fudai_analyse: if try_high_value_room_revisit(self, start_time): return - # 处理弹窗阶段 - if stage == "COUPON_POPUP": - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_coupon_popup, - ) + # 阶段分发:根据 stage 调用对应的 helper + from douyin.core.fudai_modules.fudai_detail_helpers import dispatch_stage - handle_coupon_popup(self, start_time) - return - elif stage == "SHOPPING_REDPACKET_POPUP": - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_shopping_redpacket_popup, - ) - - handle_shopping_redpacket_popup(self, start_time) - return - elif stage == "FANSCLUB_JOIN": - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_fansclub_join, - ) - - handle_fansclub_join(self, start_time) - return - elif stage == "FUDai_DETAIL": - # 已经在详情页:直接按正常详情流程处理(已移除预约回退直接参与标志) - try: - self._log_kv( - "预约", - "[预约] 检测到福袋详情页,按正常详情流程处理", - stage=stage, - ) - except Exception: - pass - self._handle_in_fudai_detail() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理福袋详情)", - elapsed_seconds=round(elapsed, 2), - ) - elif stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): - # 在直播间:按正常直播间流程处理(已移除预约回退直接参与标志) - try: - self._log_kv( - "预约", - "[预约] 检测到直播间,按正常直播间流程处理", - stage=stage, - ) - except Exception: - pass - - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_in_live_room_stage, - ) - - handle_in_live_room_stage(self, start_time) - elif stage in ("RECOMMEND_PAGE", "FOLLOW_LIST", "LIVE_LIST"): - # 处理推荐/关注/直播列表页:回到直播间或切换 - if stage == "RECOMMEND_PAGE": - # 识别到推荐页面:直接点击进入直播页,然后点击进入直播间 - self._log_kv( - "流程", - "检测到推荐页面,点击进入直播页并进入直播间", - stage=stage, - seen=seen, - ) - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_recommend_page, - ) - - handle_recommend_page(self) - else: - # 其他列表页:使用原有逻辑 - douyin_live_room.reflash_zhibo(self) - elif stage == "FUDai_RESULT": - # 抽奖结果弹窗:处理开奖结果(使用OCR识别,不使用阶段诊断) - from douyin.core.fudai_modules.fudai_detail_helpers import ( - handle_lottery_result_stage, - ) - - handle_lottery_result_stage(self, start_time) - return - elif stage in ("LIVE_CLOSED",): - # 直播结束:下滑/切下一个 - self._log_kv("阶段", "当前直播已结束,尝试切换下一个") - self.swipe_up_to_next_live() - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(直播结束)", - elapsed_seconds=round(elapsed, 2), - ) - else: - # 未识别出特殊阶段:尽量保持原地,交给上层循环 - elapsed = time.time() - start_time - self._log_kv( - "阶段", - "未匹配到特定阶段", - stage=stage, - elapsed_seconds=round(elapsed, 2), - ) + dispatch_stage(self, stage, seen, start_time) def _check_lottery_result_by_ocr(self) -> Optional[str]: """通过OCR识别开奖结果按钮区域,返回识别到的文本类型。""" diff --git a/douyin/core/fudai_modules/fudai_detail_helpers.py b/douyin/core/fudai_modules/fudai_detail_helpers.py index fd0e136..5a07680 100644 --- a/douyin/core/fudai_modules/fudai_detail_helpers.py +++ b/douyin/core/fudai_modules/fudai_detail_helpers.py @@ -273,6 +273,91 @@ def handle_no_fudai_in_live_room(analyser, room_pos_ref, start_time: float) -> N ) +def dispatch_stage(analyser, stage: str, seen: str, start_time: float) -> None: + """run_once_basic 阶段分发器:根据 stage 调用对应的 helper。 + + 分发规则: + - COUPON_POPUP → handle_coupon_popup + - SHOPPING_REDPACKET_POPUP → handle_shopping_redpacket_popup + - FANSCLUB_JOIN → handle_fansclub_join + - FUDai_DETAIL → analyser._handle_in_fudai_detail + - IN_LIVE_ROOM / LIVE_OR_UNKNOWN → handle_in_live_room_stage + - RECOMMEND_PAGE → handle_recommend_page + - FOLLOW_LIST / LIVE_LIST → reflash_zhibo + - FUDai_RESULT → handle_lottery_result_stage + - LIVE_CLOSED → swipe_up_to_next_live + - 其他 → 写"未匹配到特定阶段"日志 + """ + if stage == "COUPON_POPUP": + handle_coupon_popup(analyser, start_time) + return + elif stage == "SHOPPING_REDPACKET_POPUP": + handle_shopping_redpacket_popup(analyser, start_time) + return + elif stage == "FANSCLUB_JOIN": + handle_fansclub_join(analyser, start_time) + return + elif stage == "FUDai_DETAIL": + try: + analyser._log_kv( + "预约", + "[预约] 检测到福袋详情页,按正常详情流程处理", + stage=stage, + ) + except Exception: + pass + analyser._handle_in_fudai_detail() + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理福袋详情)", + elapsed_seconds=round(elapsed, 2), + ) + elif stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): + try: + analyser._log_kv( + "预约", + "[预约] 检测到直播间,按正常直播间流程处理", + stage=stage, + ) + except Exception: + pass + handle_in_live_room_stage(analyser, start_time) + elif stage in ("RECOMMEND_PAGE", "FOLLOW_LIST", "LIVE_LIST"): + if stage == "RECOMMEND_PAGE": + analyser._log_kv( + "流程", + "检测到推荐页面,点击进入直播页并进入直播间", + stage=stage, + seen=seen, + ) + handle_recommend_page(analyser) + else: + from douyin.other import douyin_live_room + + douyin_live_room.reflash_zhibo(analyser) + elif stage == "FUDai_RESULT": + handle_lottery_result_stage(analyser, start_time) + return + elif stage in ("LIVE_CLOSED",): + analyser._log_kv("阶段", "当前直播已结束,尝试切换下一个") + analyser.swipe_up_to_next_live() + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(直播结束)", + elapsed_seconds=round(elapsed, 2), + ) + else: + elapsed = time.time() - start_time + analyser._log_kv( + "阶段", + "未匹配到特定阶段", + stage=stage, + elapsed_seconds=round(elapsed, 2), + ) + + def record_room_title_if_not_just_switched(analyser) -> None: """run_once_basic 入口:尝试记录当前直播间的标题与位置。