/**
|
* description 进入消息模块
|
*/
|
let packageName = 'com.ss.android.ugc.aweme';
|
let tenantId = ""
|
//toast扩展参数
|
let toastExtra = {
|
"x":(device.getScreenWidth() - 800) / 2,
|
"y":(device.getScreenHeight() - 200) / 2,
|
"textColor":"#ffffff",
|
"duration":30000,
|
"width":800,
|
"height":200,
|
}
|
/**
|
* description 脚本入口
|
*/
|
run()
|
/**
|
* description 跳转至消息页面
|
*/
|
toMessage(packageName);
|
|
/**
|
* description 唤醒设备
|
*/
|
function run() {
|
if (!isAccMode()) {
|
toast('非无障碍模式,无法执行脚本', toastExtra);
|
randomSleep("非无障碍模式,无法执行脚本", "", "", "", "", "");
|
exit();
|
}
|
//自动化服务是否正常
|
if (!autoServiceStart(2)) {
|
toast('自动化服务启动失败,无法执行脚本', toastExtra);
|
randomSleep("自动化服务启动失败,无法执行脚本", "", "", "", "", "");
|
exit();
|
}
|
daemonEnv(true); //守护自动化环境
|
randomSleep("脚本开始运行", "脚本运行", "", "", "", "");
|
//保持设备唤醒
|
importClass(android.os.PowerManager);
|
device.keepAwake(PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP);
|
//保持屏幕唤醒
|
device.keepScreenOn();
|
sleep(1000);
|
//息屏设备上滑解锁
|
let lock = id("com.android.systemui:id/notification_stack_scroller").getOneNodeInfo(5000);
|
if (lock) {
|
let screenWidth = device.getScreenWidth();
|
let screenHeight = device.getScreenHeight();
|
let startX = screenWidth / 2;
|
let startY = screenHeight - 200;
|
let endX = screenWidth / 2;
|
let endY = 50;
|
let duration = 2000;
|
swipeToPoint(startX, startY, endX, endY, duration);
|
}
|
//脚本停止监听
|
setStopCallback(function () {
|
device.cancelKeepingAwake(); //取消保持唤醒状态
|
shell.stopApp(packageName); //关闭抖音
|
home();
|
exit()
|
});
|
//脚本异常停止监听
|
setExceptionCallback(function (msg) {
|
device.cancelKeepingAwake(); //取消保持唤醒状态
|
randomSleep('脚本异常停止消息:' + msg, "脚本停止", "", "", "", "");
|
});
|
launch(packageName, 5000, 2); //打开抖音
|
}
|
|
|
|
/**
|
* description 通过包名启动应用
|
* @param packageName {string} : 应用包名
|
* @param delay {number} : 启动后等待时长(毫秒)
|
* @param startNum {number} : 重试次数
|
* @return {boolean} : 返回是否成功
|
*/
|
function launch(packageName, delay, startNum) {
|
let result = false;
|
//判断应用的包名是否存在
|
if (utils.isAppExist(packageName)) {
|
//判断应用是否在前台
|
if (getRunningPkg() == null) {
|
toast('请开启辅助功能权限');
|
exit();
|
} else if (getRunningPkg() === packageName) {
|
logd('应用已经在前台');
|
result = true;
|
} else {
|
logd('启动应用...');
|
let alert_selectors = text("允许");
|
let num = 0;
|
//判断应用是否启动成功
|
while (num < startNum) {
|
if (getRunningPkg() === packageName) {
|
logd('启动成功');
|
result = true;
|
break;
|
} else if (has(alert_selectors)) {
|
//初次打开应用 可能会弹出是否允许打开的弹窗
|
let node = alert_selectors.getOneNodeInfo(1000);
|
if (node) {
|
node.click();
|
}
|
} else {
|
utils.openApp(packageName);
|
sleep(delay);
|
num++;
|
}
|
}
|
}
|
alert(); //应用启动后处理弹窗
|
getDevice()
|
randomSleep("应用启动", "应用启动", "", "", "", "")
|
operaLog(1);
|
} else {
|
toast('应用未安装');
|
exit();
|
}
|
return result;
|
}
|
|
function toMessage(packageName) {
|
try {
|
//定位到首页
|
execSync(function () {
|
isHome();
|
}, 1000);
|
let message_icon = text("消息").getOneNodeInfo(randomSleep("准备点击消息按钮", "", "", "", "", ""));
|
//寻找消息图标并点击
|
if (message_icon.click()) {
|
operaLog(2);
|
randomSleep("点击消息按钮,进入消息页面", "", "", "", "", "")
|
let interact_message = text("互动消息").getOneNodeInfo(randomSleep("准备点击互动消息按钮", "", "", "", "", ""))
|
//寻找互动消息图标并点击
|
if (interact_message.click()) {
|
operaLog(3)
|
randomSleep("点击互动消息按钮,进入互动消息页面", "", "", "", "", "")
|
let grabRequestList = [];
|
var node = clz("androidx.recyclerview.widget.RecyclerView").getOneNodeInfo(2000);
|
var result = node.allChildren();
|
if (result != null) {
|
for (let i = 0; i < result.length; i++) {
|
let value = result[i];
|
let desc = value.desc;
|
if (desc == '' || desc == null || desc == 'undefined' || desc == undefined) {
|
break;
|
}
|
if (!value.desc.includes("主页访客")) {
|
let grabRequest = {
|
nickname: '',
|
relation: '',
|
comment: '',
|
commentTime: ''
|
}
|
let split = value.desc.split(",");
|
if (split.length >= 3) {
|
let parts = split[1].trim().split(" ");
|
let lastPart = parts.pop();
|
let time = handleTime(lastPart);
|
let nicknameAndRelation = split[0].split(",");
|
grabRequest.nickname = nicknameAndRelation[0].trim();
|
let relationArr = new Array(["朋友", "作者", "粉丝", "你的关注", "在线"])
|
let relation = '';
|
for (let keyword of relationArr) {
|
if (nicknameAndRelation[1].trim().includes(keyword)) {
|
relation = '路人'
|
} else {
|
relation = nicknameAndRelation[1].trim();
|
}
|
}
|
grabRequest.relation = relation;
|
grabRequest.comment = handleComment(split[1].trim(), lastPart);
|
grabRequest.commentTime = time;
|
grabRequestList.push(grabRequest);
|
} else if (split.length === 2) {
|
let parts = split[0].trim().split(" ");
|
let lastPart = parts.pop();
|
let time = handleTime(lastPart);
|
grabRequest.nickname = split[0].trim().split(",")[0];
|
grabRequest.relation = '路人';
|
grabRequest.comment = handleComment(split[0].trim(), lastPart);
|
grabRequest.commentTime = time;
|
grabRequestList.push(grabRequest);
|
}
|
}
|
}
|
save(grabRequestList, packageName);
|
randomSleep("数据添加成功!", "", "", "", "", "")
|
operaLog(4)
|
} else {
|
logd("暂无互动消息");
|
}
|
// 定位到首页
|
isHome();
|
randomSleep("脚本运行结束", "脚本运行结束", "", "", "", "");
|
}
|
}
|
} catch (e) {
|
logd(e)
|
randomSleep("脚本异常", "脚本异常", "", "", "", "");
|
isHome()
|
}
|
}
|
|
function handleComment(comment, lastPart) {
|
return comment.substring(0, comment.length - lastPart.length)
|
}
|
|
function handleTime(lastPart) {
|
let time = '';
|
if (lastPart.includes("刚刚") || lastPart.includes("分钟前") || lastPart.includes("小时前")) {
|
// 当天
|
time = formatDate(getCurrentDate());
|
} else if (lastPart.includes("昨天")) {
|
// 昨天
|
time = formatDate(getDateBeforeDays(getCurrentDate(), 1));
|
} else if (lastPart.includes("天前")) {
|
// 几天前
|
var array = lastPart.split("天前");
|
time = formatDate(getDateBeforeDays(getCurrentDate(), parseInt(array[0], 10)));
|
} else if (lastPart.split("-").length === 2) {
|
// 月-日
|
let newTime = new Date().getFullYear() + "-" + lastPart
|
time = formatDateString(newTime)
|
} else if (lastPart.split("-").length === 3) {
|
// 年-月-日
|
time = formatDateString(lastPart)
|
}
|
return time;
|
}
|
|
function save(grabRequestList, packageName) {
|
let paltform = '';
|
let uniqueNo = '';
|
if ('com.ss.android.ugc.aweme'.equals(packageName)) {
|
// 斗音
|
paltform = 'DY'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
} else if ('com.xingin.xhs'.equals(packageName)) {
|
// 小红书
|
paltform = 'XHS'
|
uniqueNo = ecloud.getDeviceNo().substring(4)
|
} else if ('com.smile.gifmaker'.equals(packageName)) {
|
// 快手
|
paltform = 'KS'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
}
|
for (let i = 0; i < grabRequestList.length; i++) {
|
let add = {
|
"tableNameEn": "grab_comment",
|
"columns": {
|
"task_id": ecloud.getTaskInfo().taskId,
|
"task_name": ecloud.getTaskInfo().taskName,
|
"device_no": ecloud.getDeviceNo(),
|
"nickname": grabRequestList[i].nickname,
|
"unique_no": uniqueNo,
|
"content": grabRequestList[i].comment,
|
"comment_time": grabRequestList[i].commentTime,
|
"platform": paltform,
|
"relation": grabRequestList[i].relation,
|
"tenant_id": tenantId,
|
}
|
}
|
ecloud.dynamicAdd(add)
|
randomSleep("数据新增成功", "", "", "", "", "")
|
}
|
}
|
|
/**
|
* 记录设备操作记录日志
|
*/
|
function deviceOperaLog(operateType, logDetail, intervals, tarPostName, tarUniqueNo, tarComment, tarNickname,time) {
|
let paltform = '';
|
let uniqueNo = '';
|
if ('com.ss.android.ugc.aweme'.equals(packageName)) {
|
// 斗音
|
paltform = 'DY'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
} else if ('com.xingin.xhs'.equals(packageName)) {
|
// 小红书
|
paltform = 'XHS'
|
uniqueNo = ecloud.getDeviceNo().substring(4)
|
} else if ('com.smile.gifmaker'.equals(packageName)) {
|
// 快手
|
paltform = 'KS'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
}
|
let add = {
|
"tableNameEn": "device_log",
|
"columns": {
|
"unique_no": uniqueNo,
|
"platform": paltform,
|
"device_no": ecloud.getDeviceNo(),
|
"task_name": ecloud.getTaskInfo().taskName,
|
"log_detail": logDetail,
|
"operate_type": operateType,
|
"intervals": intervals + "ms",
|
"operate_time": getDate(),
|
"tar_post_name": tarPostName,
|
"tar_unique_no": tarUniqueNo,
|
"tar_comment": tarComment,
|
"tar_nickname": tarNickname,
|
"task_id": ecloud.getTaskInfo().taskId,
|
"tenant_id": tenantId,
|
"serial_num": time
|
}
|
}
|
ecloud.dynamicAdd(add)
|
}
|
|
/**
|
* description 获取当前时间
|
* @return {string} : 返回时间
|
*/
|
function getDate() {
|
let date = new Date();
|
let sign2 = ":";
|
let year = date.getFullYear() // 年
|
let month = date.getMonth() + 1; // 月
|
let day = date.getDate(); // 日
|
let hour = date.getHours(); // 时
|
let minutes = date.getMinutes(); // 分
|
let seconds = date.getSeconds() //秒
|
let weekArr = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期天'];
|
let week = weekArr[date.getDay()];
|
// 给一位数的数据前面加 “0”
|
if (month >= 1 && month <= 9) {
|
month = "0" + month;
|
}
|
if (day >= 0 && day <= 9) {
|
day = "0" + day;
|
}
|
if (hour >= 0 && hour <= 9) {
|
hour = "0" + hour;
|
}
|
if (minutes >= 0 && minutes <= 9) {
|
minutes = "0" + minutes;
|
}
|
if (seconds >= 0 && seconds <= 9) {
|
seconds = "0" + seconds;
|
}
|
return year + "-" + month + "-" + day + " " + hour + sign2 + minutes + sign2 + seconds;
|
}
|
|
/**
|
* 记录埋点数据
|
*/
|
function operaLog(num) {
|
let paltform = '';
|
let operateType = '';
|
let uniqueNo = '';
|
if (num === 1) {
|
operateType = '应用启动'
|
} else if (num === 2) {
|
operateType = '点击消息'
|
} else if (num === 3) {
|
operateType = '点击互动消息'
|
} else if (num === 4) {
|
operateType = '脚本运行结束'
|
}
|
if ('com.ss.android.ugc.aweme'.equals(packageName)) {
|
// 斗音
|
paltform = 'DY'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
} else if ('com.xingin.xhs'.equals(packageName)) {
|
// 小红书
|
paltform = 'XHS'
|
uniqueNo = ecloud.getDeviceNo().substring(4)
|
} else if ('com.smile.gifmaker'.equals(packageName)) {
|
// 快手
|
paltform = 'KS'
|
uniqueNo = ecloud.getDeviceNo().substring(3)
|
}
|
let add = {
|
"tableNameEn": "trace",
|
"columns": {
|
"device_no": ecloud.getDeviceNo(),
|
"platform": paltform,
|
"unique_no": uniqueNo,
|
"post_name": '',
|
"nickname": '',
|
"keyword": '',
|
"operate_type": operateType,
|
"operate_time": getDate(),
|
"task_id": ecloud.getTaskInfo().taskId,
|
"task_name": ecloud.getTaskInfo().taskName,
|
"tenant_id": tenantId,
|
}
|
}
|
ecloud.dynamicAdd(add)
|
}
|
|
/**
|
* description 异步执行线程 处理弹窗
|
* @return {boolean} : 返回是否成功
|
*/
|
function alert() {
|
thread.execAsync(function () {
|
logd('启动线程,查找弹窗');
|
let btnText = ["下次再说",
|
"以后再说",
|
"关闭",
|
"允许",
|
"暂不",
|
"暂时不要",
|
"我知道了",
|
"知道了",
|
"取消"];
|
while (true) {
|
for (let i = 0; i < btnText.length; i++) {
|
let node = text(btnText[i]).getOneNodeInfo(1000);
|
if (node) {
|
node.click();
|
logd('弹窗已处理');
|
randomSleep("弹窗处理完毕", "处理弹窗", "", "", "", "")
|
}
|
}
|
}
|
});
|
return true;
|
}
|
|
// 获取当前日期
|
function getCurrentDate() {
|
return new Date();
|
}
|
|
// 格式化日期为年-月-日
|
function formatDate(date) {
|
const year = date.getFullYear();
|
const month = String(date.getMonth() + 1).padStart(2, '0');
|
const day = String(date.getDate()).padStart(2, '0');
|
const format = 'yyyy-MM-dd'
|
return format
|
.replace('yyyy', year)
|
.replace('MM', month)
|
.replace('dd', day);
|
}
|
|
// 格式化日期字符串的函数
|
function formatDateString(dateString) {
|
// 使用正则表达式分割日期字符串
|
const parts = dateString.match(/^(\d{4})-(\d{1,2})-(\d{1,2})$/);
|
|
if (parts) {
|
// 提取年份、月份和日期
|
const year = parts[1];
|
const month = String(parseInt(parts[2], 10)).padStart(2, '0');
|
const day = String(parseInt(parts[3], 10)).padStart(2, '0');
|
|
// 拼接成新的日期字符串
|
return year + "-" + month + "-" + day;
|
} else {
|
// 如果日期字符串格式不正确,返回一个错误消息或默认值
|
return 'Invalid date string format';
|
}
|
}
|
|
// 获取指定天数后的日期
|
function getDateAfterDays(date, days) {
|
const newDate = new Date(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
return newDate;
|
}
|
|
// 获取指定天数前的日期
|
function getDateBeforeDays(date, days) {
|
return getDateAfterDays(date, -days);
|
}
|
|
/**
|
* description 定位到首页
|
* @return {boolean} : 返回是否成功
|
*/
|
function isHome() {
|
let result = false;
|
while (true) {
|
var home_selectors = text("首页").getOneNodeInfo(3000); //首页按钮
|
var message_selectors = text("消息").getOneNodeInfo(3000); //消息按钮
|
//判断是否在首页
|
if (home_selectors && message_selectors) {
|
var follow_selectors = text("关注"); //关注按钮
|
var recommend_selectors = text("推荐"); //推荐按钮
|
if (has(follow_selectors) && has(recommend_selectors)) {
|
toast('已定位首页');
|
randomSleep("定位到首页", "定位到首页", "", "", "", "");
|
result = true;
|
break;
|
} else {
|
home_selectors.click();
|
sleep(3000);
|
}
|
} else {
|
toast('返回首页...');
|
//返回上一页
|
go_back(1);
|
}
|
}
|
return result;
|
}
|
|
/**
|
* description 自定义返回函数
|
* @param page {number} : 返回页数
|
*/
|
function go_back(page) {
|
let count = 0;
|
while (count < page) {
|
back();
|
count++;
|
randomSleep("返回上一页", "", "", "", "", "");
|
}
|
}
|
|
/**
|
* description 随机暂停时间
|
*/
|
function randomSleep(log, opeType, tarPostName, tarUniqueNo, tarComment, tarNickname) {
|
let min = Math.ceil(30);
|
let max = Math.floor(60);
|
let times = (Math.floor(Math.random() * (max - min + 1)) + min) * 100;
|
logd(log + ",停留" + times + "ms");
|
// 记录设备操作日志
|
if (opeType === "脚本启动" || opeType === "脚本运行结束") {
|
let serialNum = time()
|
deviceOperaLog(opeType, log + ",停留" + times + "ms", times, tarPostName, tarUniqueNo, tarComment, tarNickname, serialNum)
|
}else {
|
deviceOperaLog(opeType, log + ",停留" + times + "ms", times, tarPostName, tarUniqueNo, tarComment, tarNickname,null)
|
}
|
sleep(times);
|
return times;
|
}
|
|
/**
|
* description 随机暂停时间
|
*/
|
function randomSleepDetail(log) {
|
let min = Math.ceil(3);
|
let max = Math.floor(6);
|
let num = Math.floor(Math.random() * (max - min + 1)) + min
|
let time = num * 1000;
|
sleep(time);
|
return log + ",停留" + num + "秒";
|
}
|
|
/**
|
* 获取设备信息
|
*/
|
function getDevice() {
|
var d = ecloud.getTaskInfo()
|
let taskId = d.taskId
|
taskId = parseInt(taskId,10)
|
var url = "http://47.120.7.97:8098/openapi/getTask";
|
var pa = {
|
"dataSecret": "test123"
|
};
|
var x = http.postJSON(url, pa, 10 * 1000);
|
x = JSON.parse(x)
|
for (let i = 0; i < x.length; i++) {
|
if (x[i].taskId === taskId) {
|
tenantId = x[i].tenantId
|
}
|
}
|
}
|
|
/**
|
* description 启动自动化
|
* @return {boolean} : 返回自动化服务结果
|
*/
|
function autoServiceStart(time) {
|
for (let i = 0; i < time; i++) {
|
if (isServiceOk()) {
|
return true;
|
}
|
let started = startEnv();
|
randomSleep("第' + (i + 1) + '次启动服务结果: " + started, "", "", "", "", "");
|
if (isServiceOk()) {
|
return true;
|
}
|
}
|
return isServiceOk();
|
}
|