refactor(fudai_modules): 抽 swipe 兜底重复逻辑到 fudai_device_control (1238 → 1147)

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) <noreply@anthropic.com>
This commit is contained in:
CodexandClaude Opus 4.7 committed 2026-05-23 03:07:41 +08:00
1 parent 8ecd2783db
commit 3a9f01ad74
2 files changed
+99 -99

No files matched your search

@@ -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)
@@ -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)