diff --git a/main.py b/main.py index 703e4e8..05a0b4f 100644 --- a/main.py +++ b/main.py @@ -1,12 +1,13 @@ import uvicorn from fastapi import FastAPI from pydantic import BaseModel -from typing import Any +from typing import Any, Union import uiautomator2 as u2 import time import random import tasks.douyin +import tasks.wechat app = FastAPI() @@ -104,5 +105,26 @@ async def douyin_task(item: Device): d.xpath("老耗游戏").click() +@app.post("/task/app/wechat/search") +async def wechat_search_task(item: Device): + if item.serial not in devices: + return Response(code=500, msg="设备 {} 不存在".format(item.serial)) + d = devices[item.serial] + tasks.wechat.click_search_btn(d) + + +class WechatSearchInput(Device): + text: str + + +@app.post("/task/app/wechat/search/input") +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] + tasks.wechat.click_search_btn(d) + tasks.wechat.input_search_text(d, item.text) + tasks.wechat.click_search_result(d) + if __name__ == '__main__': uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True) diff --git a/tasks/wechat/__init__.py b/tasks/wechat/__init__.py new file mode 100644 index 0000000..574342c --- /dev/null +++ b/tasks/wechat/__init__.py @@ -0,0 +1,36 @@ +import logging + +from uiautomator2 import Device + + +def click_search_btn(d: Device): + selector = d.xpath('//*[@resource-id="com.tencent.mm:id/h5n"]') + if selector.exists: + selector.click() + else: + logging.warning("未找到组件") + + +def input_search_text(d: Device, text: str): + clear_btn = d.xpath('//*[@resource-id="com.tencent.mm:id/nhn"]') + if clear_btn.exists: + clear_btn.click() + + selector = d.xpath('//*[@resource-id="com.tencent.mm:id/mdd"]') + + if selector.exists: + selector.set_text(text) + else: + logging.warning("未找到组件") + + +def click_search_result(d: Device): + selector = d.xpath('//*[@resource-id="com.tencent.mm:id/kbo"]') + selector.wait(timeout=3) + elements = selector.all() + print(elements) + + if len(elements) > 0: + elements[0].click() + else: + logging.warning("未找到组件")