From 6cf3ce9a7278641bc9bfd84ab61bbc3e43a18a1c Mon Sep 17 00:00:00 2001 From: Codex Date: Fri, 22 May 2026 02:46:21 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai):=20=E6=8B=86=E5=87=BA=20IN=5FLI?= =?UTF-8?q?VE=5FROOM=20+=20RECOMMEND=5FPAGE=20stage=20=E5=A4=84=E7=90=86?= =?UTF-8?q?=EF=BC=882915=E2=86=922822=EF=BC=8C-93=20=E8=A1=8C=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 把 run_once_basic 中两个 stage 分支抽到 fudai_run_blocks: - handle_in_live_room_stage:等待福袋加载(如刚切换)+ 查找福袋 - handle_recommend_page_stage:点击直播页 + 进入直播间(连点两次) 新增 5 个单元测试覆盖:刚切换 / 未切换 / 点击失败回退 / 完成日志。 1114 → 1119 用例全过。 Co-Authored-By: Claude Opus 4.7 (1M context) --- douyin/core/douyin_fudai.py | 97 +------------------ douyin/core/fudai_modules/fudai_run_blocks.py | 96 ++++++++++++++++++ tests/test_fudai_run_blocks.py | 66 +++++++++++++ 3 files changed, 164 insertions(+), 95 deletions(-) diff --git a/douyin/core/douyin_fudai.py b/douyin/core/douyin_fudai.py index 5714578..409d637 100644 --- a/douyin/core/douyin_fudai.py +++ b/douyin/core/douyin_fudai.py @@ -1944,104 +1944,11 @@ class fudai_analyse: ) elif stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"): # 在直播间:按正常直播间流程处理(已移除预约回退直接参与标志) - try: - self._log_kv( - "预约", - "[预约] 检测到直播间,按正常直播间流程处理", - stage=stage, - ) - except Exception: - pass - - # 在直播间:如果刚刚切换了直播间,等待福袋加载 - just_switched = getattr(self, "_just_switched_room", False) - if just_switched: - try: - self._just_switched_room = False # 清除标志 - wait_seconds = float( - getattr(self, "switch_room_wait_after_stage_diag_seconds", 7.0) - or 7.0 - ) - if wait_seconds > 0: - self._log_kv( - "流程", - "切换直播间后,阶段检测完成,等待福袋加载", - wait_seconds=wait_seconds, - ) - self.operation.delay(wait_seconds) - except Exception as e: - try: - self._just_switched_room = False # 确保清除标志 - self._log_kv("流程", "等待福袋加载时出错", error=str(e)) - except Exception: - pass - - # 在直播间:寻找福袋图标并尝试点开 - fudai_search_start = time.time() - self._handle_live_room_has_or_not_fudai() - fudai_search_elapsed = time.time() - fudai_search_start - self._log_kv("福袋查找", f"福袋查找完成,耗时: {fudai_search_elapsed:.3f}s") - elapsed = time.time() - start_time - self._log_kv( - "流程", - "run_once_basic完成(处理直播间)", - elapsed_seconds=round(elapsed, 2), - ) + fudai_run_blocks.handle_in_live_room_stage(self, stage, start_time) elif stage in ("RECOMMEND_PAGE", "FOLLOW_LIST", "LIVE_LIST"): # 处理推荐/关注/直播列表页:回到直播间或切换 if stage == "RECOMMEND_PAGE": - # 识别到推荐页面:直接点击进入直播页,然后点击进入直播间 - self._log_kv( - "流程", - "检测到推荐页面,点击进入直播页并进入直播间", - stage=stage, - seen=seen, - ) - try: - # 直接点击 (718, 169) 进入直播页 - 基于1080x2400分辨率 - live_tab_x = int(718 * self.resolution_ratio_x // 1080) - live_tab_y = int(169 * self.resolution_ratio_y // 2400) - self.operation.click(self.device_id, live_tab_x, live_tab_y) - self._log_kv("流程", "点击进入直播页", x=live_tab_x, y=live_tab_y) - self.operation.delay(self.FOLLOW_TAB_CLICK_DELAY) - - # 点击进入直播间的位置 (103, 382),连点两次 - enter_live_x = int(103 * self.resolution_ratio_x // 1080) - enter_live_y = int(382 * self.resolution_ratio_y // 2400) - self.operation.click(self.device_id, enter_live_x, enter_live_y) - self.operation.delay(self.ENTER_LIVE_CLICK_DELAY) - # 小延迟,避免界面卡顿导致第二次点击未生效 - try: - self.operation.delay( - getattr(self, "RETURN_STEP_DELAY_SECONDS", 0.8) - ) - except Exception: - self.operation.delay(0.8) - self.operation.click(self.device_id, enter_live_x, enter_live_y) - self._log_kv( - "流程", - "点击进入直播间位置", - x=enter_live_x, - y=enter_live_y, - 次数=2, - ) - # 进入后等待(保留原有最终等待) - try: - # 小延迟,确保界面就绪(可配置) - self.operation.delay( - getattr(self, "RETURN_STEP_DELAY_SECONDS", 0.8) - ) - except Exception: - self.operation.delay(0.8) - self.operation.delay(self.ENTER_LIVE_FINAL_DELAY) - except Exception as e: - self._log_kv("流程", "处理推荐页面时出错", error=str(e)) - # 如果出错,尝试回退 - try: - self.operation.click_back(self.device_id) - self.operation.delay(1.0) - except Exception: - pass + fudai_run_blocks.handle_recommend_page_stage(self, stage, seen) else: # 其他列表页:使用原有逻辑 douyin_live_room.reflash_zhibo(self) diff --git a/douyin/core/fudai_modules/fudai_run_blocks.py b/douyin/core/fudai_modules/fudai_run_blocks.py index 1142faa..4ad6c37 100644 --- a/douyin/core/fudai_modules/fudai_run_blocks.py +++ b/douyin/core/fudai_modules/fudai_run_blocks.py @@ -653,3 +653,99 @@ def handle_live_closed_stage(analyser: Any, start_time: float) -> None: "run_once_basic完成(直播结束)", elapsed_seconds=round(elapsed, 2), ) + + +def handle_recommend_page_stage(analyser: Any, stage: str, seen: Any) -> None: + """stage == "RECOMMEND_PAGE":点击进入直播页 → 进入直播间(连点两次)。""" + analyser._log_kv( + "流程", + "检测到推荐页面,点击进入直播页并进入直播间", + stage=stage, + seen=seen, + ) + try: + # 直接点击 (718, 169) 进入直播页 - 基于1080x2400分辨率 + live_tab_x = int(718 * analyser.resolution_ratio_x // 1080) + live_tab_y = int(169 * analyser.resolution_ratio_y // 2400) + analyser.operation.click(analyser.device_id, live_tab_x, live_tab_y) + analyser._log_kv("流程", "点击进入直播页", x=live_tab_x, y=live_tab_y) + analyser.operation.delay(analyser.FOLLOW_TAB_CLICK_DELAY) + + # 点击进入直播间的位置 (103, 382),连点两次 + enter_live_x = int(103 * analyser.resolution_ratio_x // 1080) + enter_live_y = int(382 * analyser.resolution_ratio_y // 2400) + analyser.operation.click(analyser.device_id, enter_live_x, enter_live_y) + analyser.operation.delay(analyser.ENTER_LIVE_CLICK_DELAY) + try: + analyser.operation.delay( + getattr(analyser, "RETURN_STEP_DELAY_SECONDS", 0.8) + ) + except Exception: + analyser.operation.delay(0.8) + analyser.operation.click(analyser.device_id, enter_live_x, enter_live_y) + analyser._log_kv( + "流程", + "点击进入直播间位置", + x=enter_live_x, + y=enter_live_y, + 次数=2, + ) + try: + analyser.operation.delay( + getattr(analyser, "RETURN_STEP_DELAY_SECONDS", 0.8) + ) + except Exception: + analyser.operation.delay(0.8) + analyser.operation.delay(analyser.ENTER_LIVE_FINAL_DELAY) + except Exception as e: + analyser._log_kv("流程", "处理推荐页面时出错", error=str(e)) + try: + analyser.operation.click_back(analyser.device_id) + analyser.operation.delay(1.0) + except Exception: + pass + + +def handle_in_live_room_stage(analyser: Any, stage: str, start_time: float) -> None: + """stage in ("IN_LIVE_ROOM", "LIVE_OR_UNKNOWN"):检测到刚切换则等待加载,否则查找福袋。""" + try: + analyser._log_kv( + "预约", + "[预约] 检测到直播间,按正常直播间流程处理", + stage=stage, + ) + except Exception: + pass + + just_switched = getattr(analyser, "_just_switched_room", False) + if just_switched: + try: + analyser._just_switched_room = False + wait_seconds = float( + getattr(analyser, "switch_room_wait_after_stage_diag_seconds", 7.0) + or 7.0 + ) + if wait_seconds > 0: + analyser._log_kv( + "流程", + "切换直播间后,阶段检测完成,等待福袋加载", + wait_seconds=wait_seconds, + ) + analyser.operation.delay(wait_seconds) + except Exception as e: + try: + analyser._just_switched_room = False + analyser._log_kv("流程", "等待福袋加载时出错", error=str(e)) + except Exception: + pass + + fudai_search_start = time.time() + analyser._handle_live_room_has_or_not_fudai() + fudai_search_elapsed = time.time() - fudai_search_start + analyser._log_kv("福袋查找", f"福袋查找完成,耗时: {fudai_search_elapsed:.3f}s") + elapsed = time.time() - start_time + analyser._log_kv( + "流程", + "run_once_basic完成(处理直播间)", + elapsed_seconds=round(elapsed, 2), + ) diff --git a/tests/test_fudai_run_blocks.py b/tests/test_fudai_run_blocks.py index 091c921..102ab7c 100644 --- a/tests/test_fudai_run_blocks.py +++ b/tests/test_fudai_run_blocks.py @@ -318,3 +318,69 @@ def test_live_closed_swipes_to_next_and_logs_completion(): if len(c.args) >= 2 and "run_once_basic完成(直播结束)" in c.args[1] ] assert len(completion) == 1 + + +# ---- handle_recommend_page_stage ---- # + +def _make_recommend_analyser(): + a = _make_analyser() + a.resolution_ratio_x = 1080 + a.resolution_ratio_y = 2400 + a.FOLLOW_TAB_CLICK_DELAY = 0.5 + a.ENTER_LIVE_CLICK_DELAY = 0.6 + a.ENTER_LIVE_FINAL_DELAY = 1.0 + a.RETURN_STEP_DELAY_SECONDS = 0.8 + return a + + +def test_recommend_page_clicks_live_tab_and_enter_live_twice(): + a = _make_recommend_analyser() + fudai_run_blocks.handle_recommend_page_stage(a, "RECOMMEND_PAGE", "") + # 点击次数:直播页 1 + 直播间 2 = 3 + assert a.operation.click.call_count == 3 + + +def test_recommend_page_handles_click_failure(): + a = _make_recommend_analyser() + a.operation.click.side_effect = RuntimeError("boom") + fudai_run_blocks.handle_recommend_page_stage(a, "RECOMMEND_PAGE", "") + # 异常时 fallback 调用 click_back + a.operation.click_back.assert_called_once() + + +# ---- handle_in_live_room_stage ---- # + +def test_in_live_room_no_just_switched_calls_handler(): + a = _make_analyser() + a._just_switched_room = False + a._handle_live_room_has_or_not_fudai = MagicMock() + fudai_run_blocks.handle_in_live_room_stage(a, "IN_LIVE_ROOM", time.time()) + a._handle_live_room_has_or_not_fudai.assert_called_once() + # 不应该调用 delay(没切换不需要等加载) + a.operation.delay.assert_not_called() + + +def test_in_live_room_just_switched_waits_for_load(): + a = _make_analyser() + a._just_switched_room = True + a.switch_room_wait_after_stage_diag_seconds = 5.0 + a._handle_live_room_has_or_not_fudai = MagicMock() + fudai_run_blocks.handle_in_live_room_stage(a, "IN_LIVE_ROOM", time.time()) + # 应当 delay(5.0) + delays = [c.args[0] for c in a.operation.delay.call_args_list] + assert 5.0 in delays + # 标志已清除 + assert a._just_switched_room is False + a._handle_live_room_has_or_not_fudai.assert_called_once() + + +def test_in_live_room_logs_completion(): + a = _make_analyser() + a._just_switched_room = False + a._handle_live_room_has_or_not_fudai = MagicMock() + fudai_run_blocks.handle_in_live_room_stage(a, "LIVE_OR_UNKNOWN", time.time()) + completion = [ + c for c in a._log_kv.call_args_list + if len(c.args) >= 2 and "run_once_basic完成(处理直播间)" in c.args[1] + ] + assert len(completion) == 1