apexlegendsstatus 只有打开网页挂着才能更新战绩记录,我经常忘记。
所以,自己使用NodeJs 写了一个简单的脚本
const puppeteer = require('puppeteer');
const fs = require('fs');
const path = require('path');
const request = require('request')
const waitNum = 10 //等待时间
const reloadNum = 160 //定时
const linkUrl = 'https://apexlegendsstatus.com/profile/uid/PC/xxxxxx#S-match-history' // apexlegendsstatus.com的地址
const barkToken = 'xxxR9xxxxxxAvxxxxrC' // bark 的通知地址,留空不触发
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 hours = String(date.getHours()).padStart(2, '0');
const minutes = String(date.getMinutes()).padStart(2, '0');
const seconds = String(date.getSeconds()).padStart(2, '0');
return `${year}-${month}-${day}-${hours}:${minutes}:${seconds}`;
}
async function checkFileCount() {
console.log('----清理文件-----');
const filesDir = path.join(__dirname, 'files');
const files = fs.readdirSync(filesDir);
if (files.length > 200) {
const sortedFiles = files.map(file => ({
file,
mtime: fs.statSync(path.join(filesDir, file)).mtime.getTime()
})).sort((a, b) => a.mtime - b.mtime);
const oldestFile = sortedFiles[0].file;
fs.unlinkSync(path.join(filesDir, oldestFile));
}
}
function updateStatus(status) {
let currentStatus;
try {
currentStatus = fs.readFileSync('apexstatus.txt', 'utf-8');
} catch (error) {
console.error('Error reading file:', error);
return;
}
if (status !== currentStatus) {
console.log('登录状态更新');
try {
// 写入最新状态到文件中
fs.writeFileSync('apexstatus.txt', status, 'utf-8');
if (barkToken == '') {
return
}
if (status.includes('Offline') || currentStatus.includes('Offline')) {
let url = `https://api.day.app/${barkToken}/${encodeURIComponent('Apex登录状态发生更新:' + status)}`
request(url)
}
} catch (error) {
console.error('Error updating file:', error);
}
}
}
async function run() {
async function takeScreenshot() {
const browser = await puppeteer.launch(
{
headless: 'new',
defaultViewport: {
width: 1920, height: 1080
},
args: ['--no-sandbox', '--disable-setuid-sandbox']
}
);
const page = await browser.newPage(); // 实例化
console.log('----进入页面-----');
await page.goto(linkUrl);
// await page.waitForNavigation({ waitUntil: 'networkidle0' });
console.log(`开始等待${waitNum}秒`);
await new Promise(resolve => setTimeout(resolve, 1000 * waitNum));
const elementContent = await page.evaluate(() => {
return document.querySelector('body > main > div:nth-child(1) > div.container-fluid > div > div.col-md-3 > div:nth-child(1) > div.center-element > p').textContent;
});
updateStatus(elementContent);
const date = new Date();
const formattedDate = formatDate(date);
console.log(formattedDate);
const filePath = path.join(__dirname, 'files', `${formattedDate}.png`);
console.log('截图');
await page.screenshot({ path: filePath });
await browser.close()
await checkFileCount();
}
setInterval(takeScreenshot, 1000 * reloadNum);
await takeScreenshot();
}
run()
使用PM2持久化运行就行了。
同级目录需要建一个文件:apexstatus.txt
还有一个文件夹:files
此脚本刷新频率和网站的自动刷新频率180秒差不多。
|