APP自动化-ATX集成到代码中

发布时间 2023-06-13 19:16:05作者: 琉璃星眸

把atx的框架集成到代码中

把ATX-API的脚本复制到已有项目中

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 # @Time    : 2023/6/13 10:29
  4 # @Author  : gezirui
  5 # @File    : package_atx_api.py
  6 # @Software: PyCharm
  7 from requests import get, post, delete
  8 import random
  9 import time
 10 
 11 
 12 class AtxServer:
 13     def __init__(self, host, token):
 14         self.host = host
 15         self.token = token
 16         self.headers = {"Authorization": f'Bearer {self.token}'}
 17         self.uuid = ""
 18         self.using_device_info = {}
 19 
 20     def get_user(self):
 21         user_data = get(self.host + "/api/v1/user", headers=self.headers).json()
 22 
 23         return user_data
 24 
 25     def get_all_devices(self):
 26         all_devices = get(self.host + "/api/v1/devices", headers=self.headers).json()
 27 
 28         return all_devices
 29 
 30     def get_no_using_devices(self, all_devices):
 31         no_using_devices = []
 32         for i in all_devices["devices"]:
 33             present = i.get("present")
 34             using = i.get("using")
 35             colding = i.get("colding")
 36             if present == True and using == 0 and not colding:
 37                 # print("设备正在空闲状态中")
 38                 udid = i["udid"]
 39                 no_using_devices.append(udid)
 40 
 41         return no_using_devices
 42 
 43     def random_one_device(self, no_using_devices):
 44         self.udid = random.choice(no_using_devices)
 45 
 46         return random.choice(no_using_devices)
 47 
 48     def using_device(self, udid=None):
 49 
 50         if udid:
 51             self.udid = udid
 52 
 53         result = post(self.host + "/api/v1/user/devices",
 54                       headers=self.headers,
 55                       json={"udid": self.udid}).json()
 56 
 57         return result
 58 
 59     def get_using_device_info(self, udid=None):
 60         if udid:
 61             self.udid = udid
 62 
 63         result = get(self.host + "/api/v1/user/devices/" + self.udid,
 64                      headers=self.headers).json()
 65         self.using_device_info = result
 66         return result
 67 
 68     def get_remote_connect_address(self):
 69         sources = self.using_device_info["device"]["sources"]
 70         # print("1:", sources.values)
 71         # print("2:", list(sources.values())[0])
 72         self.remoteConnectAddress = list(sources.values())[0]["remoteConnectAddress"]
 73 
 74     def release_device(self, udid=None):
 75         if udid:
 76             self.udid = udid
 77 
 78         result = delete(self.host + "/api/v1/user/devices/" + self.udid,
 79                         headers=self.headers).json()
 80         return result
 81 
 82 
 83 if __name__ == '__main__':
 84     host_ = "http://localhost:4000"
 85     token_ = "dd7fe77f7b9c400cabd29514a8341a6b"
 86     atx = AtxServer(host=host_, token=token_)
 87     all_devices = atx.get_all_devices()
 88     print("所有设备:", all_devices)
 89     no_using_devices = atx.get_no_using_devices(all_devices)
 90     print("udid列表:",no_using_devices)
 91     atx.random_one_device(no_using_devices)
 92     print("随机设备是:" ,atx.random_one_device(no_using_devices))
 93     atx.using_device()
 94     print("使用设备:",atx.using_device())
 95     atx.get_using_device_info()
 96     print("使用设备详情:",atx.get_using_device_info())
 97     atx.get_remote_connect_address()
 98     print("远程连接地址:", atx.remoteConnectAddress)
 99     time.sleep(5)
100     atx.release_device()
101     print("释放情况:",atx.release_device())
View Code