rss-monitor

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

RSS Monitor

RSS监控

Track RSS and Atom feeds for new content.
追踪RSS和Atom订阅源的新内容。

Installation

安装

bash
npm install feedparser
bash
npm install feedparser

Quick Start

快速开始

Basic Feed Fetch

基础订阅源获取

javascript
const { fetchFeed } = require('./rss-monitor');

const items = await fetchFeed('https://example.com/feed.xml');
console.log(items); // Array of feed items
javascript
const { fetchFeed } = require('./rss-monitor');

const items = await fetchFeed('https://example.com/feed.xml');
console.log(items); // Array of feed items

Track Multiple Feeds

追踪多个订阅源

javascript
const { trackFeeds } = require('./rss-monitor');

const feeds = [
  { url: 'https://blog1.com/rss', name: 'Blog 1' },
  { url: 'https://blog2.com/atom.xml', name: 'Blog 2' }
];

const results = await trackFeeds(feeds);
console.log(results);
javascript
const { trackFeeds } = require('./rss-monitor');

const feeds = [
  { url: 'https://blog1.com/rss', name: 'Blog 1' },
  { url: 'https://blog2.com/atom.xml', name: 'Blog 2' }
];

const results = await trackFeeds(feeds);
console.log(results);

Check for New Items

检查新条目

javascript
const { checkForNewItems } = require('./rss-monitor');

const lastCheck = new Date('2024-01-01T00:00:00Z');
const newItems = await checkForNewItems('https://example.com/feed.xml', lastCheck);
console.log(`Found ${newItems.length} new items`);
javascript
const { checkForNewItems } = require('./rss-monitor');

const lastCheck = new Date('2024-01-01T00:00:00Z');
const newItems = await checkForNewItems('https://example.com/feed.xml', lastCheck);
console.log(`Found ${newItems.length} new items`);

Common Operations

常用操作

Fetch Latest Items

获取最新条目

javascript
const items = await fetchFeed('https://xkcd.com/atom.xml', { limit: 10 });
javascript
const items = await fetchFeed('https://xkcd.com/atom.xml', { limit: 10 });

Get Feed Metadata

获取订阅源元数据

javascript
const feed = await fetchFeedMetadata('https://example.com/feed.xml');
console.log(feed.title, feed.link, feed.description);
javascript
const feed = await fetchFeedMetadata('https://example.com/feed.xml');
console.log(feed.title, feed.link, feed.description);

Parse Feed with Options

带选项解析订阅源

javascript
const items = await fetchFeed('https://example.com/feed.xml', {
  limit: 20,
  includeContent: false,
  timeout: 5000
});
javascript
const items = await fetchFeed('https://example.com/feed.xml', {
  limit: 20,
  includeContent: false,
  timeout: 5000
});

Compare with Previous Check

与上次检查结果对比

javascript
const newItems = await checkForNewItems(
  'https://example.com/feed.xml',
  lastCheckDate,
  { limit: 50 }
);
javascript
const newItems = await checkForNewItems(
  'https://example.com/feed.xml',
  lastCheckDate,
  { limit: 50 }
);

Output Format

输出格式

Each feed item contains:
  • title
    - Item title
  • link
    - Item URL
  • description
    - Item summary/description
  • pubDate
    - Publication date (ISO string)
  • author
    - Author name (if available)
  • content
    - Full content (if available and includeContent: true)
  • categories
    - Tags/categories
  • enclosures
    - Media attachments (images, audio, video)
每个订阅源条目包含:
  • title
    - 条目标题
  • link
    - 条目URL
  • description
    - 条目摘要/描述
  • pubDate
    - 发布日期(ISO字符串)
  • author
    - 作者名称(若可用)
  • content
    - 完整内容(若可用且includeContent设为true)
  • categories
    - 标签/分类
  • enclosures
    - 媒体附件(图片、音频、视频)

Examples

示例

RSS Feed Reader

RSS订阅源阅读器

javascript
const rssMonitor = require('./rss-monitor');

