import uiautomator2 as u2
from threading import Thread
import logging
import sys
import os
logger=logging.getLogger("uiautomator2")
logger.setLevel(logging.INFO)
d = u2.connect()"""下载模块"""
class yingyongbao(object):
def __init__(self):self.width, self.height = d.window_size()print(self.width,self.height) def get_path(self):self.basedir = os.path.dirname(os.path.abspath(__file__))def get_screen_path(self):screen_path = os.path.join(self.basedir,'log')if not os.path.exists(screen_path):os.mkdir(screen_path)return screen_pathdef get_error_result(self):error_path = os.path.join(self.basedir, 'screen')if not os.path.exists(error_path):os.mkdir(error_path)return error_pathdef start_yingyongbao(self):try:os.system("python -m uiautomator2 init")file_path=input("请输入需要下载的文本路径:")with open(file_path, 'r') as f:self.package_list = f.readlines()d.shell("pm grant com.tencent.android.qqdownloader android.permission.'POST_NOTIFICATIONS'")d.app_clear("com.tencent.android.qqdownloader")d.app_start("com.tencent.android.qqdownloader", wait=True)d.sleep(8)try:if "应用宝" == d.xpath('//*[@resource-id="android:id/alertTitle"]').get_text():d.xpath("//*[@resource-id='android:id/button1']").click()except:print("点击应用宝权限后未识别")if d(textContains="欢迎使用应用宝").exists(timeout=30):d.xpath('//*[@resource-id="com.tencent.android.qqdownloader:id/ri"]').click()if d(textContains="发送通知吗").exists(timeout=30):d.xpath('//*[@resource-id="com.android.permissioncontroller:id/permission_allow_button"]').click()if d(textContains='一键下载').exists(timeout=30):d.press("back")if d(textContains="秒完游戏").exists(timeout=20):d.press("back")except:logger.info("function:" + sys._getframe().f_code.co_name + ":start_yingyongbao:")def login_yingyongbao(self):try:d(resourceId="com.tencent.android.qqdownloader:id/e6", text="我的").click()if d(text="欢迎登录应用宝").exists(timeout=6):d.xpath('//*[@resource-id="com.tencent.android.qqdownloader:id/uy"]').click()d.xpath('//*[@resource-id="com.tencent.android.qqdownloader:id/ac1"]').click()d(text="下载完成后提示安装").right().click()d.press("back")d(text="下载完成后提示安装").wait_gone()except:logger.info("function:" + sys._getframe().f_code.co_name + ":login_yingyongbao:")def down_app(self):try:for i in range(0, len(self.package_list)):d.shell(f"am start -a android.intent.action.VIEW -d market://details?id={self.package_list[i]} -p com.tencent.android.qqdownloader")try:d(text="下载量").exists(10)d(text="评价").exists(10)assert "下载量" in d(text="下载量").get_text() and "评价" in d(text="评价").get_text()d.click(0.15 * self.width, 0.95 * self.height)d.sleep(3)print(f"{self.package_list[i].strip()}已加入到下载名单")except:assert "找不到数据" in d(resourceId="com.tencent.android.qqdownloader:id/v5").get_text()print(f"网络问题导致{self.package_list[i]}下载失败,检查网络")self.package_list_sceen=self.package_list[i].strip()d.screenshot(f'{self.package_list_sceen}.png')continueexcept:logger.info("function:" + sys._getframe().f_code.co_name + ":yinngyongbao_install:")"""安装模块已写好"""def yingyongbao_install_Permissions(self):d.app_start("com.tencent.android.qqdownloader", wait=True)d.xpath('//*[@text="我的"]').click_exists(timeout=30)if d(text="欢迎登录应用宝").exists(timeout=6):d.xpath('//*[@resource-id="com.tencent.android.qqdownloader:id/uy"]').click()try:d.xpath("//*[@text='下载安装']").wait(20).click()try:d.xpath("//*[@text='一键安装']").wait(20).click()except Exception as result:print(f"{result}\n没有一键安装按钮,检查是否已经下载了安装包")except:logger.info("function:" + sys._getframe().f_code.co_name + ":yingyongbao_install_Permissions:")try:assert "未知应用" in d(resourceId="android:id/message").get_text()d(resourceId="android:id/button1", text="设置").click()d.xpath('//*[@text="允许来自此来源的应用"]').click()d(text="取消").click()d.press("back")d(text="安装未知应用").wait_gone(timeout=5)except:logger.info("function:" + sys._getframe().f_code.co_name + ":yingyongbao_install_Permissions:")
def yingyongbao_install(self):app_all=[]while True:try:assert "安装" in d(textContains="此应用").get_text()if d(text="要安装此应用吗?").exists(timeout=10):app_name=d.xpath("//*[@resource-id='android:id/alertTitle']").get_text()print(f"安装的应用昵称是{app_name}")app_all.append(app_name)d(text="安装").click()d(text="正在安装…").wait(timeout=10)if d(text="完成").exists(timeout=120):d(text="完成").click()except:print(f"总共安装了len{app_all}个应用,分别是{app_all}")break
@staticmethoddef show_menu():print("========================================================== ")print("|| <1>应用宝下载 <2>应用宝安装 <3> ||\n")print("========================================================== ")if __name__ == '__main__':anzhuang=yingyongbao()"""应用宝下载功能"""while True:anzhuang.show_menu()num = int(input("输入你要选择的功能,用数字替代:先下载后执行安装:"))if num == 1:anzhuang.start_yingyongbao()anzhuang.login_yingyongbao()anzhuang.down_app()print("=======================下载执行完毕=========================== ")elif num == 2:"""应用宝安装功能"""anzhuang.yingyongbao_install_Permissions()anzhuang.yingyongbao_install()else:print("输入有误,重新输入")