Files
douyin_guaji-main/douyin/core/underlying_operations.py
CodexandClaude Sonnet 4.5 703da834fb fix(security): 修复 shell 注入风险和类型转换未捕获问题
- path_config.py: find_executable Unix 回退路径使用 shlex.quote 防注入
- underlying_operations.py: try_set_ime IME ID 使用 shlex.quote 防注入
- screenshot_direct.py: 环境变量 float/int 转换添加 try/except 容错
- device_select.py: 用户输入 int() 转换添加 try/except 容错
- read_once.py / image_acquirer.py / detail_verifier.py: PIL Image 添加 close()
- detector.py: 移除 return 后不可达死代码
- optimized_entry.py: 统一返回值类型 int,失败时返回 0
- read_once.py: 移除 try/except 中重复的相同代码

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-05-23 20:50:26 +08:00

1333 lines
51 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import subprocess
import threading
from datetime import datetime, timedelta
import os
import sys
import warnings
# PaddleOCR 3.x 会在初始化/导入时进行模型源连通性检查;在国内网络环境下容易卡住。
# 这里提前设置环境变量,避免启动耗时/卡死。
os.environ.setdefault("DISABLE_MODEL_SOURCE_CHECK", "True")
# 额外抑制 paddle/oneDNN/glog 的冗余信息(INFO/WARNING)
os.environ.setdefault("GLOG_minloglevel", "2")
os.environ.setdefault("OMP_NUM_THREADS", "1")
# 抑制 PyTorch DataLoader 的 pin_memory 警告(当没有 GPU 时,EasyOCR 会触发此警告)
warnings.filterwarnings("ignore", message=".*pin_memory.*", category=UserWarning)
warnings.filterwarnings("ignore", message=".*accelerator.*", category=UserWarning)
import time as time_module
# alias for existing code that uses `time` name
time = time_module
from PIL import Image, ImageFilter # type: ignore[import]
from PIL import UnidentifiedImageError # type: ignore[import]
import numpy as np # type: ignore[import]
import logging
import re
import random
import xml.etree.ElementTree as ET
import shlex
import contextlib
import base64
from typing import Tuple, Optional, Any
# Pillow 版本兼容:使用 Image.Resampling 或回退到 Image 常量
try:
_RESAMPLE = Image.Resampling
except Exception:
_RESAMPLE = Image
# 外部完整版 fudai 逻辑所在的工作树路径,用于镜像 screenshot 给那边使用。
# 这些常量与文件 IO 辅助函数已抽离到 io_utils.py,此处仅为向后兼容重新导出。
from douyin.core.io_utils import (
_EXTERNAL_DOUYIN_FUDAI_DIR,
_EXTERNAL_AXF_SCREENSHOT,
_verify_png_integrity,
_safe_replace_file,
_ensure_parent_dir,
_mirror_screenshot_to_external,
)
# 模块级 logger:原代码用 print 输出 OCR 初始化 / 截图 / 滑动 / 调试日志,统一走 logging。
# 保留消息中已有的 [OCR] / [截图] / [Underlying] / [警告] / [错误] 前缀,避免改变历史搜索习惯。
# 设备选择交互(含 input())的 CLI 输出仍用 print,因为用户需要在 stdout 看到提示。
_LOGGER = logging.getLogger("douyin.core.underlying_operations")
def _uo_log(msg: str, level: str = "INFO") -> None:
"""统一底层操作日志出口;失败兜底 stdout。"""
try:
lv = str(level or "INFO").upper()
if lv == "ERROR":
_LOGGER.error(str(msg))
elif lv == "WARNING":
_LOGGER.warning(str(msg))
elif lv == "DEBUG":
_LOGGER.debug(str(msg))
else:
_LOGGER.info(str(msg))
except Exception:
try:
print(str(msg))
except Exception:
pass
# 已搬到子包 douyin.core.underlying_operations_pkg 的 6 个方法实现
from douyin.core.underlying_operations_pkg import (
_adb_path as _impl_adb_path,
_find_pic_path as _impl_find_pic_path,
_parse_bounds as _impl_parse_bounds,
_pick_adbkeyboard_ime as _impl_pick_adbkeyboard_ime,
check_countdown as _impl_check_countdown,
extract_ocr_content as _impl_extract_ocr_content,
_adb_args as _impl_adb_args,
save_reward_pic as _impl_save_reward_pic,
set_clipboard_text as _impl_set_clipboard_text,
_swipe_with_sendevent_improved as _impl_swipe_sendevent_improved,
select_device as _impl_select_device,
analyse_pic_digits as _impl_analyse_pic_digits,
)
class underlying_operations:
"""底层操作类"""
def __init__(self):
# 关闭文档方向/矫正等重型前处理:我们的截图是手机UI,不是扫描文档。
# 支持延迟初始化(测试流程/只做点击时不需要 OCR)。
self.ocr: Any = None
self.ocr_type = None # 'easyocr' 或 'paddleocr'
# 用于存储OCR区域的坐标信息(key为picname,value为(left, top, right, bottom))
self._ocr_coords_cache: dict[str, tuple[int, int, int, int]] = {}
# 线程安全锁
self._ocr_lock = threading.RLock() # 保护 OCR 初始化
self._ocr_coords_lock = threading.Lock() # 保护坐标缓存
# 默认使用CPU模式(删除GPU检测)
self._gpu_available = False
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
# 统一通过 operations.ocr_operations 的工厂函数初始化(避免与 OCRProcessor 重复定义)
# 优先 PaddleOCR v4-mobile(实测精度+速度均胜 EasyOCR),失败回退 EasyOCR
try:
from operations.ocr_operations import _build_paddle, _build_easyocr # noqa: WPS433
except Exception:
_build_paddle = None
_build_easyocr = None
try:
if _build_paddle is None:
raise ImportError("ocr_operations._build_paddle 不可用")
self.ocr = _build_paddle()
self.ocr_type = "paddleocr"
_uo_log("[OCR] 使用 PaddleOCR v4-mobile(CPU模式)")
except Exception as e:
_uo_log(f"[OCR] PaddleOCR 初始化失败: {e},回退到 EasyOCR")
try:
if _build_easyocr is None:
raise ImportError("ocr_operations._build_easyocr 不可用")
self.ocr = _build_easyocr()
self.ocr_type = "easyocr"
_uo_log("[OCR] 使用 EasyOCR(CPU模式,回退引擎)")
except Exception as e2:
_uo_log(f"[OCR] 所有 OCR 引擎初始化失败: {e2}")
self.ocr = None
self.ocr_type = None
# 注释掉全局日志禁用,以便查看详细的计时日志
# logging.disable(logging.DEBUG)
# logging.disable(logging.WARNING)
# 用于短期内记录 get_screenshot 调用来源(用于精确定位高频调用)
try:
import collections
self._screenshot_call_cache = collections.deque(maxlen=1000)
except Exception:
self._screenshot_call_cache = []
# 日志文件路径(JSON Lines 格式)
try:
from douyin.core import path_config
self._screenshot_log_path = os.path.join(
path_config.get_logs_dir(), "screenshot_calls.jsonl"
)
except Exception:
# path_config 不可用时退化到 dirname(保留旧行为兼容)
self._screenshot_log_path = os.path.join(
os.path.dirname(__file__), "logs", "screenshot_calls.jsonl"
)
@staticmethod
def _adb() -> str:
adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb")
# 始终加引号,避免路径带空格 / 版本混用
if adb.startswith('"') and adb.endswith('"'):
return adb
return f'"{adb}"'
@staticmethod
def _adb_path() -> str:
return _impl_adb_path()
def _adb_args(self, device_id: str, *args: object) -> list[str]:
return _impl_adb_args(device_id, *args)
@staticmethod
def _split_shell_cmd(shell_cmd: str) -> list[str]:
return shlex.split(str(shell_cmd or ""), posix=(os.name != "nt"))
def _ensure_ocr(self):
"""OCR 引擎延迟初始化(与 __init__ 同优先级:Paddle 优先,EasyOCR 兜底)
加锁防止多线程并发触发重复初始化(main 在 ff8de2c 修复 #7 并发安全问题时引入 _ocr_lock)。
"""
if self.ocr is not None:
return
with self._ocr_lock:
# 双检:拿到锁后再确认一次
if self.ocr is not None:
return
try:
from operations.ocr_operations import _build_paddle, _build_easyocr # noqa: WPS433
except Exception:
_build_paddle = None
_build_easyocr = None
try:
if _build_paddle is None:
raise ImportError("ocr_operations._build_paddle 不可用")
self.ocr = _build_paddle()
self.ocr_type = "paddleocr"
_uo_log("[OCR] 延迟初始化:使用 PaddleOCR v4-mobile(CPU模式)")
return
except Exception as e:
_uo_log(f"[OCR] 延迟初始化 PaddleOCR 失败: {e},回退 EasyOCR")
try:
if _build_easyocr is None:
raise ImportError("ocr_operations._build_easyocr 不可用")
self.ocr = _build_easyocr()
self.ocr_type = "easyocr"
_uo_log("[OCR] 延迟初始化:使用 EasyOCR(CPU模式,回退)")
except Exception as e:
_uo_log(f"[OCR] 延迟初始化失败: {e}")
self.ocr = None
self.ocr_type = None
def select_device(self):
return _impl_select_device()
def _find_pic_path(self, picname: str, for_reading: bool = True):
return _impl_find_pic_path(picname, for_reading)
def get_screenshot(self, device_id, path="pic", use_service: bool = False):
"""截图(支持后台服务模式)
参数:
device_id: 设备ID
path: 路径(默认"pic")
use_service: 是否使用后台截图服务(默认False,强制使用直接截图)
返回:
是否成功获取截图
"""
start_ts = time_module.time()
# Diagnostic: record pre-call info including caller stack (top frames)
try:
import inspect, json, threading
frames = []
for fr in inspect.stack()[1:9]:
frame_info = {
"file": os.path.relpath(fr.filename, os.path.dirname(__file__)),
"line": fr.lineno,
"function": fr.function,
}
frames.append(frame_info)
pre_record = {
"ts": datetime.now().isoformat(),
"device_id": device_id,
"use_service": bool(use_service),
"path": path,
"phase": "start",
"thread": threading.get_ident(),
"stack": frames,
}
# cache in memory
try:
if hasattr(self, "_screenshot_call_cache"):
try:
self._screenshot_call_cache.append(pre_record)
except Exception:
pass
except Exception:
pass
# append JSON line to log file
try:
log_dir = os.path.join(os.path.dirname(__file__), "logs")
os.makedirs(log_dir, exist_ok=True)
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
_lf.write(json.dumps(pre_record, ensure_ascii=False) + "\n")
except Exception:
pass
except Exception:
pass
service_success = False
# 如果启用服务模式,使用后台截图服务;若服务不可用或返回 False,则回退到直接截图
if use_service:
try:
from operations.screenshot_service import ScreenshotService
service = ScreenshotService.get_instance()
# 诊断:检查服务是否在运行
service_running = getattr(service, "_running", False)
if not service_running:
logging.debug(
"[截图] 后台服务未运行,尝试启动 device=%s", device_id
)
try:
service.start(device_id, interval=1.5)
# 等待一小段时间让服务完成第一次截图
time_module.sleep(0.2)
except Exception as start_err:
logging.warning(
"[截图] 启动后台服务失败: %s,将使用直接截图", start_err
)
try:
if service.get_screenshot(device_id, force=False):
dur = (time_module.time() - start_ts) * 1000.0
logging.info(
"[截图] 使用后台服务耗时(ms)=%.1f device=%s", dur, device_id
)
# 写结束记录
try:
import json, threading
end_record = {
"ts": datetime.now().isoformat(),
"device_id": device_id,
"use_service": True,
"path": path,
"phase": "end",
"result": True,
"duration_ms": dur,
"thread": threading.get_ident(),
}
try:
if hasattr(self, "_screenshot_call_cache"):
self._screenshot_call_cache.append(end_record)
except Exception:
pass
try:
with open(
self._screenshot_log_path, "a", encoding="utf-8"
) as _lf:
_lf.write(
json.dumps(end_record, ensure_ascii=False)
+ "\n"
)
except Exception:
pass
except Exception:
pass
# do not return here; let final return at function end
service_success = True
else:
timetag = datetime.now().strftime("%H:%M:%S")
logging.info(
"[%s] [截图操作] 后台服务未提供有效截图,回退到直接截图",
timetag,
)
# 不 return,这里继续走直接截图逻辑
except Exception as e:
timetag = datetime.now().strftime("%H:%M:%S")
logging.warning(
"[%s] [截图操作] 调用后台服务异常,回退到直接截图: %s",
timetag,
e,
)
service_success = False
except ImportError as e:
timetag = datetime.now().strftime("%H:%M:%S")
logging.warning(
"[%s] [截图操作] 无法导入截图服务模块,回退到直接截图: %s",
timetag,
e,
)
service_success = False
except Exception as e:
timetag = datetime.now().strftime("%H:%M:%S")
logging.warning(
"[%s] [截图操作] 服务模式不可用,回退到直接截图: %s", timetag, e
)
service_success = False
# 原有的直接截图逻辑(保持向后兼容),测量其耗时
try:
# If result already set by service branch, skip direct call
if service_success:
final_result = True
else:
final_result = self._get_screenshot_direct(device_id, path)
except Exception:
final_result = False
dur = (time_module.time() - start_ts) * 1000.0
logging.info(
"[截图] 总耗时(ms)=%.1f use_service=%s result=%s",
dur,
bool(use_service),
final_result,
)
# 写结束记录(包含持续时长与结果)
try:
import json, threading
end_record = {
"ts": datetime.now().isoformat(),
"device_id": device_id,
"use_service": bool(use_service),
"path": path,
"phase": "end",
"result": bool(final_result),
"duration_ms": dur,
"thread": threading.get_ident(),
}
try:
if hasattr(self, "_screenshot_call_cache"):
self._screenshot_call_cache.append(end_record)
except Exception:
pass
try:
with open(self._screenshot_log_path, "a", encoding="utf-8") as _lf:
_lf.write(json.dumps(end_record, ensure_ascii=False) + "\n")
except Exception:
pass
except Exception:
pass
return bool(final_result)
def _get_screenshot_direct(self, device_id, path="pic"):
"""直接截图(不使用服务模式)。
实现已抽到 underlying_operations_pkg.screenshot_direct.capture_screenshot_direct。
"""
from douyin.core.underlying_operations_pkg.screenshot_direct import (
capture_screenshot_direct,
)
return capture_screenshot_direct(self, device_id, path)
def get_current_hour(self):
"""获取当前的时间小时数"""
time_hour = datetime.now().strftime("%H")
_uo_log("当前时间{}点".format(time_hour))
return int(time_hour)
def click_confirm(self, device_id):
"""点击确认键"""
subprocess.run(
self._adb_args(device_id, "shell", "input", "keyevent", "66"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
_uo_log("点击确认键")
def click_back(self, device_id):
"""点击返回键"""
subprocess.run(
self._adb_args(device_id, "shell", "input", "keyevent", "4"),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
_uo_log("点击返回键")
# 电量检测方法已删除
def get_device_resolution(self, device_id):
"""获取设备分辨率"""
resolution_info = subprocess.run(
self._adb_args(device_id, "shell", "wm", "size"),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
encoding="utf-8",
errors="replace",
check=False,
)
resolution_info_string = resolution_info.stdout or ""
match = re.search(r"Physical size: (\d+)x(\d+)", resolution_info_string)
if match:
width = int(match.group(1))
height = int(match.group(2))
_uo_log("设备分辨率为:{}*{}".format(width, height))
return width, height
else:
raise ValueError("Could not find resolution in adb output")
def save_reward_pic(self, device_id):
return _impl_save_reward_pic(device_id)
def cut_pic(self, left_up=(0, 63), right_down=(1080, 1620), target="", name=""):
"""裁剪图片,获取需要的区域小图片方便识别。"""
from douyin.core.underlying_operations_pkg.image_ops import (
cut_pic as _impl,
)
return _impl(self, left_up, right_down, target, name)
def analyse_pic_word(
self, picname, change_color=0, pic_path=None, return_confidence=False
):
"""对指定图片运行 OCR 并返回拼接后的文本结果。
参数:
picname: 用于分类和调试的图片名(如"福袋内容")
change_color: 颜色处理模式,0=保留彩色(推荐),1/2=二值化等
pic_path: 可选的图片路径,未提供时通过 `_find_pic_path` 查找
return_confidence: 是否返回置信度信息(默认False,保持向后兼容)
"""
from douyin.core.underlying_operations_pkg.ocr_pipeline import (
analyse_pic_word as _impl,
)
return _impl(self, picname, change_color, pic_path, return_confidence)
def analyse_pic_digits(self, picname, change_color=1, pic_path=None):
return _impl_analyse_pic_digits(self, picname, change_color, pic_path)
def extract_ocr_content(self, content=None, return_confidence: bool = False):
return _impl_extract_ocr_content(content, return_confidence)
def delay(self, seconds: float = 1):
time_module.sleep(seconds)
def random_delay(self, min_seconds=1, max_seconds=100):
"""等待随机的时长"""
random_wait_seconds = random.randint(min_seconds, max_seconds)
_uo_log("随机等待{}秒.".format(random_wait_seconds))
self.delay(random_wait_seconds)
def check_countdown(self, last_time: str = "") -> tuple:
return _impl_check_countdown(last_time)
def swipe(
self,
device_id,
left_up_x=0,
left_up_y=0,
right_down_x=1080,
right_down_y=1500,
steps=300,
):
"""划动屏幕
参数说明:
- left_up_x, left_up_y: 滑动起始位置(手指按下位置)
- right_down_x, right_down_y: 滑动结束位置(手指抬起位置)
- steps: 滑动持续时间(毫秒),数值越大滑动越慢,默认300ms(已优化,比之前的200ms更快)
执行命令:adb shell input swipe <start_x> <start_y> <end_x> <end_y> <duration_ms>
"""
# 注意:adb input swipe 的参数顺序是:start_x start_y end_x end_y duration_ms
# 这里 left_up_x/y 是起始位置,right_down_x/y 是结束位置
cmd = self._adb_args(
device_id,
"shell",
"input",
"swipe",
int(left_up_x),
int(left_up_y),
int(right_down_x),
int(right_down_y),
int(steps),
)
try:
# 使用 subprocess 执行命令,捕获错误输出
result = subprocess.run(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=5.0,
text=True,
encoding="utf-8",
errors="replace",
check=False,
)
# 检查是否有权限错误
stderr_text = result.stderr if result.stderr else ""
has_permission_error = result.returncode != 0 and (
"SecurityException" in stderr_text
or "INJECT_EVENTS" in stderr_text
or "Injecting to another application" in stderr_text
)
if has_permission_error:
# 尝试使用 uiautomator(如果可用,某些设备可能支持)
try:
self._swipe_with_uiautomator(
device_id, left_up_x, left_up_y, right_down_x, right_down_y
)
return
except Exception:
pass
# 记录错误但不抛出异常,避免中断主流程
error_msg = stderr_text.strip() if stderr_text else "权限不足"
_uo_log(f"[警告] 滑动操作失败: {error_msg}")
_uo_log("[提示] 设备不允许 ADB 输入注入,可能的解决方案:")
_uo_log(
" 1. 在设备开发者选项中启用'USB调试(安全设置)'或'允许模拟点击'"
)
_uo_log(" 2. 使用已 root 的设备")
_uo_log(" 3. 某些设备需要在设置中手动授权 ADB 输入权限")
# 不抛出异常,让程序继续运行(可能只是滑动失败,其他功能仍可用)
except subprocess.TimeoutExpired:
_uo_log("[警告] 滑动操作超时")
except Exception as e:
# 降级重试一次,保持旧逻辑的宽容行为。
try:
subprocess.run(
cmd,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
except Exception:
_uo_log(f"[警告] 滑动操作失败: {type(e).__name__}: {e}")
def _swipe_with_sendevent(self, device_id, x1, y1, x2, y2, steps):
"""使用 sendevent 方法滑动(需要 root 权限)"""
# 获取触摸设备路径
result = subprocess.run(
self._adb_args(device_id, "shell", "getevent", "-p"),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
text=True,
encoding="utf-8",
errors="replace",
check=False,
)
if "ABS_MT" not in result.stdout or "event" not in result.stdout.lower():
raise FileNotFoundError("无法找到触摸设备")
# 简化实现:直接使用 input 命令的替代参数
# 注意:sendevent 需要 root 权限,这里只是尝试
raise PermissionError("sendevent 需要 root 权限,暂不支持")
def _swipe_with_uiautomator(self, device_id, x1, y1, x2, y2):
"""使用 uiautomator 滑动(如果可用)
注意:uiautomator 在某些设备上可能也需要权限,但可以尝试作为备选方案。
"""
# 使用 uiautomator swipe 命令
# 注意:uiautomator 命令格式可能因 Android 版本而异
# Android 5.0+: uiautomator swipe <x1> <y1> <x2> <y2> [steps]
result = subprocess.run(
self._adb_args(
device_id, "shell", "uiautomator", "swipe", int(x1), int(y1), int(x2), int(y2)
),
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
timeout=5.0,
check=False,
)
if result.returncode != 0:
raise RuntimeError(
f"uiautomator swipe 失败: {result.stderr.decode('utf-8', errors='ignore')}"
)
def _swipe_with_sendevent_improved(self, device_id, x1, y1, x2, y2, steps=50):
return _impl_swipe_sendevent_improved(device_id, x1, y1, x2, y2, steps)
def click(self, device_id, x=500, y=500, log: bool = True):
"""点击坐标位置
参数:
device_id: 设备ID
x, y: 屏幕坐标
log: 是否打印底层日志(默认为 True)。在需要安静点击的场景(如养号)传 False。
"""
try:
if log:
_uo_log(f"[Underlying] click: device_id={device_id} coord=({x},{y})")
except Exception:
pass
subprocess.run(
self._adb_args(device_id, "shell", "input", "tap", int(x), int(y)),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
def long_press(
self, device_id: str, x: int, y: int, duration_ms: int = 900
) -> None:
"""长按某个坐标(通过 swipe x y x y duration 模拟)。"""
d = int(duration_ms or 900)
if d < 300:
d = 300
if d > 2000:
d = 2000
subprocess.run(
self._adb_args(
device_id, "shell", "input", "swipe", int(x), int(y), int(x), int(y), d
),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
def _run_adb_shell(
self, device_id: str, shell_cmd: str, timeout_seconds: float = 3.0
) -> bool:
"""运行 adb shell 命令,成功返回 True。"""
try:
r = subprocess.run(
self._adb_args(device_id, "shell", *self._split_shell_cmd(shell_cmd)),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=timeout_seconds,
check=False,
)
return r.returncode == 0
except Exception:
return False
def _run_adb_shell_capture(
self, device_id: str, shell_cmd: str, timeout_seconds: float = 3.0
) -> Tuple[bool, str]:
"""运行 adb shell 命令并捕获输出,返回 (ok, stdout)。"""
try:
r = subprocess.run(
self._adb_args(device_id, "shell", *self._split_shell_cmd(shell_cmd)),
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
timeout=timeout_seconds,
text=True,
encoding="utf-8",
errors="ignore",
check=False,
)
return (r.returncode == 0), (r.stdout or "")
except Exception:
return False, ""
def get_top_package(self, device_id: str) -> str:
"""尝试返回当前处于前台的包名(如 com.ss.android.ugc.aweme)。
失败返回空字符串。实现采用 dumpsys 输出的多种字段进行解析,兼容不同 Android 版本。
"""
try:
ok, out = self._run_adb_shell_capture(
device_id, "dumpsys window windows", timeout_seconds=4.0
)
if ok and out:
for ln in (out or "").splitlines():
if (
"mCurrentFocus" in ln
or "mFocusedApp" in ln
or "mFocusedWindow" in ln
):
m = re.search(r"([A-Za-z0-9_\.]+)\/", ln)
if m:
return m.group(1)
# 兜底尝试:activity 栈里的 resumed activity
ok2, out2 = self._run_adb_shell_capture(
device_id, "dumpsys activity activities", timeout_seconds=4.0
)
if ok2 and out2:
for ln in (out2 or "").splitlines():
if "mResumedActivity" in ln or "ResumedActivity" in ln:
m = re.search(r"([A-Za-z0-9_\.]+)\/", ln)
if m:
return m.group(1)
except Exception:
pass
return ""
# 抖音常见包名(从主到 lite/极速版/海外版),launch_package 默认按此顺序尝试
DOUYIN_PACKAGES = (
"com.ss.android.ugc.aweme", # 抖音正式版
"com.ss.android.ugc.aweme.lite", # 抖音极速版
"com.zhiliaoapp.musically", # TikTok(兼容)
)
def launch_package(
self,
device_id: str,
package_name: str = "",
timeout_seconds: float = 5.0,
) -> Tuple[bool, str]:
"""通过包名启动应用。
策略:先用 `monkey -p <pkg> -c android.intent.category.LAUNCHER 1` 启动主 launcher activity,
失败时回退到 `am start -n <pkg>/.<MainActivity>`(按抖音常用 activity 列表尝试)。
参数:
device_id: ADB 设备 ID
package_name: 要启动的包名;留空则按 DOUYIN_PACKAGES 顺序尝试
timeout_seconds: 单次 adb shell 命令超时
返回:
(success, used_package) — used_package 为实际成功启动的包名(失败返回 "")
"""
candidates = (
[package_name] if package_name else list(self.DOUYIN_PACKAGES)
)
for pkg in candidates:
pkg = (pkg or "").strip()
if not pkg:
continue
# 1) monkey 主入口
cmd = f"monkey -p {pkg} -c android.intent.category.LAUNCHER 1"
ok, out = self._run_adb_shell_capture(
device_id, cmd, timeout_seconds=timeout_seconds
)
# monkey 失败时输出会包含 "No activities found" 或 "monkey aborted"
if ok and "No activities found" not in (out or "") and "aborted" not in (
out or ""
).lower():
return True, pkg
# 2) am start 主入口兜底
for activity in (
".main.MainActivity",
".splash.SplashActivity",
".main.homepage.MainActivity",
):
am_cmd = f"am start -n {pkg}/{activity}"
if self._run_adb_shell(
device_id, am_cmd, timeout_seconds=timeout_seconds
):
return True, pkg
return False, ""
def is_douyin_foreground(self, device_id: str) -> bool:
"""便捷判断:当前前台是否是抖音相关包。"""
top_pkg = self.get_top_package(device_id) or ""
return bool(top_pkg) and (
"aweme" in top_pkg
or "douyin" in top_pkg
or "com.ss.android.ugc" in top_pkg
)
def set_clipboard_text(self, device_id: str, text: str) -> bool:
return _impl_set_clipboard_text(device_id, text)
def paste_from_clipboard(self, device_id: str) -> bool:
"""尝试触发“粘贴”操作(需要输入框已获得焦点)。"""
# KEYCODE_PASTE=279(部分机型/系统可能不支持,但无副作用)
return self._run_adb_shell(device_id, "input keyevent 279", timeout_seconds=2.0)
def input_text(self, device_id: str, text: str) -> bool:
"""使用 adb input text 输入(对中文可能不稳定,仅兜底)。"""
t = str(text or "")
if not t:
return False
# 空格用 %s,避免被 shell 分词
t = t.replace(" ", "%s")
# 完整转义所有 shell 特殊字符
for ch in ['&', '<', '>', '|', ';', '$', '`', "'", '"', '\\', '(', ')', '{', '}', '[', ']', '!', '#', '*', '?']:
t = t.replace(ch, '\\' + ch)
try:
return (
subprocess.run(
self._adb_args(device_id, "shell", "input", "text", t),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=5.0,
check=False,
).returncode
== 0
)
except Exception:
return False
def paste_text(self, device_id: str, text: str) -> bool:
"""优先走剪贴板粘贴,其次兜底 input text。"""
if self.set_clipboard_text(device_id, text):
self.paste_from_clipboard(device_id)
return True
return self.input_text(device_id, text)
# ===== ADBKeyboard (可选) =====
def get_current_ime(self, device_id: str) -> str:
ok, out = self._run_adb_shell_capture(
device_id, "settings get secure default_input_method", timeout_seconds=2.0
)
if not ok:
return ""
return (out or "").strip()
def list_imes(self, device_id: str) -> list[str]:
# 部分 ROM 会限制 `ime` shell 命令(报 WRITE_SECURE_SETTINGS),这里优先用 settings 查询。
ok, out = self._run_adb_shell_capture(
device_id, "settings get secure enabled_input_methods", timeout_seconds=2.5
)
if ok and out:
raw = (out or "").strip()
# 形如:a/.A:b/.B:c/.C
parts = [p.strip() for p in raw.split(":") if p.strip()]
if parts:
return parts
ok2, out2 = self._run_adb_shell_capture(
device_id, "ime list -s", timeout_seconds=2.5
)
if not ok2:
# 最后兜底:至少把当前默认输入法返回,避免上层无法判断
cur = self.get_current_ime(device_id)
return [cur] if cur else []
lines = [ln.strip() for ln in (out2 or "").splitlines() if ln.strip()]
return lines
@staticmethod # type: ignore[misc]
def _pick_adbkeyboard_ime(imes: list[str]) -> str:
return _impl_pick_adbkeyboard_ime(imes)
def ensure_adbkeyboard_ime(
self, device_id: str, preferred_ime: str = "com.android.adbkeyboard/.AdbIME"
) -> Tuple[bool, str, str]:
"""确保 ADBKeyboard 输入法处于激活状态。
返回 (ok, prev_ime, adb_ime)。ok=False 表示设备未安装/未启用 ADBKeyboard。
"""
prev = self.get_current_ime(device_id)
# 如果当前已经是 ADBKeyboard,就不用再尝试切换(很多 ROM 禁止 ime set)
if "adbkeyboard" in str(prev).lower() or "adbime" in str(prev).lower():
return True, prev, prev
imes = self.list_imes(device_id)
adb_ime = ""
pref = str(preferred_ime or "").strip()
if pref and pref in imes:
adb_ime = pref
else:
adb_ime = self._pick_adbkeyboard_ime(imes)
if not adb_ime:
return False, prev, ""
if prev != adb_ime:
if not self.try_set_ime(device_id, adb_ime):
return False, prev, adb_ime
return True, prev, adb_ime
def restore_ime(self, device_id: str, prev_ime: str) -> bool:
ime = str(prev_ime or "").strip()
if not ime:
return False
return self.try_set_ime(device_id, ime)
def try_set_ime(self, device_id: str, ime_id: str) -> bool:
ime = str(ime_id or "").strip()
if not ime:
return False
# 1) 优先走 ime(某些 ROM 会失败)
self._run_adb_shell(device_id, f"ime enable {shlex.quote(ime)}", timeout_seconds=2.5)
if self._run_adb_shell(device_id, f"ime set {shlex.quote(ime)}", timeout_seconds=2.5):
return True
# 2) 退化:尝试 settings put(通常也会被 ROM 限制,但试一下无害)
if self._run_adb_shell(
device_id,
f"settings put secure default_input_method {shlex.quote(ime)}",
timeout_seconds=2.5,
):
cur = self.get_current_ime(device_id)
return cur == ime
return False
def _adbkeyboard_broadcast(self, device_id: str, action: str, msg: str) -> bool:
act = str(action or "").strip()
if not act:
return False
# 使用 shlex.quote 转义消息,防止 shell 注入
safe_msg = shlex.quote(str(msg or ""))
try:
return (
subprocess.run(
self._adb_args(
device_id, "shell", "am", "broadcast", "-a", act, "--es", "msg", safe_msg
),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=3.0,
check=False,
).returncode
== 0
)
except Exception:
return False
def _adbkeyboard_broadcast_int(
self, device_id: str, action: str, key: str, value: int
) -> bool:
act = str(action or "").strip()
k = str(key or "").strip()
if not act or not k:
return False
try:
v = int(value)
except Exception:
return False
# 使用 shlex.quote 转义 key,防止 shell 注入
safe_k = shlex.quote(k)
try:
return (
subprocess.run(
self._adb_args(
device_id, "shell", "am", "broadcast", "-a", act, "--ei", safe_k, v
),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=3.0,
check=False,
).returncode
== 0
)
except Exception:
return False
def keyevent(self, device_id: str, keycode: int) -> bool:
try:
code = int(keycode)
except Exception:
return False
return self._run_adb_shell(
device_id, f"input keyevent {code}", timeout_seconds=2.0
)
def adbkeyboard_editor_action(self, device_id: str, action_code: int = 4) -> bool:
"""使用 ADBKeyBoard 触发 editor action。
常用:IME_ACTION_SEND=4, IME_ACTION_DONE=6。
仅当当前输入法是 ADBKeyBoard 时更可靠。
"""
return self._adbkeyboard_broadcast_int(
device_id, "ADB_EDITOR_CODE", "code", int(action_code)
)
def confirm_from_keyboard(self, device_id: str) -> bool:
"""在输入框中用“键盘确认/发送”提交。
优先:ADBKeyBoard editor action SEND(4)
兜底:KEYCODE_ENTER(66)
"""
cur = ""
try:
cur = self.get_current_ime(device_id)
except Exception:
cur = ""
if "adbkeyboard" in str(cur).lower() or "adbime" in str(cur).lower():
if self.adbkeyboard_editor_action(device_id, 4):
return True
return self.keyevent(device_id, 66)
def input_text_adbkeyboard(
self,
device_id: str,
text: str,
ime_id: str = "com.android.adbkeyboard/.AdbIME",
restore_prev: bool = True,
) -> bool:
"""使用 ADBKeyboard 输入文本(更适合中文)。
要求:手机已安装并启用 ADBKeyboard。若不可用返回 False。
"""
t = str(text or "")
if not t:
return False
ok, prev, adb_ime = self.ensure_adbkeyboard_ime(device_id, preferred_ime=ime_id)
if not ok:
return False
success = False
try:
# 优先 b64(兼容中文/特殊字符),其次纯文本
b64 = base64.b64encode(t.encode("utf-8")).decode("ascii")
if self._adbkeyboard_broadcast(device_id, "ADB_INPUT_B64", b64):
success = True
elif self._adbkeyboard_broadcast(device_id, "ADB_INPUT_TEXT", t):
success = True
# 给 IME 一点时间把文本真正“打进”输入框
time_module.sleep(0.35)
return success
finally:
# 尝试恢复原输入法,避免影响手动使用
if restore_prev and prev and prev != adb_ime:
# 再稍等一下,避免过早切回导致输入丢失
time_module.sleep(0.25)
self.restore_ime(device_id, prev)
def paste_text_at(
self, device_id: str, x: int, y: int, text: str, ensure_focus: bool = True
) -> bool:
"""最简单的“粘贴到输入框”方法。
流程:点击输入框获取焦点 -> 设置剪贴板 -> 发送 KEYCODE_PASTE(279)。
若剪贴板不可用则退化为 `adb shell input text`(中文可能不稳定)。
"""
t = str(text or "").strip()
if not t:
return False
# 先确保输入框获得焦点
try:
if ensure_focus:
try:
_uo_log(
f"[Underlying] paste_text_at: clicking to focus device_id={device_id} coord=({x},{y})"
)
except Exception:
pass
self.click(device_id, int(x), int(y))
time_module.sleep(0.2)
# 有些输入框第一次点不进,补一次轻点
try:
_uo_log(
f"[Underlying] paste_text_at: clicking again to ensure focus device_id={device_id} coord=({x},{y})"
)
except Exception:
pass
self.click(device_id, int(x), int(y))
time_module.sleep(0.15)
except Exception:
pass
# 0) 优先:ADBKeyboard(如果已安装/可用)
# 说明:这一步不依赖剪贴板/粘贴菜单,通常对中文更稳定
try:
ok_adbkb = False
try:
ok_adbkb = bool(
self.input_text_adbkeyboard(device_id, t, restore_prev=False)
)
except Exception:
ok_adbkb = False
try:
_uo_log(
f"[Underlying] paste_text_at: input_text_adbkeyboard -> {ok_adbkb}"
)
except Exception:
pass
if ok_adbkb:
return True
except Exception:
pass
# 1) 优先:剪贴板 + 粘贴键
ok_clip = False
try:
ok_clip = bool(self.set_clipboard_text(device_id, t))
except Exception:
ok_clip = False
try:
_uo_log(f"[Underlying] paste_text_at: set_clipboard_text -> {ok_clip}")
except Exception:
pass
if ok_clip:
# 给系统一点时间刷新剪贴板
time_module.sleep(0.15)
ok_paste = False
try:
ok_paste = bool(self.paste_from_clipboard(device_id))
except Exception:
ok_paste = False
try:
_uo_log(f"[Underlying] paste_text_at: paste_from_clipboard -> {ok_paste}")
except Exception:
pass
if ok_paste:
return True
# 2) 兜底:adb input text(中文可能不稳定)
try:
_uo_log(
f"[Underlying] paste_text_at: falling back to input_text for device_id={device_id}"
)
except Exception:
pass
return self.input_text(device_id, t)
def _uiautomator_dump(self, device_id: str, local_path: str) -> bool:
"""拉取当前界面 UI 树到本地 xml(失败返回 False)。"""
try:
remote = "/sdcard/window_dump.xml"
subprocess.run(
self._adb_args(device_id, "shell", "uiautomator", "dump", remote),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2,
check=False,
)
subprocess.run(
[self._adb_path(), "-s", str(device_id), "pull", remote, str(local_path)],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2,
check=False,
)
return os.path.exists(local_path)
except subprocess.TimeoutExpired:
return False
except Exception:
return False
@staticmethod
def _parse_bounds(bounds: str):
return _impl_parse_bounds(bounds)
def tap_by_text(
self,
device_id: str,
text: str,
contains: bool = True,
timeout_seconds: float = 2.0,
) -> bool:
"""通过 uiautomator dump 按 text/content-desc 点击(命中即点并返回 True)。
适用于:\"推荐\"、\"直播\"、\"点击进入直播间\" 等。
"""
if not text:
return False
try:
from douyin.core import path_config
local_path = path_config.get_window_dump_path()
except Exception:
from douyin.core import path_config
local_path = path_config.get_window_dump_path()
start = time_module.time()
while True:
if self._uiautomator_dump(device_id, local_path):
try:
tree = ET.parse(local_path)
root = tree.getroot()
except Exception:
root = None
if root is not None:
target = str(text)
for node in root.iter():
t = node.attrib.get("text", "")
d = node.attrib.get("content-desc", "")
hit = False
if contains:
if (target in t) or (target in d):
hit = True
else:
if (target == t) or (target == d):
hit = True
if not hit:
continue
b = node.attrib.get("bounds", "")
parsed = self._parse_bounds(b)
if not parsed:
continue
x1, y1, x2, y2 = parsed
cx = int((x1 + x2) / 2)
cy = int((y1 + y2) / 2)
self.click(device_id, cx, cy)
return True
if time_module.time() - start >= timeout_seconds:
return False
time_module.sleep(0.2)
def tap_close_icon(
self, device_id: str, screen_width: int = 1080, timeout_seconds: float = 2.0
) -> bool:
"""尝试点击直播间顶部的关闭(X)按钮。
逻辑:从 uiautomator dump 里找顶部(较小 y)的可点击小图标,优先命中 content-desc/text 含“关闭/退出/返回”。
如果没有文字,就在顶部区域按“最像关闭”的位置进行启发式选择。
"""
try:
from douyin.core import path_config
local_path = path_config.get_window_dump_path()
except Exception:
from douyin.core import path_config
local_path = path_config.get_window_dump_path()
start = time_module.time()
def _score(node_attrib: dict, bounds_tuple) -> int:
x1, y1, x2, y2 = bounds_tuple
w = x2 - x1
h = y2 - y1
if y2 > 420:
return -9999
if w < 50 or h < 50 or w > 260 or h > 260:
return -9999
t = (node_attrib.get("text", "") or "").strip()
d = (node_attrib.get("content-desc", "") or "").strip()
s = t + " " + d
score = 0
# 明确关闭/退出优先
for kw in ("关闭", "退出", "返回", "close", "Close"):
if kw in s:
score += 200
# 排除一些常见非关闭按钮
for bad in ("分享", "更多", "菜单", "礼物", "榜", "关注", "评论"):
if bad in s:
score -= 120
# 顶部更靠上更像
score += max(0, 120 - y1)
# 更接近正方形的小图标更像
score -= abs(w - h)
# 默认更偏右上角(如果你的关闭在左边,也能被“更靠上/更像图标”拉回来)
cx = (x1 + x2) / 2
if screen_width > 0:
score += int((cx / screen_width) * 40)
return score
while True:
if self._uiautomator_dump(device_id, local_path):
try:
root = ET.parse(local_path).getroot()
except Exception:
root = None
best = None
best_score = -999999
if root is not None:
for node in root.iter():
attrib = getattr(node, "attrib", {}) or {}
# clickable 属性可能是 'true'/'false'
if str(attrib.get("clickable", "")).lower() != "true":
continue
if str(attrib.get("enabled", "true")).lower() == "false":
continue
b = attrib.get("bounds", "")
parsed = self._parse_bounds(b)
if not parsed:
continue
sc = _score(attrib, parsed)
if sc > best_score:
best_score = sc
best = parsed
if best and best_score > -1000:
x1, y1, x2, y2 = best
cx = int((x1 + x2) / 2)
cy = int((y1 + y2) / 2)
self.click(device_id, cx, cy)
return True
if time_module.time() - start >= timeout_seconds:
return False
time_module.sleep(0.2)