python +adb控制手机自动化签到
将之前用go写的代码改为python,这种应用还是用python方便
#coding=utf8 import os,re,time,logging import pyautogui from apscheduler.schedulers.background import BackgroundScheduler #后台运行任务计划 sc = BackgroundScheduler() UIFILE = "ui.xml" #如何找APP的值,请看runapp()函数注释 APP = r"com.XXX/XXXActivity" logging.basicConfig(filename="adbwork.log",filemode="w",format="%(asctime)s-%(name)s-%(levelname)s-%(message)s",level=logging.INFO) #执行cmd命令 def a(c): r=os.popen(c).read() return r #判断设备是否连接 def caniuse(): s = a("adb devices").split("\n") if s[1]=='': return False else: return True #判断wifi是否连接 #adb shell getprop wifi.interface 得到返回wlan0 def canwifi(): s = a("adb shell getprop dhcp.wlan0.result").split("\n") if s[0]=='ok': return True else: return False #清理关闭所有程序==可能不适用您的手机 def clean(): a("adb shell input keyevent 187") #切换应用 time.sleep(1) a("adb shell input tap 540 1740") #点击清扫按钮 time.sleep(1) #判断手机电源是否打开 def poweron(): s = a("adb shell dumpsys power") s = re.search(r"Display Power: state=ON",s,re.I | re.M) if s == None: return False else: return True #启动APP def runapp(app): #获取所有package:adb shell pm list packages 查看所有包名 #获取当前package和activity:首先打开APP,然后运行:adb shell dumpsys window | findstr mCurrentFocus #获取package和activity:1、首先运行:adb shell dumpsys package 用上一行命令获取的package名 #2、然后在输入结果中找 Activity Resolver Table: ... Non-Data Actions:下面第2行, #类似1960b60b com.baidu.searchbox/com.baidu.android.pushservice.PushPatchMessageReceiver的 #用作本函数的参数,以启动APP a("adb shell am start "+ app) def Tap(kw,ordinal=0): ui()#刷新 while not os.path.isfile(UIFILE): time.sleep(0.5) with open(UIFILE, mode='r', encoding='UTF-8') as f: # 打开文件 data = f.read() # 读取文件 regkw = "<node.[^>]+?" + kw + ".[^>]+?\[(\d+),(\d+)\]\[(\d+),(\d+)\].+?>" a = re.findall(regkw, data) x1 = int(a[ordinal][0]) y1 = int(a[ordinal][1]) x2 = int(a[ordinal][2]) y2 = int(a[ordinal][3]) xx = int((x2-x1)/2 + x1) yy = int((y2-y1)/2 + y1) cmd = "adb shell input tap " + str(xx) +" " + str(yy) r=os.popen(cmd).read() return '' #获取手机当前页面的源代码,并复制到当前脚本所在目录 def ui(): a(r"cmd /c del ui.xml -y")#删除当前目录下旧的ui.xml(如果有的话) a("adb shell rm /sdcard/ui.xml")#删除手机中旧的的ui.xml(如果有的话) a("adb shell uiautomator dump /sdcard/ui.xml") #重新dump time.sleep(0)#等待保存到手机卡 a("adb pull /sdcard/ui.xml .") time.sleep(0)#等待下载到电脑 #判断用ui()函数获取的ui.xml的源码中是否包括1个或多个关键字(不能用正则表达式) #用法:uiContains("关键字1","关键字n") def uiContains(*kw): numkw=len(kw) kws='' if numkw<1: return False for i in range(len(kw)): kws = kws + kw[i]+'|' kws = kws.strip('|') ui()#刷新 while not os.path.isfile(UIFILE): time.sleep(0.5) with open(UIFILE, mode='r', encoding='UTF-8') as f: # 打开文件 data = f.read() # 读取文件 a = re.findall(kws, data) numkwfind = len(set(a)) if numkw == numkwfind: return True else: return False #判断用ui()函数获取的ui.xml的源码中是否匹配1个“正则表达式” #用法:uiRegEx("正则表达式") def uiRegEx(regkw): ui()#刷新 while not os.path.isfile(UIFILE): time.sleep(0.5) with open(UIFILE, mode='r', encoding='UTF-8') as f: # 打开文件 data = f.read() # 读取文件 a = re.findall(regkw, data) print(a) num = len(a) print(num) if num > 0 : return True else: return False #强制退出应用adb shell am force-stop <package name> #a("adb shell am force-stop XXXX") #点击:adb shell input tap 939 1820 #滑动:adb shell input swipe 451 1183 490 321 #长按:将滑动距离缩小并加延时1秒 adb shell input swipe 500 1500 500 1500 1000 #模拟按键:adb shell input keyevent 26 # 常用keycode #HOME键3 返回键4 电源键26 #切换应用187 点亮屏幕224 系统休眠223 #KEYCODE_0表示数字0 KEYCODE_ENTER KEYCODE_TAB #签到,要根据个人手机情况微调 def qd(): a("adb shell input tap 939 1820") #点积分 time.sleep(1) #签到积分 @sc.scheduled_job('interval', seconds=3600*8 ,id='dowork') # 每隔X秒执行一次 def dowork(): #开始操作 logging.info("开始签到") a("adb shell input keyevent 224") #点亮手机屏幕 time.sleep(1) a("adb shell input swipe 500 1500 500 400")#向上滑动 time.sleep(1) a("adb shell input keyevent 3") #HOME if caniuse() == False:#如果在手机点亮前检查,经常是连接不上 pyautogui.alert("请检查手机连接状态") return if canwifi() == False:#如果在手机点亮前检查,经常是连接不上 pyautogui.alert("请检查wifi状态") return runapp(APP)#启动应用 time.sleep(30) #手机休眠前清理应用================================== clean() a("adb shell input keyevent 223") #系统休眠 logging.info("完成签到") if __name__ == '__main__': dowork() sc.start() while(1):#保持程序不退出 pass