python-uiautomator2/main.py

109 lines
2.7 KiB
Python
Raw Normal View History

2024-08-01 20:39:20 +08:00
import uvicorn
from fastapi import FastAPI
from pydantic import BaseModel
2024-08-01 21:07:17 +08:00
from typing import Any
2024-08-01 18:52:16 +08:00
import uiautomator2 as u2
2024-08-01 20:39:20 +08:00
import time
2024-08-01 18:52:16 +08:00
import random
2024-08-01 21:58:33 +08:00
import tasks.douyin
2024-08-01 20:39:20 +08:00
app = FastAPI()
devices = {
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
}
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
class Device(BaseModel):
serial: str
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
class Response(BaseModel):
code: int = 200
msg: str = "OK"
data: Any = None
2024-08-01 18:52:16 +08:00
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
@app.get("/health", response_model=Response)
def health():
return Response()
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
@app.post("/device/connect", response_model=Response)
def connect_device(item: Device):
try:
d = u2.connect(item.serial)
except Exception as e:
return Response(code=500, msg=str(e))
print(d.info)
print(d.device_info)
devices[item.serial] = d
return Response(data=d.device_info)
@app.post("/device/size", response_model=Response)
def get_device_size(item: Device):
if item.serial not in devices:
return Response(code=500, msg="设备 {} 不存在".format(item.serial))
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
d = devices[item.serial]
width, height = d.window_size()
return Response(data={
"width": width,
"heighth": height,
})
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
# width, height = d.window_size()
# print("width:{}, height:{}".format(width, height))
2024-08-01 18:52:16 +08:00
2024-08-01 20:39:20 +08:00
# print(d.wlan_ip)
# print(d.serial)
2024-08-01 18:52:16 +08:00
2024-08-01 20:39:20 +08:00
# print(d.app_current())
2024-08-01 18:52:16 +08:00
2024-08-01 20:39:20 +08:00
# d.app_start("com.ss.android.ugc.aweme", stop=False)
2024-08-01 21:07:17 +08:00
#
2024-08-01 18:52:16 +08:00
2024-08-01 20:39:20 +08:00
@app.post("/task/app/douyin")
2024-08-01 20:41:23 +08:00
async def douyin_task(item: Device):
2024-08-01 20:39:20 +08:00
if item.serial not in devices:
2024-08-01 20:41:23 +08:00
return Response(code=500, msg="设备 {} 不存在".format(item.serial))
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
d = devices[item.serial]
d.app_start("com.ss.android.ugc.aweme", stop=False)
# d.xpath("关闭").wait(timeout=1)
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
width, height = d.window_size()
random.seed(time.time())
for i in range(10):
2024-08-01 21:58:33 +08:00
start_x = random.randint(0, int(width))
end_x = round(start_x + random.random(), 1)
2024-08-01 21:07:17 +08:00
2024-08-01 21:58:33 +08:00
end_y = random.randint(0, int(height / 10))
start_y = round(end_y * 9.75, 1)
if start_y - end_y < (height / 3):
start_y += round((height / 3), 1)
2024-08-01 21:07:17 +08:00
2024-08-01 20:39:20 +08:00
# d.swipe_ext("up", scale=0.95, duration=0.1)
2024-08-01 21:58:33 +08:00
print(f"({start_x}, {start_y}) => ({end_x}, {end_y})")
d.swipe(start_x, start_y, end_x + random.random(), end_y, duration=0.05 + random.random() * 0.1)
2024-08-01 20:39:20 +08:00
time.sleep(5 + random.random() * 2)
2024-08-01 20:42:18 +08:00
return Response()
2024-08-01 21:07:17 +08:00
2024-08-01 21:58:33 +08:00
@app.post("/task/app/douyin/test")
async def douyin_task(item: Device):
if item.serial not in devices:
return Response(code=500, msg="设备 {} 不存在".format(item.serial))
d = devices[item.serial]
d.app_start("com.ss.android.ugc.aweme", stop=False)
2024-08-01 22:20:29 +08:00
tasks.douyin.click_search_icon(d)
tasks.douyin.input_search_text(d, "老耗")
d.xpath("老耗游戏").click()
2024-08-01 21:58:33 +08:00
2024-08-01 20:39:20 +08:00
if __name__ == '__main__':
2024-08-01 21:07:17 +08:00
uvicorn.run('main:app', host='0.0.0.0', port=8000, reload=True)