本平台帖子推送 js 脚本

const axios = require('axios');
const fs = require('fs');

// 配置
const config = {
  siteUrl: 'https://hhwl.hhwl.top',
  pushToken: '9a7bd105de4842f6a67215fbbc193141', // PushPlus Token
  lastIdFile: '/ql/data/hhwl-last-id.txt', // 存储最新帖子ID的文件
  maxCheck: 5, // 每次最多检查5个递增ID
  invalidKeywords: ['未找到页面', '页面不存在', '该帖子已删除', '无法找到'] // 无效页面关键词
};

// 读取上次保存的最新帖子ID(监控的起点ID)
function getLastPostId() {
  return fs.existsSync(config.lastIdFile) 
    ? fs.readFileSync(config.lastIdFile, 'utf8').trim() 
    : '39449'; // 初始ID(你的示例帖子ID)
}

// 保存最新帖子ID(仅当发现有效新帖时更新)
function saveLastPostId(id) {
  fs.writeFileSync(config.lastIdFile, id);
}

// 推送消息到微信
async function pushToWechat(title, content) {
  try {
    const res = await axios.post('https://www.pushplus.plus/send', {
      token: config.pushToken,
      title: title,
      content: content,
      template: 'txt'
    });
    if (res.data.code === 200) {
      console.log('微信推送成功');
    } else {
      console.error('推送失败:', res.data.msg);
    }
  } catch (e) {
    console.error('推送接口请求失败:', e.message);
  }
}

// 检查指定ID的帖子是否有效(存在且非无效页面)
async function checkPostExists(id) {
  try {
    const url = `${config.siteUrl}/${id}.html`;
    const res = await axios.get(url, {
      headers: {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0.0.0 Safari/537.36'
      },
      timeout: 10000
    });

    // 检查页面内容是否包含无效关键词
    const isInvalid = config.invalidKeywords.some(keyword => 
      res.data.includes(keyword)
    );

    if (isInvalid) {
      console.log(`ID=${id}:页面内容无效,不推送,下次将重新检查`);
      return null;
    }

    // 提取帖子标题(从<title>标签)
    const titleMatch = res.data.match(/<title>(.*?)<\/title>/);
    const title = titleMatch ? titleMatch[1].trim() : '未知标题';
    return { id, url, title };

  } catch (e) {
    // 请求异常(如超时、连接失败),不推送,下次重新检查
    console.log(`ID=${id}:请求异常(${e.message}),不推送,下次将重新检查`);
    return null;
  }
}

// 主逻辑(核心:仅有效新帖才更新最新ID,无效ID不跳过)
async function main() {
  console.log(`开始监控(${new Date().toLocaleString()})`);

  const lastSavedId = getLastPostId();
  const startId = parseInt(lastSavedId, 10);
  const newPosts = [];
  let latestValidId = startId; // 记录本次监控到的最新有效ID(初始为上次保存的ID)

  // 检查递增ID的帖子(从startId+1开始)
  for (let i = 1; i <= config.maxCheck; i++) {
    const currentId = startId + i;
    const postInfo = await checkPostExists(currentId);
    
    if (postInfo) {
      newPosts.push(postInfo);
      console.log(`发现有效新帖:ID=${currentId},标题=${postInfo.title}`);
      // 更新最新有效ID(仅有效帖子才会更新)
      if (currentId > latestValidId) {
        latestValidId = currentId;
      }
    }
    // 无效帖子不更新latestValidId,保持为startId
  }

  // 处理有效新帖(推送并更新最新ID)
  if (newPosts.length > 0) {
    // 构建推送内容
    let content = '发现新帖子:\n\n';
    newPosts.forEach(post => {
      content += `【标题】${post.title}\n`;
      content += `【链接】${post.url}\n\n`;
    });

    await pushToWechat('hhwl网站新帖通知', content);
    saveLastPostId(latestValidId.toString());
    console.log(`已推送${newPosts.length}条新帖,更新最新监控ID为${latestValidId}`);
  } else {
    // 无有效新帖,不更新最新ID,下次仍从startId开始检查
    console.log('未发现有效新帖,最新监控ID保持不变,下次将重新检查');
  }
}

// 执行
main().catch(e => console.error('脚本错误:', e));

 

温馨提示: 本文最后更新于2025-07-23 20:09:54,某些文章具有时效性,若有错误或已失效,请在下方 留言或联系 哗哗资源分享
© 版权声明
THE END
喜欢就支持一下吧
点赞8赞赏 分享