下载地址:https://www.pan38.com/share.php?code=pvvmX 提取码:6673 【仅供学习】
这个完整方案包含5个模块文件,实现抖音直播间数据采集全流程。主程序处理初始化,监控服务负责实时采集,数据处理模块进行清洗存储,UI模块提供交互控制,工具模块包含通用函数。使用时需Android 7.0+和AutoJS 4.1.1+环境。
/**
* 抖音直播间监控系统 v2.0
* 功能:实时采集评论、用户UID、礼物信息
* 需AutoJS 4.1.1以上版本
*/
const CONFIG = {
ROOM_ID: "", // 通过参数传入
DATA_PATH: "/sdcard/DouyinData/",
INTERVAL: 2500, // 采集间隔(ms)
MAX_HISTORY: 1000, // 最大历史记录
DEBUG: true
};
// 数据存储结构
let liveData = {
meta: {
version: "2.0",
startTime: new Date().getTime(),
device: device.brand + " " + device.model
},
comments: [],
users: new Map(),
gifts: [],
stats: {
commentCount: 0,
uniqueUsers: 0,
giftCount: 0
}
};
// 主入口
function main(args) {
initRuntime();
let roomId = parseArgs(args) || inputRoomId();
if (!roomId) return;
enterLiveRoom(roomId);
startMonitorService();
setupUI();
}
// 初始化运行环境
function initRuntime() {
if (!files.exists(CONFIG.DATA_PATH)) {
files.createWithDirs(CONFIG.DATA_PATH);
}
auto.setWindowFilter(function(window) {
return window.title.indexOf("抖音") >= 0 ||
window.packageName === "com.ss.android.ugc.aweme";
});
events.on("exit", function() {
saveFinalData();
releaseResources();
});
}
* 核心监控服务
* 包含评论采集、用户识别、数据存储
*/
// 启动监控服务
function startMonitorService() {
setInterval(() => {
try {
captureLiveData();
updateStatistics();
autoScrollComments();
if (liveData.comments.length % 50 === 0) {
saveTempData();
}
} catch (e) {
handleError(e);
}
}, CONFIG.INTERVAL);
}
// 捕获直播间数据
function captureLiveData() {
let newComments = captureComments();
let newGifts = captureGifts();
if (newComments.length > 0) {
liveData.comments.push(...newComments);
updateUserDatabase(newComments);
}
if (newGifts.length > 0) {
liveData.gifts.push(...newGifts);
}
}
// 捕获评论内容
function captureComments() {
let result = [];
let commentNodes = findCommentNodes();
commentNodes.forEach(node => {
let comment = parseCommentNode(node);
if (comment && !isDuplicateComment(comment)) {
result.push(comment);
if (CONFIG.DEBUG) {
log("捕获评论: " + comment.user.nickname + ": " + comment.content);
}
}
});
return result;
}
// 解析评论节点
function parseCommentNode(node) {
try {
let parent = node.parent();
let userNode = parent.child(0);
let contentNode = parent.child(1);
return {
timestamp: Date.now(),
user: {
uid: extractUid(userNode.text()),
nickname: userNode.text().replace(/^用户\d+/, "").trim(),
level: extractUserLevel(userNode)
},
content: contentNode.text().trim(),
type: "comment",
raw: node
};
} catch (e) {
return null;
}
}
/**
* 数据处理模块
* 包含数据清洗、分析和存储
*/
// 更新用户数据库
function updateUserDatabase(comments) {
comments.forEach(comment => {
if (!liveData.users.has(comment.user.uid)) {
liveData.users.set(comment.user.uid, {
firstSeen: Date.now(),
lastSeen: Date.now(),
commentCount: 1,
nickname: comment.user.nickname,
level: comment.user.level
});
} else {
let user = liveData.users.get(comment.user.uid);
user.lastSeen = Date.now();
user.commentCount++;
}
});
liveData.stats.uniqueUsers = liveData.users.size;
}
// 保存临时数据
function saveTempData() {
let filename = CONFIG.DATA_PATH + "temp_" +
new Date().format("yyyyMMdd_HHmmss") + ".json";
let dataToSave = {
meta: liveData.meta,
comments: liveData.comments.slice(-CONFIG.MAX_HISTORY),
users: Array.from(liveData.users.values()),
stats: liveData.stats
};
files.write(filename, JSON.stringify(dataToSave));
if (CONFIG.DEBUG) log("临时数据已保存: " + filename);
}
// 最终数据保存
function saveFinalData() {
let filename = CONFIG.DATA_PATH + "final_" +
new Date().format("yyyyMMdd_HHmmss") + ".json";
files.write(filename, JSON.stringify(liveData));
log("监控结束,最终数据已保存: " + filename);
}
/**
* 用户界面控制
* 包含悬浮窗和控制面板
*/
let floatingWindow = null;
// 设置监控UI
function setupUI() {
createFloatingWindow();
registerGestureEvents();
}
// 创建悬浮窗
function createFloatingWindow() {
floatingWindow = floaty.window(
<frame gravity="center">
<vertical padding="8">
<text id="stats" text="监控运行中..." textSize="14"/>
<button id="exportBtn" text="导出数据"/>
<button id="configBtn" text="设置"/>
<button id="closeBtn" text="退出监控"/>
</vertical>
</frame>
);
updateUIStats();
floatingWindow.exportBtn.click(() => {
saveFinalData();
toast("数据已导出");
});
floatingWindow.closeBtn.click(() => {
exit();
});
}
// 更新UI统计信息
function updateUIStats() {
setInterval(() => {
if (floatingWindow) {
let text = `直播间: ${CONFIG.ROOM_ID}\n` +
`评论数: ${liveData.stats.commentCount}\n` +
`用户数: ${liveData.stats.uniqueUsers}\n` +
`运行: ${formatDuration(Date.now() - liveData.meta.startTime)}`;
floatingWindow.stats.setText(text);
}
}, 1000);
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。