Files
douyi_guaji_main/Underlying_Operations.py
T

1193 lines
48 KiB
Python
Raw 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
from datetime import datetime, timedelta
import os
# 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")
import time
from PIL import Image, ImageFilter
import numpy as np
import logging
import re
import random
import xml.etree.ElementTree as ET
import shlex
import contextlib
import base64
from typing import Tuple, Optional
class underlying_operations:
"""底层操作类"""
def __init__(self):
# 关闭文档方向/矫正等重型前处理:我们的截图是手机UI,不是扫描文档。
# 支持延迟初始化(测试流程/只做点击时不需要 OCR)。
self.ocr = None
if not str(os.environ.get("SKIP_OCR_INIT", "")).strip():
# 在导入和实例化 PaddleOCR 时重定向 stdout/stderr,
# 以抑制其在控制台打印的“Checking connectivity/create model”等信息。
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):
from paddleocr import PaddleOCR
self.ocr = PaddleOCR(
lang="ch",
ocr_version="PP-OCRv4",
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
try:
devnull.close()
except Exception:
pass
except Exception:
# 若静默初始化失败,保留 None 并允许后续按需初始化,避免阻塞启动。
try:
devnull.close()
except Exception:
pass
self.ocr = None
logging.disable(logging.DEBUG)
logging.disable(logging.WARNING)
@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:
"""返回可用于 subprocess(list_args) 的 adb 路径(不带外层引号)。"""
adb = str(os.environ.get("ADB_EXECUTABLE", "adb") or "adb")
if adb.startswith('"') and adb.endswith('"'):
adb = adb[1:-1]
return adb
def _ensure_ocr(self):
if self.ocr is not None:
return
try:
devnull = open(os.devnull, "w")
with contextlib.redirect_stdout(devnull), contextlib.redirect_stderr(devnull):
from paddleocr import PaddleOCR
self.ocr = PaddleOCR(
lang="ch",
ocr_version="PP-OCRv4",
use_doc_orientation_classify=False,
use_doc_unwarping=False,
use_textline_orientation=False,
)
try:
devnull.close()
except Exception:
pass
except Exception:
try:
devnull.close()
except Exception:
pass
self.ocr = None
def select_device(self):
"""选择需要连接的设备"""
string = subprocess.Popen(f'{self._adb()} devices', shell=True, stdout=subprocess.PIPE)
totalstring = string.stdout.read()
totalstring = totalstring.decode('utf-8')
# print(totalstring)
pattern = r'(\b(?:[0-9]{1,3}(?:\.[0-9]{1,3}){3}(?::[0-9]+)?|[A-Za-z0-9]{8,})\b)\s*device\b'
devicelist = re.findall(pattern, totalstring)
# devicelist = re.compile(r'(\w*)\s*device\b').findall(totalstring)
devicenum = len(devicelist)
if devicenum == 0:
print("当前无设备连接电脑,请检查设备连接情况!")
return False
elif devicenum == 1:
print("当前有一台设备连接,编号:%s." % devicelist[0])
return devicelist[0]
else:
print("当前存在多台设备连接! 输入数字选择对应设备:")
dictdevice = {}
for i in range(devicenum):
string = subprocess.Popen("%s -s %s shell getprop ro.product.device" % (self._adb(), devicelist[i]), shell=True,
stdout=subprocess.PIPE)
modestring = string.stdout.read().strip() # 去除掉自动生成的回车
print("%s:%s---%s" % (i + 1, devicelist[i], modestring))
dictdevice[i + 1] = devicelist[i]
num = input()
num = int(num)
while not num in dictdevice.keys():
print('输入不正确,请重新输入:')
num = input()
num = int(num)
return dictdevice[num]
def get_screenshot(self, device_id, path='pic'):
"""截图3个adb命令需要2S左右的时间"""
if path == 'pic':
out_dir = os.path.join(os.path.dirname(__file__), 'pic')
else:
out_dir = os.path.join(os.path.dirname(__file__), 'target_pic')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
# 统一输出到固定文件名,兼容现有 cut_pic()/后续流程
out_file = os.path.join(out_dir, 'screenshot.png')
tmp_file = out_file + '.tmp'
timetag = datetime.now().strftime('%H:%M:%S')
adb = self._adb_path()
def _valid_png(path0: str) -> bool:
try:
if not os.path.exists(path0):
return False
sz = os.path.getsize(path0)
# PNG 通常远大于 1KB;0 字节/极小文件大概率是 adb 输出异常/中断
return sz >= 1024
except Exception:
return False
# 优先走 exec-out:只需 1 次 adb 调用;不在手机端落盘,减少 I/O 与卡顿
# 为避免被并发读取到“半写入文件”,写入 tmp 后再原子替换。
for _ in range(2):
try:
try:
if os.path.exists(tmp_file):
os.remove(tmp_file)
except Exception:
pass
with open(tmp_file, 'wb') as f:
subprocess.run(
[adb, '-s', device_id, 'exec-out', 'screencap', '-p'],
stdout=f,
stderr=subprocess.DEVNULL,
check=True,
)
if _valid_png(tmp_file):
os.replace(tmp_file, out_file)
print("{} 获取屏幕截图".format(timetag))
return True
except Exception:
# 继续回退/重试
try:
if os.path.exists(tmp_file):
os.remove(tmp_file)
except Exception:
pass
# 回退方案:老式 shell screencap + pull(兼容极少数不支持 exec-out 的环境)
try:
subprocess.run(
[adb, '-s', device_id, 'shell', 'screencap', '-p', '/sdcard/DCIM/screenshot.png'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
subprocess.run(
[adb, '-s', device_id, 'pull', '/sdcard/DCIM/screenshot.png', tmp_file],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
if _valid_png(tmp_file):
os.replace(tmp_file, out_file)
print("{} 获取屏幕截图".format(timetag))
return True
# 文件异常则继续进入更深回退
except Exception:
# 再次回退:尝试用 screenshot1.png 覆盖 screenshot.png
try:
subprocess.run(
[adb, '-s', device_id, 'shell', 'screencap', '-p', '/sdcard/DCIM/screenshot1.png'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
subprocess.run(
[adb, '-s', device_id, 'shell', 'mv', '/sdcard/DCIM/screenshot1.png', '/sdcard/DCIM/screenshot.png'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
return self.get_screenshot(device_id, path)
except Exception:
return False
def get_current_hour(self):
"""获取当前的时间小时数"""
time_hour = datetime.now().strftime('%H')
print("当前时间{}点".format(time_hour))
return int(time_hour)
def click_confirm(self, device_id):
"""点击确认键"""
os.system("%s -s %s shell input keyevent 66" % (self._adb(), device_id))
print("点击确认键")
def click_back(self, device_id):
"""点击返回键"""
os.system("%s -s %s shell input keyevent 4" % (self._adb(), device_id))
print("点击返回键")
def get_ballery_level(self, device_id):
"""获取设备电量信息"""
battery_info = subprocess.Popen("%s -s %s shell dumpsys battery" % (self._adb(), device_id), shell=True,
stdout=subprocess.PIPE)
battery_info_string = battery_info.stdout.read()
battery_info_string = bytes.decode(battery_info_string)
location = re.search('level:', battery_info_string)
span = location.span()
start, end = span
start = end + 1
for i in range(5):
end += 1
if battery_info_string[end] == "\n":
break
battery_level = battery_info_string[start:end] # 第几个到第几个中间接冒号
battery_level = int(battery_level)
print("设备当前电量为{}".format(battery_level))
return battery_level
def get_device_resolution(self, device_id):
"""获取设备分辨率"""
resolution_info = subprocess.Popen("%s -s %s shell wm size" % (self._adb(), device_id), shell=True,
stdout=subprocess.PIPE)
resolution_info_string = resolution_info.stdout.read()
resolution_info_string = bytes.decode(resolution_info_string)
match = re.search(r'Physical size: (\d+)x(\d+)', resolution_info_string)
if match:
width = int(match.group(1))
height = int(match.group(2))
print("设备分辨率为:{}*{}".format(width, height))
return width, height
else:
raise ValueError("Could not find resolution in adb output")
return False
def save_reward_pic(self, device_id):
out_dir = os.path.join(os.path.dirname(__file__), 'pic')
if not os.path.exists(out_dir):
os.makedirs(out_dir)
timepic = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
out_file = os.path.join(out_dir, f'{timepic}.png')
adb = self._adb_path()
# 同样优先使用 exec-out,减少手机端 I/O
try:
with open(out_file, 'wb') as f:
subprocess.run(
[adb, '-s', device_id, 'exec-out', 'screencap', '-p'],
stdout=f,
stderr=subprocess.DEVNULL,
check=True,
)
print("中奖了,点击领奖,保存中奖图片{}.png".format(timepic))
return
except Exception:
pass
# 回退:shell screencap + pull
try:
subprocess.run(
[adb, '-s', device_id, 'shell', 'screencap', '-p', '/sdcard/DCIM/screenshot.png'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
subprocess.run(
[adb, '-s', device_id, 'pull', '/sdcard/DCIM/screenshot.png', out_file],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=True,
)
subprocess.run(
[adb, '-s', device_id, 'shell', 'rm', '/sdcard/DCIM/screenshot.png'],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
check=False,
)
print("中奖了,点击领奖,保存中奖图片{}.png".format(timepic))
except Exception:
# 保存失败不打断主流程
print("保存中奖截图失败")
def cut_pic(self, left_up=(0, 63), right_down=(1080, 1620), target='', name=''):
'''裁剪图片,获取需要的区域小图片方便识别'''
if target == '' or target == False:
path = os.path.dirname(__file__) + '/pic'
pic1_path = path + '/screenshot.png'
pic = Image.open(pic1_path)
if name == '':
cut_pic_path = path + '/cut.png'
else:
cut_pic_path = path + '/' + name + '.png'
pic.crop((left_up[0], left_up[1], right_down[0], right_down[1])).save(cut_pic_path)
return True
path_target = os.path.dirname(__file__) + '/pic/' + target
pic1_path = path_target + '/screenshot.png'
pic = Image.open(pic1_path)
if name == '':
cut_pic_path = path_target + '/cut.png'
else:
cut_pic_path = path_target + '/' + name + '.png'
pic.crop((left_up[0], left_up[1], right_down[0], right_down[1])).save(cut_pic_path)
def analyse_pic_word(self, picname='', change_color=0):
"""识别图像中的文字, change_color=1或2为不同的二值化模式,其他不做处理"""
self._ensure_ocr()
path = os.path.dirname(__file__) + '/pic'
if picname == '':
pic = path + '/cut.png'
else:
pic = path + '/' + picname + '.png'
img = Image.open(pic)
# 福袋内容/倒计时属于手机 UI 字体,强二值化容易把细节打没,默认保持彩色。
if change_color in (1, 2):
img = img.convert('L') # 转换为灰度图
if change_color == 1:
img = img.point(lambda x: 0 if x < 128 else 255) # 二值化
else:
img = img.point(lambda x: 0 if x < 180 else 255) # 二值化
img = img.convert('RGB')
else:
img = img.convert('RGB')
# 性能优化:先用原尺寸跑一次 OCR;只有识别为空时才放大重试。
w, h = img.size
img1 = img.filter(ImageFilter.SHARPEN)
img_np = np.array(img1)
result = self.ocr.ocr(img_np)
if result == [None]:
return ''
text = self.extract_ocr_content(result)
if (not text) and max(w, h) <= 1200:
try:
img2 = img.resize((w * 2, h * 2), Image.BICUBIC).filter(ImageFilter.SHARPEN)
result2 = self.ocr.ocr(np.array(img2))
if result2 != [None]:
text = self.extract_ocr_content(result2)
result = result2
except Exception:
pass
# 对关键字段做轻量级调试输出:图片很清楚但识别为空时,通常是返回结构变化或未检出文本
if picname in ("fudai_content", "fudai_countdown") and not text:
try:
if isinstance(result, list) and result and isinstance(result[0], dict):
print(f"OCR调试({picname}): result[0] keys={list(result[0].keys())}")
else:
print(f"OCR调试({picname}): result type={type(result)}")
except Exception:
pass
return text
def analyse_pic_word_fast(self, picname='', change_color=0, max_side=600):
"""更快的 OCR 路径:对裁剪图像做轻量化预处理并只执行一次 OCR(不做放大重试),用于对小区域的快速判定。
参数:
picname: 图片在 pic/ 中的文件名(不含后缀),空串表示默认 cut.png
change_color: 同 `analyse_pic_word` 的二值化模式(0=不二值化,1/2=两种二值化)
max_side: 若图片任一边超过该值,则按比例缩小到该尺寸,减少 OCR 计算量
返回: 识别到的文本(字符串)或空串
"""
self._ensure_ocr()
path = os.path.dirname(__file__) + '/pic'
if picname == '':
img_path = path + '/cut.png'
else:
img_path = path + '/' + picname + '.png'
try:
img = Image.open(img_path)
except Exception:
return ''
# 轻量化预处理:灰度/二值化 (若指定)
try:
if change_color in (1, 2):
img = img.convert('L')
if change_color == 1:
img = img.point(lambda x: 0 if x < 128 else 255)
else:
img = img.point(lambda x: 0 if x < 180 else 255)
img = img.convert('RGB')
else:
img = img.convert('RGB')
except Exception:
img = img.convert('RGB')
# 若图片较大,按比例缩小到 max_side,减小 OCR 负担
try:
w, h = img.size
m = max(w, h)
if max_side and m > max_side:
scale = max_side / float(m)
nw = max(1, int(w * scale))
nh = max(1, int(h * scale))
img = img.resize((nw, nh), Image.BILINEAR)
except Exception:
pass
try:
img_np = np.array(img.filter(ImageFilter.SHARPEN))
result = self.ocr.ocr(img_np)
if result == [None]:
return ''
return self.extract_ocr_content(result)
except Exception:
return ''
def extract_ocr_content(self, content=[]):
"""对OCR识别到的内容进行取值和拼接,变成完整的一段内容"""
ocr_result = content
def _extract_text_from_item(item):
# 兼容旧格式:[box, (text, score)]
if isinstance(item, (list, tuple)) and len(item) >= 2:
second = item[1]
if isinstance(second, (list, tuple)) and len(second) >= 1:
if isinstance(second[0], str):
return second[0]
if isinstance(second, dict):
for key in ("text", "rec_text", "transcription"):
val = second.get(key)
if isinstance(val, str) and val:
return val
if isinstance(second, str):
return second
# 兼容新格式:dict / 对象
if isinstance(item, dict):
# 常见:rec_texts 为字符串列表
for list_key in ("rec_texts", "texts", "text_list"):
vals = item.get(list_key)
if isinstance(vals, list) and vals:
vals = [v for v in vals if isinstance(v, str) and v]
if vals:
return ''.join(vals)
for key in ("text", "rec_text", "transcription"):
val = item.get(key)
if isinstance(val, str) and val:
return val
for attr in ("text", "rec_text", "transcription"):
val = getattr(item, attr, None)
if isinstance(val, str) and val:
return val
# 对象属性里也可能是列表
for list_attr in ("rec_texts", "texts", "text_list"):
vals = getattr(item, list_attr, None)
if isinstance(vals, list) and vals:
vals = [v for v in vals if isinstance(v, str) and v]
if vals:
return ''.join(vals)
return ""
# PaddleOCR 可能返回 [[...]] 或 [...]
items = ocr_result
if isinstance(ocr_result, list) and len(ocr_result) == 1 and isinstance(ocr_result[0], list):
items = ocr_result[0]
def _extract_xy_from_item(item):
"""尽力从 OCR item 中提取用于排序的 (y, x)。无法提取则返回 None。"""
try:
# 旧格式:item = [box, (text, score)],box 为 4 点坐标
if isinstance(item, (list, tuple)) and len(item) >= 1:
box = item[0]
if isinstance(box, (list, tuple)) and box:
xs = []
ys = []
for p in box:
if isinstance(p, (list, tuple)) and len(p) >= 2:
xs.append(float(p[0]))
ys.append(float(p[1]))
if xs and ys:
return (min(ys), min(xs))
except Exception:
return None
return None
extracted_items = []
if isinstance(items, list):
for idx, item in enumerate(items):
text = _extract_text_from_item(item)
if not text:
continue
xy = _extract_xy_from_item(item)
if xy is None:
# 无坐标信息:保持原相对顺序
extracted_items.append((1e18, float(idx), text))
else:
y, x = xy
extracted_items.append((float(y), float(x), text))
# 按从上到下、从左到右排序;解决同一行多个框的乱序拼接(例如倒计时两段数字)
extracted_items.sort(key=lambda t: (t[0], t[1]))
return ''.join([t[2] for t in extracted_items])
def delay(self, seconds=1):
time.sleep(seconds)
def random_delay(self, min_seconds=1, max_seconds=100):
"""等待随机的时长"""
random_wait_seconds = random.randint(min_seconds, max_seconds)
print("随机等待{}秒.".format(random_wait_seconds))
self.delay(random_wait_seconds)
def check_countdown(self, last_time=''):
"""对倒计时时间进行转化,返回 (剩余秒数, 预计开奖时间戳)。
兼容 OCR 常见格式与误识别:
- 11:37 / 11:37 / 11:37后开奖
- 1分23秒 / 23秒
- 1137 / 934 / 31
- 字符混淆:O->0, l/I->1, S->5 等
"""
def _norm(s: str) -> str:
t = str(s or "")
t = re.sub(r"\s+", "", t)
t = t.replace(":", ":")
# 常见 OCR 混淆纠正
trans = str.maketrans({
"O": "0", "o": "0", "Q": "0", "D": "0",
"I": "1", "l": "1", "|": "1", "!": "1",
"S": "5", "s": "5",
"B": "8",
"G": "6",
"Z": "2",
})
t = t.translate(trans)
return t
def _parse_seconds(text: str) -> int | None:
t = _norm(text)
if not t:
return None
# 1) mm:ss(允许 ss 为 1 位)
m = re.search(r"(\d{1,2})[:](\d{1,2})", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
# 若秒数 >= 60,优先尝试交换(OCR 容易把分秒顺序颠倒)
if ss >= 60 and mm < 60:
mm, ss = ss, mm
# 兜底:仍不合法则做进位
if ss >= 60:
mm += ss // 60
ss = ss % 60
return max(0, mm * 60 + ss)
# 2) 中文:x分y秒 / y秒
m = re.search(r"(\d{1,2})分(\d{1,2})秒", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
if ss >= 60:
mm += ss // 60
ss = ss % 60
return max(0, mm * 60 + ss)
m = re.search(r"(\d{1,3})秒", t)
if m:
return max(0, int(m.group(1)))
# 3) 纯数字:1137(=11:37) / 934(=9:34) / 31(=31秒)
digits = "".join(ch for ch in t if ch.isdigit())
if not digits:
return None
if len(digits) == 4:
a = int(digits[:2])
b = int(digits[2:])
# 两种解释:a:b 或 b:a。
# 默认按“前两位=分、后两位=秒”,只有在明显异常(>15分钟)或秒不合法时才交换。
if b < 60:
total1 = a * 60 + b
else:
total1 = None
if a < 60:
total2 = b * 60 + a
else:
total2 = None
if total1 is not None and total1 <= 900:
return total1
if total2 is not None and total2 <= 900:
return total2
# 若一个明显异常而另一个更合理(<=900),交换
if total1 is not None and total2 is not None:
if total1 > 900 and total2 <= 900:
return total2
if total2 > 900 and total1 <= 900:
return total1
# 兜底:谁能解析就用谁;都不行则做进位
if total1 is not None:
return total1
if total2 is not None:
return total2
mm, ss = a, b
mm += ss // 60
ss = ss % 60
return mm * 60 + ss
if len(digits) == 3:
mm = int(digits[0])
ss = int(digits[1:])
if ss >= 60:
# 例如 159 可能是 15:09(OCR 丢 0)
mm2 = int(digits[:2])
ss2 = int(digits[2])
if ss2 < 60:
return mm2 * 60 + ss2
mm += ss // 60
ss = ss % 60
return mm * 60 + ss
if len(digits) == 2:
return int(digits) # 直接当秒(更符合“临近开奖”场景)
if len(digits) == 1:
return int(digits)
# 太长:取最后4位(OCR 常把别的数字也识别进来)
tail = digits[-4:]
a = int(tail[:2])
b = int(tail[2:])
if b >= 60 and a < 60:
a, b = b, a
if b >= 60:
a += b // 60
b = b % 60
return a * 60 + b
try:
total_seconds = _parse_seconds(last_time)
if total_seconds is None:
print("时间格式异常")
return False
print("剩余总秒数:", total_seconds)
# 超过 15 分钟基本是误识别:按 14分50秒处理,避免卡死等待
if total_seconds > 900:
total_seconds = 890
now = datetime.now()
print("当前时间:", now.strftime('%Y-%m-%d %H:%M:%S'))
future_time = now + timedelta(seconds=int(total_seconds))
future_timestamp = future_time.timestamp()
future_datetime = datetime.fromtimestamp(future_timestamp)
formatted_future_time = future_datetime.strftime('%Y-%m-%d %H:%M:%S')
print("预计开奖时间:", formatted_future_time)
return int(total_seconds), future_timestamp
except Exception:
print("输入的字符串不是有效的时间格式")
return False
def swipe(self, device_id, left_up_x=0, left_up_y=0, right_down_x=1080, right_down_y=1500, steps=200):
"""划动屏幕"""
os.system("{} -s {} shell input swipe {} {} {} {} {}".format(self._adb(), device_id, left_up_x, left_up_y, right_down_x, right_down_y, steps))
def click(self, device_id, x=500, y=500):
"""点击坐标位置"""
os.system(
"{} -s {} shell input tap {} {}".format(self._adb(), device_id, x, y))
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
os.system(f"{self._adb()} -s {device_id} shell input swipe {int(x)} {int(y)} {int(x)} {int(y)} {d}")
def _run_adb_shell(self, device_id: str, shell_cmd: str, timeout_seconds: float = 3.0) -> bool:
"""运行 adb shell 命令,成功返回 True。"""
try:
r = subprocess.run(
f"{self._adb()} -s {device_id} shell {shell_cmd}",
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=timeout_seconds,
)
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(
f"{self._adb()} -s {device_id} shell {shell_cmd}",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL,
timeout=timeout_seconds,
text=True,
encoding="utf-8",
errors="ignore",
)
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 ""
@staticmethod
def _escape_shell_double_quotes(text: str) -> str:
return str(text).replace('\\', '\\\\').replace('"', '\\"')
def set_clipboard_text(self, device_id: str, text: str) -> bool:
"""设置设备剪贴板文本(尽量支持中文)。
优先:Android 10+ 的 `cmd clipboard set`。
兜底:Clipper(若设备装了 clipper app)。
"""
t = str(text or "").strip()
if not t:
return False
# 1) cmd clipboard set "..."
esc = self._escape_shell_double_quotes(t)
if self._run_adb_shell(device_id, f'cmd clipboard set "{esc}"', timeout_seconds=3.0):
return True
# 2) Clipper broadcast: am broadcast -a clipper.set -e text "..."
if self._run_adb_shell(device_id, f'am broadcast -a clipper.set -e text "{esc}"', timeout_seconds=3.0):
return True
return False
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')
# 仅做最小转义,避免破坏中文
t = t.replace('&', '\\&').replace('<', '\\<').replace('>', '\\>').replace('|', '\\|')
return self._run_adb_shell(device_id, f'input text "{self._escape_shell_double_quotes(t)}"', timeout_seconds=5.0)
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
def _pick_adbkeyboard_ime(imes: list[str]) -> str:
# 兼容不同包名/组件名写法,优先匹配常见的 AdbIME
for cand in imes or []:
s = str(cand).strip()
ls = s.lower()
if "adbkeyboard" in ls and "adbime" in ls:
return s
for cand in imes or []:
s = str(cand).strip()
ls = s.lower()
if "adbkeyboard" in ls or "adbime" in ls:
return s
return ""
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 {ime}", timeout_seconds=2.5)
if self._run_adb_shell(device_id, f"ime set {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
esc = self._escape_shell_double_quotes(str(msg or ""))
# 这里不依赖 broadcast 的返回文本,只要命令成功执行就认为 ok
return self._run_adb_shell(device_id, f'am broadcast -a {act} --es msg "{esc}"', timeout_seconds=3.0)
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
return self._run_adb_shell(device_id, f"am broadcast -a {act} --ei {k} {v}", timeout_seconds=3.0)
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.sleep(0.35)
return success
finally:
# 尝试恢复原输入法,避免影响手动使用
if restore_prev and prev and prev != adb_ime:
# 再稍等一下,避免过早切回导致输入丢失
time.sleep(0.25)
self.restore_ime(device_id, prev)
def paste_text_at(self, device_id: str, x: int, y: int, text: str) -> bool:
"""最简单的“粘贴到输入框”方法。
流程:点击输入框获取焦点 -> 设置剪贴板 -> 发送 KEYCODE_PASTE(279)。
若剪贴板不可用则退化为 `adb shell input text`(中文可能不稳定)。
"""
t = str(text or "").strip()
if not t:
return False
# 先确保输入框获得焦点
try:
self.click(device_id, int(x), int(y))
time.sleep(0.2)
# 有些输入框第一次点不进,补一次轻点
self.click(device_id, int(x), int(y))
time.sleep(0.15)
except Exception:
pass
# 0) 优先:ADBKeyboard(如果已安装/可用)
# 说明:这一步不依赖剪贴板/粘贴菜单,通常对中文更稳定
try:
if self.input_text_adbkeyboard(device_id, t, restore_prev=False):
return True
except Exception:
pass
# 1) 优先:剪贴板 + 粘贴键
if self.set_clipboard_text(device_id, t):
# 给系统一点时间刷新剪贴板
time.sleep(0.15)
if self.paste_from_clipboard(device_id):
return True
# 2) 兜底:adb input text(中文可能不稳定)
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(
f'{self._adb()} -s {device_id} shell uiautomator dump {remote}',
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2,
)
subprocess.run(
f'{self._adb()} -s {device_id} pull {remote} "{local_path}"',
shell=True,
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
timeout=2,
)
return os.path.exists(local_path)
except subprocess.TimeoutExpired:
return False
except Exception:
return False
@staticmethod
def _parse_bounds(bounds: str):
m = re.match(r"\[(\d+),(\d+)\]\[(\d+),(\d+)\]", str(bounds or ""))
if not m:
return None
x1, y1, x2, y2 = map(int, m.groups())
return x1, y1, x2, y2
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
local_path = os.path.join(os.path.dirname(__file__), "pic", "window_dump.xml")
start = time.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.time() - start >= timeout_seconds:
return False
time.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 含“关闭/退出/返回”。
如果没有文字,就在顶部区域按“最像关闭”的位置进行启发式选择。
"""
local_path = os.path.join(os.path.dirname(__file__), "pic", "window_dump.xml")
start = time.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.time() - start >= timeout_seconds:
return False
time.sleep(0.2)