async function readFeeds() {
  const feeds = [
    'https://xkcd.com/atom.xml',
    'https://blog.github.com/feed/',
    'https://news.ycombinator.com/rss'
  ];
  
  for (const feedUrl of feeds) {
    try {
      const items = await rssMonitor.fetchFeed(feedUrl, { limit: 5 });
      console.log(`\n📰 ${feedUrl}`);
      items.forEach(item => {
        console.log(`${item.title}`);
        console.log(`    ${item.link}`);
      });
    } catch (err) {
      console.error(`Failed to fetch ${feedUrl}:`, err.message);
    }
  }
}
javascript
const rssMonitor = require('./rss-monitor');

async function readFeeds() {
  const feeds = [
    'https://xkcd.com/atom.xml',
    'https://blog.github.com/feed/',
    'https://news.ycombinator.com/rss'
  ];
  
  for (const feedUrl of feeds) {
    try {
      const items = await rssMonitor.fetchFeed(feedUrl, { limit: 5 });
      console.log(`\n📰 ${feedUrl}`);
      items.forEach(item => {
        console.log(`${item.title}`);
        console.log(`    ${item.link}`);
      });
    } catch (err) {
      console.error(`Failed to fetch ${feedUrl}:`, err.message);
    }
  }
}

Monitor for New Content

监控新内容

javascript
const rssMonitor = require('./rss-monitor');
const fs = require('fs').promises;

const STATE_FILE = './last-check.json';

async function monitorFeeds() {
  // Load last check times
  let lastChecks = {};
  try {
    lastChecks = JSON.parse(await fs.readFile(STATE_FILE, 'utf8'));
  } catch (e) {
    // No state file yet
  }
  
  const feeds = [
    { url: 'https://xkcd.com/atom.xml', name: 'XKCD' },
    { url: 'https://blog.github.com/feed/', name: 'GitHub Blog' }
  ];
  
  for (const feed of feeds) {
    const lastCheck = lastChecks[feed.url] ? new Date(lastChecks[feed.url]) : null;
    const newItems = await rssMonitor.checkForNewItems(feed.url, lastCheck);
    
    if (newItems.length > 0) {
      console.log(`\n🆕 ${feed.name}: ${newItems.length} new items`);
      newItems.forEach(item => {
        console.log(`${item.title}`);
      });
    }
    
    lastChecks[feed.url] = new Date().toISOString();
  }
  
  await fs.writeFile(STATE_FILE, JSON.stringify(lastChecks, null, 2));
}
javascript
const rssMonitor = require('./rss-monitor');
const fs = require('fs').promises;

const STATE_FILE = './last-check.json';

async function monitorFeeds() {
  // Load last check times
  let lastChecks = {};
  try {
    lastChecks = JSON.parse(await fs.readFile(STATE_FILE, 'utf8'));
  } catch (e) {
    // No state file yet
  }
  
  const feeds = [
    { url: 'https://xkcd.com/atom.xml', name: 'XKCD' },
    { url: 'https://blog.github.com/feed/', name: 'GitHub Blog' }
  ];
  
  for (const feed of feeds) {
    const lastCheck = lastChecks[feed.url] ? new Date(lastChecks[feed.url]) : null;
    const newItems = await rssMonitor.checkForNewItems(feed.url, lastCheck);
    
    if (newItems.length > 0) {
      console.log(`\n🆕 ${feed.name}: ${newItems.length} new items`);
      newItems.forEach(item => {
        console.log(`${item.title}`);
      });
    }
    
    lastChecks[feed.url] = new Date().toISOString();
  }
  
  await fs.writeFile(STATE_FILE, JSON.stringify(lastChecks, null, 2));
}

Notes

注意事项

  • Supports both RSS 2.0 and Atom 1.0 formats
  • Automatically detects feed format
  • Handles redirects and common feed variations
  • Default timeout: 10 seconds
  • Items sorted by publication date (newest first)
  • 支持RSS 2.0和Atom 1.0两种格式
  • 自动检测订阅源格式
  • 处理重定向和常见订阅源变体
  • 默认超时时间:10秒
  • 条目按发布日期排序(最新优先)