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)