合并代码后提交

This commit is contained in:
zw
2025-08-13 20:20:13 +08:00
parent cd61c2138a
commit 33610b27b0
6 changed files with 418 additions and 67 deletions

View File

@@ -6,6 +6,7 @@ import numpy as np
import wda
from Utils.LogManager import LogManager
import xml.etree.ElementTree as ET
from lxml import etree
from wda import Client
# 工具类
@@ -279,4 +280,147 @@ class AiUtils(object):
@classmethod
def getUnReadMsgCount(cls, session: Client):
btn = cls.getMsgBoxButton(session)
return cls.findNumber(btn.label)
return cls.findNumber(btn.label)
# 获取聊天页面的聊天信息
@classmethod
def extract_messages_from_xml(cls, xml: str):
"""
输入 WDA 的页面 XML输出按时间顺序的消息列表
每项形如:
{'type': 'time', 'text': '昨天 下午8:48'}
{'type': 'msg', 'dir': 'in'|'out', 'text': 'hello'}
"""
root = etree.fromstring(xml.encode("utf-8"))
items = []
# 屏幕宽度(用于右对齐判断)
app = root.xpath('/XCUIElementTypeApplication')
screen_w = cls.parse_float(app[0], 'width', 414.0) if app else 414.0
# 1) 时间分隔
for t in root.xpath('//XCUIElementTypeStaticText[contains(@traits, "Header")]'):
txt = t.get('label') or t.get('name') or t.get('value') or ''
y = cls.parse_float(t, 'y')
if txt.strip():
items.append({'type': 'time', 'text': txt.strip(), 'y': y})
# 2) 消息气泡
msg_nodes = root.xpath(
'//XCUIElementTypeTable//XCUIElementTypeCell'
'//XCUIElementTypeOther[@name or @label]'
)
EXCLUDES = {'Heart', 'Lol', 'ThumbsUp', '分享发布内容', '视频贴纸标签页', '双击发送表情'}
for o in msg_nodes:
text = (o.get('label') or o.get('name') or '').strip()
if not text or text in EXCLUDES:
continue
# 拿到所在的 Cell用来找头像按钮
cell = o.getparent()
while cell is not None and cell.get('type') != 'XCUIElementTypeCell':
cell = cell.getparent()
x = cls.parse_float(o, 'x')
y = cls.parse_float(o, 'y')
w = cls.parse_float(o, 'width')
right_edge = x + w
direction = None
# 2.1 依据同 Cell 内“图片头像”的位置判定(优先,最稳)
if cell is not None:
avatar_btns = cell.xpath('.//XCUIElementTypeButton[@name="图片头像" or @label="图片头像"]')
if avatar_btns:
ax = cls.parse_float(avatar_btns[0], 'x')
# 头像在左侧 → 对方;头像在右侧 → 自己
if ax < screen_w / 2:
direction = 'in'
else:
direction = 'out'
# 2.2 退化规则:看是否右对齐
if direction is None:
# 离右边 <= 20px 视为右对齐(自己发的)
if right_edge > screen_w - 20:
direction = 'out'
else:
direction = 'in'
items.append({'type': 'msg', 'dir': direction, 'text': text, 'y': y})
# 3) 按 y 排序并清理
items.sort(key=lambda i: i['y'])
for it in items:
it.pop('y', None)
return items
@classmethod
def parse_float(cls, el, attr, default=0.0):
try:
return float(el.get(attr, default))
except Exception:
return default
# 从导航栏读取主播名称。找不到时返回空字符串
@classmethod
def get_navbar_anchor_name(cls, session, timeout: float = 2.0) -> str:
"""只从导航栏读取主播名称。找不到时返回空字符串。"""
# 可选:限制快照深度,提升解析速度/稳定性
try:
session.appium_settings({"snapshotMaxDepth": 22})
except Exception:
pass
def _text_of(el) -> str:
info = getattr(el, "info", {}) or {}
return (info.get("label") or info.get("name") or info.get("value") or "").strip()
def _clean_tail(s: str) -> str:
return re.sub(r"[,、,。.\s]+$", "", s).strip()
# 导航栏容器:从“返回”按钮向上找最近祖先,且该祖先内包含“更多/举报”(多语言兜底)
NAV_CONTAINER = (
"//XCUIElementTypeButton"
"[@name='返回' or @label='返回' or @name='Back' or @label='Back' or @name='戻る' or @label='戻る']"
"/ancestor::XCUIElementTypeOther"
"[ .//XCUIElementTypeButton"
" [@name='更多' or @label='更多' or @name='More' or @label='More' or "
" @name='その他' or @label='その他' or @name='詳細' or @label='詳細' or "
" @name='举报' or @label='举报' or @name='Report' or @label='Report' or "
" @name='報告' or @label='報告']"
"][1]"
)
# ① 优先:可访问的 Other自身有文本且不含子 Button更贴近 TikTok 的实现
XPATH_TITLE_OTHER = (
NAV_CONTAINER +
"//XCUIElementTypeOther[@accessible='true' and count(.//XCUIElementTypeButton)=0 "
" and (string-length(@name)>0 or string-length(@label)>0 or string-length(@value)>0)"
"][1]"
)
# ② 退路:第一个 StaticText
XPATH_TITLE_STATIC = (
NAV_CONTAINER +
"//XCUIElementTypeStaticText[string-length(@value)>0 or string-length(@label)>0 or string-length(@name)>0][1]"
)
# 尝试 ①
q = session.xpath(XPATH_TITLE_OTHER)
if q.wait(timeout):
t = _clean_tail(_text_of(q.get()))
if t:
return t
# 尝试 ②
q2 = session.xpath(XPATH_TITLE_STATIC)
if q2.wait(1.0):
t = _clean_tail(_text_of(q2.get()))
if t:
return t
return ""
# AiUtils.getCurrentScreenSource()