吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 187|回复: 0
上一主题 下一主题
收起左侧

[其他原创] apexlegendsstatus 自动更新战绩,不需要挂着页面

[复制链接]
跳转到指定楼层
楼主
wishmelz 发表于 2024-4-14 15:05 回帖奖励

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秒差不多。

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-2 09:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表