From 4ee38b642f5ed8ef3a367c07e3812d903dc6c274 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 4 Feb 2025 22:08:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=20=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E6=B6=88=E6=81=AF=20=E5=92=8C=E5=B7=B2=E5=8F=91=E9=80=81=20?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .idea/.gitignore | 8 ++++++++ .idea/git_toolbox_prj.xml | 15 ++++++++++++++ .idea/misc.xml | 8 ++++++++ .idea/modules.xml | 8 ++++++++ .idea/uiautomator2.iml | 9 ++++++++ .idea/vcs.xml | 6 ++++++ api/wechat.py | 9 +++++--- tasks/wechat/__init__.py | 43 ++++++++++++++++++++++++++++++++------- 8 files changed, 96 insertions(+), 10 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/git_toolbox_prj.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/uiautomator2.iml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/git_toolbox_prj.xml b/.idea/git_toolbox_prj.xml new file mode 100644 index 0000000..02b915b --- /dev/null +++ b/.idea/git_toolbox_prj.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..4f2ea69 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,8 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1358530 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/uiautomator2.iml b/.idea/uiautomator2.iml new file mode 100644 index 0000000..1270544 --- /dev/null +++ b/.idea/uiautomator2.iml @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/api/wechat.py b/api/wechat.py index 709234d..a144307 100644 --- a/api/wechat.py +++ b/api/wechat.py @@ -18,15 +18,17 @@ class WechatSearchInput(Device): input_text: str -@router.post("/search/input") +@router.post("/search/chat") async def wechat_search_task(item: WechatSearchInput): if item.serial not in devices: return Response(code=500, msg="设备 {} 不存在".format(item.serial)) d = devices[item.serial] + d.app_start("com.tencent.mm") + tasks.wechat.click_search_btn(d) tasks.wechat.input_search_text(d, item.search_keyword) tasks.wechat.click_search_result(d) - tasks.wechat.get_last_chat_text(d) + print(tasks.wechat.get_last_friend_text_msg(d)) # tasks.wechat.input_chat_text(d, time.strftime("[AI] %Y-%m-%d %H:%M:%S", time.localtime())) tasks.wechat.input_chat_text(d, item.input_text) @@ -36,4 +38,5 @@ def wechat_launch(item: Device): return Response(code=500, msg="设备 {} 不存在".format(item.serial)) d = devices[item.serial] - d.app_start("com.tencent.mm") \ No newline at end of file + d.app_start("com.tencent.mm") + diff --git a/tasks/wechat/__init__.py b/tasks/wechat/__init__.py index 1465467..4225205 100644 --- a/tasks/wechat/__init__.py +++ b/tasks/wechat/__init__.py @@ -9,6 +9,8 @@ def click_search_btn(d: Device): selector.click() else: logging.warning("未找到组件") + d.press("back") + click_search_btn(d) def input_search_text(d: Device, text: str): @@ -50,11 +52,38 @@ def input_chat_text(d: Device, text: str): selector.click() -def get_last_chat_text(d: Device): - selector = (d.xpath('//*[@resource-id="com.tencent.mm:id/bn1"]') - .child('//*[@resource-id="com.tencent.mm:id/bkl"]')) - elements = selector.all() - if len(elements) > 0: - last_element = elements[-1] - print(last_element.text) + +def get_friend_text_msgs(d: Device): + selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bp0"]/android.widget.RelativeLayout') + + elements = selector.all() + msg = [] + for item in elements: + # 好友消息 + el = item.elem.xpath("android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.TextView") + if len(el) > 0 : + msg.append(el[0].get("text")) + + return msg + +def get_last_friend_text_msg(d: Device): + msgs = get_friend_text_msgs(d) + return msgs[-1] if len(msgs) > 0 else None + +def get_my_text_msgs(d: Device): + selector = d.xpath('//*[@resource-id="com.tencent.mm:id/bp0"]/android.widget.RelativeLayout') + + elements = selector.all() + msg = [] + for item in elements: + # 自己的消息 + el = item.elem.xpath("android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]/android.widget.LinearLayout[1]android.widget.TextView") + if len(el) > 0 : + msg.append(el[0].get("text")) + + return msg + +def get_last_my_text_msg(d: Device): + msgs = get_friend_text_msgs(d) + return msgs[-1] if len(msgs) > 0 else None \ No newline at end of file