From 3a9f01ad74f47d5465f8f04e79b61882563f4726 Mon Sep 17 00:00:00 2001 From: Codex Date: Sat, 23 May 2026 03:07:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor(fudai=5Fmodules):=20=E6=8A=BD=20swipe?= =?UTF-8?q?=20=E5=85=9C=E5=BA=95=E9=87=8D=E5=A4=8D=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=88=B0=20fudai=5Fdevice=5Fcontrol=20(1238=20=E2=86=92=201147?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit handle_detail_helpers.py 中 _handle_error_status 内联段(L266-318,53 行) 和 _fallback_swipe_up 顶层函数(L1187-1239,53 行)做的是完全一样的事: 没有 swipe_up_to_next_live 时直接 ADB shell input swipe,失败再回退到 operation.swipe。重复 1:1。 新增 douyin/core/fudai_modules/fudai_device_control.py: - fallback_swipe_up_via_adb(self): 纯 ADB 兜底 swipe - swipe_up_or_fallback(self): 优先 swipe_up_to_next_live,否则 fallback handle_detail_helpers 两处都改成 4 行委托。原文件去掉 os/subprocess import,仅保留 time。 行为完全保留:分辨率换算公式、ADB 路径解析、subprocess 超时 5s、operation.swipe 最终兜底全部不变。 1361 passed. Co-Authored-By: Claude Opus 4.7 (1M context) --- .../fudai_modules/fudai_device_control.py | 91 +++++++++++++++ .../fudai_modules/handle_detail_helpers.py | 107 ++---------------- 2 files changed, 99 insertions(+), 99 deletions(-) create mode 100644 douyin/core/fudai_modules/fudai_device_control.py diff --git a/douyin/core/fudai_modules/fudai_device_control.py b/douyin/core/fudai_modules/fudai_device_control.py new file mode 100644 index 0000000..7408d09 --- /dev/null +++ b/douyin/core/fudai_modules/fudai_device_control.py @@ -0,0 +1,91 @@ +"""douyin/core/fudai_modules/fudai_device_control - 共享 ADB 设备操作 + +抽出 handle_detail_helpers 中重复 2 次的兜底 swipe 逻辑: +- _handle_error_status (L266-318) +- _fallback_swipe_up (L1187-1239) + +两处都做"没有 swipe_up_to_next_live 时直接 ADB shell input swipe,失败再回退到 +operation.swipe"。提到这里统一管理。 +""" +from __future__ import annotations + +import os +import subprocess +from typing import Any + + +def fallback_swipe_up_via_adb(self: Any) -> None: + """没有 swipe_up_to_next_live 时用 ADB shell input swipe 兜底切换直播间。 + + 流程: + 1. 按 self.resolution_ratio_x/y(默认 1080x2400)计算起止坐标 + 2. 优先调用 self.operation._adb_path() 获取 adb 路径 + 3. 直接 subprocess.run ADB shell input swipe + 4. 任何异常 → 回退到 self.operation.swipe(...) + + 主要用于 _handle_error_status 的"连续 task_count_zero≥3 强制切房"分支 + 和 _fallback_swipe_up 的"没有 swipe_up_to_next_live 时兜底"。 + """ + try: + rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080) + ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400) + except Exception: + rx, ry = 1080, 2400 + + start_x = 540 * rx // 1080 + start_y = 1200 * ry // 2400 + end_x = 540 * rx // 1080 + end_y = 600 * ry // 2400 + + try: + if hasattr(self.operation, "_adb_path"): + adb_cmd = self.operation._adb_path() + else: + adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb" + if adb_cmd.startswith('"') and adb_cmd.endswith('"'): + adb_cmd = adb_cmd[1:-1] + cmd = [ + adb_cmd, + "-s", + str(self.device_id), + "shell", + "input", + "swipe", + str(int(start_x)), + str(int(start_y)), + str(int(end_x)), + str(int(end_y)), + "300", + ] + subprocess.run( + cmd, + timeout=5.0, + stdout=subprocess.PIPE, + stderr=subprocess.PIPE, + check=False, + ) + except Exception: + # 回退到 operation.swipe + self.operation.swipe( + self.device_id, + start_x, + start_y, + end_x, + end_y, + 300, + ) + + +def swipe_up_or_fallback(self: Any) -> None: + """优先调 self.swipe_up_to_next_live(),否则走 fallback_swipe_up_via_adb 兜底。 + + 替代 handle_detail_helpers 中 L266-318 的内联兜底实现。 + """ + try: + if hasattr(self, "swipe_up_to_next_live"): + self.swipe_up_to_next_live() + return + except Exception: + pass + + fallback_swipe_up_via_adb(self) diff --git a/douyin/core/fudai_modules/handle_detail_helpers.py b/douyin/core/fudai_modules/handle_detail_helpers.py index c4ab837..b15e0bb 100644 --- a/douyin/core/fudai_modules/handle_detail_helpers.py +++ b/douyin/core/fudai_modules/handle_detail_helpers.py @@ -264,55 +264,10 @@ def _handle_error_status(self: "fudai_analyse", start_time: float) -> bool: needswitch = True if needswitch: try: - if hasattr(self, "swipe_up_to_next_live"): - self.swipe_up_to_next_live() - else: - rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080) - ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400) - start_x = 540 * rx // 1080 - start_y = 1200 * ry // 2400 - end_x = 540 * rx // 1080 - end_y = 600 * ry // 2400 - # 直接使用 ADB 内部滑动命令 - try: - if hasattr(self.operation, "_adb_path"): - adb_cmd = self.operation._adb_path() - else: - adb_cmd = ( - os.environ.get("ADB_EXECUTABLE", "adb") or "adb" - ) - if adb_cmd.startswith('"') and adb_cmd.endswith('"'): - adb_cmd = adb_cmd[1:-1] - cmd = [ - adb_cmd, - "-s", - str(self.device_id), - "shell", - "input", - "swipe", - str(int(start_x)), - str(int(start_y)), - str(int(end_x)), - str(int(end_y)), - "300", - ] - subprocess.run( - cmd, - timeout=5.0, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=False, - ) - except Exception: - # 回退到 operation.swipe - self.operation.swipe( - self.device_id, - start_x, - start_y, - end_x, - end_y, - 300, - ) + from douyin.core.fudai_modules.fudai_device_control import ( + swipe_up_or_fallback, + ) + swipe_up_or_fallback(self) self.operation.delay(0.5) except Exception: pass @@ -1186,53 +1141,7 @@ def _handle_skip_filter( def _fallback_swipe_up(self: "fudai_analyse") -> None: """没有 swipe_up_to_next_live 时的兜底 swipe(基于 1080x2400 基准坐标)。""" - import os - import subprocess - - try: - rx = int(getattr(self, "resolution_ratio_x", 1080) or 1080) - ry = int(getattr(self, "resolution_ratio_y", 2400) or 2400) - except Exception: - rx, ry = 1080, 2400 - start_x = 540 * rx // 1080 - start_y = 1200 * ry // 2400 - end_x = 540 * rx // 1080 - end_y = 600 * ry // 2400 - - try: - if hasattr(self.operation, "_adb_path"): - adb_cmd = self.operation._adb_path() - else: - adb_cmd = os.environ.get("ADB_EXECUTABLE", "adb") or "adb" - if adb_cmd.startswith('"') and adb_cmd.endswith('"'): - adb_cmd = adb_cmd[1:-1] - cmd = [ - adb_cmd, - "-s", - str(self.device_id), - "shell", - "input", - "swipe", - str(int(start_x)), - str(int(start_y)), - str(int(end_x)), - str(int(end_y)), - "300", - ] - subprocess.run( - cmd, - timeout=5.0, - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - check=False, - ) - except Exception: - # 回退到 operation.swipe - self.operation.swipe( - self.device_id, - start_x, - start_y, - end_x, - end_y, - 300, - ) + from douyin.core.fudai_modules.fudai_device_control import ( + fallback_swipe_up_via_adb, + ) + fallback_swipe_up_via_adb(self)