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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM