#coding=utf8
importos,re,time,loggingimportpyautoguifrom 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命令
defa(c):
r=os.popen(c).read()returnr#判断设备是否连接
defcaniuse():
s= a("adb devices").split("\n")if s[1]==‘‘:returnFalseelse:returnTrue#判断wifi是否连接#adb shell getprop wifi.interface 得到返回wlan0
defcanwifi():
s= a("adb shell getprop dhcp.wlan0.result").split("\n")if s[0]==‘ok‘:returnTrueelse:returnFalse#清理关闭所有程序==可能不适用您的手机
defclean():
a("adb shell input keyevent 187") #切换应用
time.sleep(1)
a("adb shell input tap 540 1740") #点击清扫按钮
time.sleep(1)#判断手机电源是否打开
defpoweron():
s= a("adb shell dumpsys power")
s= re.search(r"Display Power: state=ON",s,re.I |re.M)if s ==None:returnFalseelse:returnTrue#启动APP
defrunapp(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 notos.path.isfile(UIFILE):
time.sleep(0.5)
with open(UIFILE, mode=‘r‘, encoding=‘UTF-8‘) as f: #打开文件
data = f.read() #读取文件
regkw = "]+?" + 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 ‘‘
#获取手机当前页面的源代码,并复制到当前脚本所在目录
defui():
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:returnFalsefor i inrange(len(kw)):
kws= kws + kw[i]+‘|‘kws= kws.strip(‘|‘)
ui()#刷新
while notos.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:returnTrueelse:returnFalse#判断用ui()函数获取的ui.xml的源码中是否匹配1个“正则表达式”#用法:uiRegEx("正则表达式")
defuiRegEx(regkw):
ui()#刷新
while notos.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 :returnTrueelse:returnFalse#强制退出应用adb shell am force-stop #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
#签到,要根据个人手机情况微调
defqd():
a("adb shell input tap 939 1820") #点积分
time.sleep(1)#签到积分
@sc.scheduled_job(‘interval‘, seconds=3600*8 ,id=‘dowork‘) #每隔X秒执行一次
defdowork():#开始操作
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状态")returnrunapp(APP)#启动应用
time.sleep(30)#手机休眠前清理应用==================================
clean()
a("adb shell input keyevent 223") #系统休眠
logging.info("完成签到")if __name__ == ‘__main__‘:
dowork()
sc.start()while(1):#保持程序不退出
pass