1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| import subprocess import os import time
from anyio import sleep from loguru import logger from matplotlib.cbook import pts_to_midstep
ADB_PATH = r"C:\Users\DELL\Desktop\platform-tools\adb.exe" DEVICE_ID = "emulator-5554"
@logger.catch def adb_connect(): try: result = subprocess.run([ADB_PATH, "-s", DEVICE_ID, "devices"], capture_output=True, text=True, check=True) if "device" in result.stdout: logger.debug(f"已连接到模拟器 {DEVICE_ID}") else: raise Exception(f"无法连接到模拟器 {DEVICE_ID}") except subprocess.CalledProcessError as e: logger.debug(f"连接模拟器失败: {e}") raise
@logger.catch def adb_root(): try: subprocess.run([ADB_PATH, "-s", DEVICE_ID, "root"], check=True) logger.debug("获取 root 权限成功!") except subprocess.CalledProcessError as e: logger.debug(f"无法获取 root 权限: {e}")
@logger.catch def list_files(remote_dir): try: result = subprocess.run( [ADB_PATH, "-s", DEVICE_ID, "shell", "ls", remote_dir], capture_output=True, text=True, check=True ) files = result.stdout.splitlines() return files except subprocess.CalledProcessError as e: logger.debug(f"获取文件列表失败: {e}") return []
@logger.catch def pull_files(remote_dir, local_dir, files): try: os.makedirs(local_dir, exist_ok=True)
for file in files: remote_file = os.path.join(remote_dir, file).replace("\\", "/") local_file = os.path.join(local_dir, file).replace("\\", "/") logger.debug(f"正在下载 {remote_file} 到 {local_file}")
subprocess.run([ADB_PATH, "-s", DEVICE_ID, "pull", remote_file, local_file], check=True) logger.debug(f"{file} 下载完成!") except subprocess.CalledProcessError as e: logger.debug(f"下载文件失败: {e}")
@logger.catch def main(): remote_dir = "/storage/emulated/0/Android/data/com.wtp.wtpilates/cache/video-cache" local_dir = "./video/"
try: adb_connect()
adb_root()
files = list_files(remote_dir) if files: logger.debug("文件列表:") for file in files: if not os.path.exists(os.path.join(local_dir, file)): logger.debug(file) else: files.remove(file) logger.debug(file + ", 已存在,跳过") continue pull_files(remote_dir, local_dir, files) else: logger.debug("没有找到文件。")
except Exception as e: logger.debug(f"发生错误: {e}")
if __name__ == "__main__": logger.add("../logs/ld_monitor.log", level="DEBUG", rotation="5MB") while True: main() logger.debug("sleep 60 s..") time.sleep(30)
|