好,不啰嗦,上代码:
(PS:为了丰富执行逻辑,本人在练习时,有添加了几个app:请仅限于技术研究使用)
#coding:utf-8
import os
import threading
import re
import time
import random
stepOut1 = ""
stepOut2 = ""
stepOut3 = ""
curAppType = "7"
AppTypeList = ["1", "2", "3", "5", "7"]
startReadTime = int(time.time())
MaxReadTimeInOnePlatform = 60*20 # 秒
curAppStartActivity = ""
curAppMainActivity = ""
curAppMainReadActivity = ""
def setAppInfoByType():
global curAppMainActivity
global curAppMainReadActivity
global curAppStartActivity
global curAppType
if curAppType == "2": # 聚看点
curAppStartActivity = "com.xiangzi.jukandian/com.xiangzi.jukandian.activity.V2WelcomeActivity"
curAppMainActivity = "com.xiangzi.jukandian/com.xiangzi.jukandian.activity.MainActivity"
curAppMainReadActivity = ["com.xiangzi.jukandian/com.xiangzi.jukandian.activity.WebViewActivity"]
if curAppType == "3": # 微鲤看看
curAppStartActivity = "cn.weli.story/cn.etouch.ecalendar.MainActivity"
curAppMainActivity = "cn.weli.story/cn.etouch.ecalendar.MainActivity"
curAppMainReadActivity = ["cn.weli.story/cn.etouch.ecalendar.tools.life.LifeDetailsActivity"]
if curAppType == "5": # 搜狐资讯
curAppStartActivity = "com.sohu.infonews/com.sohu.quicknews.splashModel.activity.SplashActivity"
curAppMainActivity = "com.sohu.infonews/com.sohu.quicknews.homeModel.activity.HomeActivity"
curAppMainReadActivity = ["com.sohu.infonews/com.sohu.quicknews.articleModel.activity.DetailActivity", "com.sohu.infonews/com.sohu.quicknews.articleModel.activity.VideoDetailActivity"]
if curAppType == "7": # 牛牛咨询
curAppStartActivity = "com.huolea.bull/com.huolea.bull.page.other.activity.SplashActivity"
curAppMainActivity = "com.huolea.bull/com.huolea.bull.page.other.activity.MainActivity"
curAppMainReadActivity = ["com.huolea.bull/com.huolea.bull.page.news.activity.NewsDetailActivity"]
if curAppType == "1" or curAppType == "": # 趣头条
curAppStartActivity = "com.jifen.qukan/com.jifen.qkbase.main.MainActivity"
curAppMainActivity = "com.jifen.qukan/com.jifen.qkbase.main.MainActivity"
curAppMainReadActivity = ["com.jifen.qukan/com.jifen.qukan.content.newsdetail.news.NewsDetailNewActivity", "com.jifen.qukan/com.jifen.qukan.content.newsdetail.video.VideoNewsDetailNewActivity"]
if curAppMainActivity == "":
curAppType = AppTypeList[0]
def startActivity():
os.system('adb shell am start -n ' + curAppStartActivity)
def isActivityInList(activity_des):
for activity in curAppMainReadActivity:
if activity_des.find(activity) != -1:
return True
return False
def toutiaoRead(device_name):
print(device_name)
setAppInfoByType()
startActivity()
time.sleep(10)
while True:
curTime = int(time.time())
global startReadTime
global curAppType
if ((curTime - startReadTime) > MaxReadTimeInOnePlatform):
print("切换app-------------")
startReadTime = curTime
print("关闭当前app-------------")
os.popen("adb shell input keyevent 4")
time.sleep(0.1)
os.popen("adb shell input keyevent 4")
time.sleep(0.1)
os.popen("adb shell input keyevent 4")
if AppTypeList.index(curAppType) == len(AppTypeList) - 1:
curAppType = AppTypeList[0]
else:
curAppType = AppTypeList[AppTypeList.index(curAppType) + 1]
setAppInfoByType()
if curAppType == "":
print("error")
return
print("切换中-------------")
startActivity()
time.sleep(10)
global stepOut1
stepOut1 = os.popen("adb shell dumpsys window windows |grep 'Current'").read()
if curAppMainActivity in stepOut1:
os.popen("adb shell input swipe 200 800 200 400 100").read()
time.sleep(2+random.uniform(0, 2))
os.popen("adb shell input tap 200 700")
time.sleep(4+random.uniform(0, 2))
global stepOut2
stepOut2 = os.popen("adb shell dumpsys window windows |grep 'Current'").read()
if isActivityInList(stepOut2):
readPageContentCount = 0
while readPageContentCount < 7:
randomTime = 5;
if readPageContentCount == 0:
randomTime = 3
os.system("adb shell input swipe 200 650 200 400 100")
time.sleep(randomTime)
readPageContentCount = readPageContentCount + 1
global stepOut3
stepOut3 = os.popen("adb shell dumpsys window windows |grep 'Current'").read()
exitCount = 0
while (stepOut3.find(curAppMainActivity) == -1) or (stepOut3.find(curAppStartActivity) == -1):
exitCount = exitCount + 100
os.popen("adb shell input keyevent 4")
time.sleep(2)
stepOut3 = os.popen("adb shell dumpsys window windows |grep 'Current'").read()
if exitCount > 5:
break
if stepOut3.find(curAppMainActivity) == -1: # 有可能是意外退出
print("重新唤醒中-------------")
startActivity()
time.sleep(10)
pass
out = os.popen("adb devices").read()
device_list = re.sub('\tdevice', '', out[25:]).strip().split("\n")
for device in device_list:
if device:
toutiaoRead(device)
实在是非常之抱歉,本人写代码不怎么习惯于书写注释。除非公司特别强调或者逻辑非常难以理解的时候,才会写点注释,如果这里有大人看不太懂的地方,请联系我QQ: 467598557,我可以做讲解,欢迎交流
附: 要跑本段代码,请保证你的USB调试模式已经开启,且手机上已经装了这些app,如果您不想安装这些app,那就把上面部分代码修改一下,仅安装您想安装的或者已经安装的就可以了。
QQ: 467598557