fix: 微信改用会话列表(左)提取第二行+window.Refresh()强制刷新UIA
This commit is contained in:
+82
-30
@@ -143,33 +143,27 @@ class MessageMonitor:
|
||||
|
||||
# ── 核心:读消息 ──────────────────────────────────────────
|
||||
|
||||
def _read_messages(self, window) -> list:
|
||||
"""每轮重新定位 ListControl(Name='消息'),不缓存。"""
|
||||
# 策略 A: 精确搜索 Name="消息" 的 ListControl
|
||||
for name in ["消息", "Message"]:
|
||||
try:
|
||||
lst = window.ListControl(Name=name)
|
||||
if lst.Exists(maxSearchSeconds=0.2):
|
||||
texts = self._extract_list_texts(lst)
|
||||
if texts:
|
||||
logger.debug(f"[S-A] ListControl({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"")
|
||||
return texts
|
||||
else:
|
||||
logger.debug(f"[S-A] ListControl({name}) 存在但无文本")
|
||||
except Exception as e:
|
||||
logger.debug(f"[S-A] ListControl({name}) 异常: {e}")
|
||||
def _read_messages(self, window, platform: str = "") -> list:
|
||||
"""每轮重新定位 ListControl,不缓存。"""
|
||||
# 微信:先读"会话"(左侧摘要,永远最新)
|
||||
if platform == "wechat":
|
||||
texts = self._read_conversation_list(window)
|
||||
if texts:
|
||||
return texts
|
||||
|
||||
# 策略 B: Name="会话"(左侧列表,能读到最新摘要)
|
||||
for name in ["会话", "聊天", "Chat"]:
|
||||
try:
|
||||
lst = window.ListControl(Name=name)
|
||||
if lst.Exists(maxSearchSeconds=0.2):
|
||||
texts = self._extract_list_texts(lst)
|
||||
if texts:
|
||||
logger.debug(f"[S-B] ListControl({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"")
|
||||
return texts
|
||||
except Exception as e:
|
||||
logger.debug(f"[S-B] ListControl({name}) 异常: {e}")
|
||||
# QQ:先读"消息"(右侧详情列表)
|
||||
if platform == "qq":
|
||||
texts = self._read_message_list(window)
|
||||
if texts:
|
||||
return texts
|
||||
|
||||
# 兜底:两个都试
|
||||
texts = self._read_message_list(window)
|
||||
if texts:
|
||||
return texts
|
||||
texts = self._read_conversation_list(window)
|
||||
if texts:
|
||||
return texts
|
||||
|
||||
# 策略 C: 任何右侧 ListControl
|
||||
try:
|
||||
@@ -179,7 +173,7 @@ class MessageMonitor:
|
||||
try:
|
||||
cr = lst.BoundingRectangle
|
||||
if cr.left >= boundary:
|
||||
texts = self._extract_list_texts(lst)
|
||||
texts = self._extract_last_line(lst)
|
||||
if texts:
|
||||
logger.debug(f"[S-C] ListControl(x={cr.left}) → {len(texts)} items")
|
||||
return texts
|
||||
@@ -199,10 +193,40 @@ class MessageMonitor:
|
||||
|
||||
return []
|
||||
|
||||
# ── 提取 ListItem 文本 ───────────────────────────────────
|
||||
def _read_message_list(self, window) -> list:
|
||||
"""读 ListControl '消息'(右侧详情列表)。"""
|
||||
for name in ["消息", "Message"]:
|
||||
try:
|
||||
lst = window.ListControl(Name=name)
|
||||
if lst.Exists(maxSearchSeconds=0.2):
|
||||
texts = self._extract_last_line(lst)
|
||||
if texts:
|
||||
logger.debug(f"[S-A] ListControl({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"")
|
||||
return texts
|
||||
logger.debug(f"[S-A] ListControl({name}) 存在但无文本")
|
||||
except Exception as e:
|
||||
logger.debug(f"[S-A] ListControl({name}) 异常: {e}")
|
||||
return []
|
||||
|
||||
def _read_conversation_list(self, window) -> list:
|
||||
"""读 ListControl '会话'(左侧摘要,总是包含最新消息摘要)。"""
|
||||
for name in ["会话", "聊天", "Chat"]:
|
||||
try:
|
||||
lst = window.ListControl(Name=name)
|
||||
if lst.Exists(maxSearchSeconds=0.2):
|
||||
texts = self._extract_second_line(lst)
|
||||
if texts:
|
||||
logger.debug(f"[S-B] 会话({name}) → {len(texts)} items, last=\"{texts[-1][:50]}\"")
|
||||
return texts
|
||||
except Exception as e:
|
||||
logger.debug(f"[S-B] ListControl({name}) 异常: {e}")
|
||||
return []
|
||||
|
||||
# ── 提取方法 ─────────────────────────────────────────────
|
||||
|
||||
@staticmethod
|
||||
def _extract_list_texts(lst) -> list:
|
||||
def _extract_last_line(lst) -> list:
|
||||
"""从 ListItem Name 提取最后一行(消息详情列表用)。"""
|
||||
texts = []
|
||||
try:
|
||||
for item in lst.GetChildren():
|
||||
@@ -218,6 +242,28 @@ class MessageMonitor:
|
||||
pass
|
||||
return texts
|
||||
|
||||
@staticmethod
|
||||
def _extract_second_line(lst) -> list:
|
||||
"""从 ListItem Name 提取第二行(会话摘要列表用:格式为 名字\\n消息\\n时间)。"""
|
||||
texts = []
|
||||
try:
|
||||
for item in lst.GetChildren():
|
||||
try:
|
||||
name = item.Name
|
||||
if name and name.strip():
|
||||
lines = [l.strip() for l in name.split("\n") if l.strip()]
|
||||
# 会话列表格式:姓名、消息内容、时间、状态...
|
||||
# 取索引 1(消息内容),不够两行时取最后一行
|
||||
if len(lines) >= 2:
|
||||
texts.append(lines[1])
|
||||
elif lines:
|
||||
texts.append(lines[-1])
|
||||
except Exception:
|
||||
pass
|
||||
except Exception:
|
||||
pass
|
||||
return texts
|
||||
|
||||
# ── 枚举所有 ListControl ────────────────────────────────
|
||||
|
||||
@staticmethod
|
||||
@@ -277,8 +323,14 @@ class MessageMonitor:
|
||||
|
||||
win = self._cached_window[platform]
|
||||
|
||||
# 强制刷新 UIA 控件树(微信/QQ 虚拟列表不自动同步新消息)
|
||||
try:
|
||||
win.Refresh()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 读消息
|
||||
texts = self._read_messages(win)
|
||||
texts = self._read_messages(win, platform)
|
||||
|
||||
if not texts:
|
||||
empty = self._empty_reads.get(platform, 0) + 1
|
||||
|
||||
Reference in New Issue
Block a user