如何使用 RSSHub 将自己的项目转换为 RSS 源

1,549 阅读4分钟

RSSHub 是一个强大的开源工具,能够为几乎任何内容生成 RSS 订阅源。无论是博客、新闻网站、社交媒体,还是自定义项目,RSSHub 都能帮助你轻松实现 RSS 订阅功能。本文将详细介绍如何使用 RSSHub 将自己的项目转换为 RSS 源。


1. 什么是 RSSHub?

RSSHub 是由中国开发者 DIYgod 创建的一个开源项目,旨在为不支持 RSS 的网站生成 RSS 订阅源。它通过抓取网页内容或调用 API,将数据转换为 RSS 格式,方便用户订阅和跟踪更新。

DIYgod 简介

  • GitHub 主页: DIYgod
  • 知名项目: RSSHub、DPlayer、APlayer 等。
  • 特点: 代码简洁高效,注重用户体验,活跃于开源社区。

2. 安装 RSSHub

方法一:使用 Docker(推荐)

  1. 安装 Docker 和 Docker Compose。
  2. 创建 docker-compose.yml 文件:
    version: '3'
    services:
      rsshub:
        image: diygod/rsshub
        restart: always
        ports:
          - '1200:1200'
        environment:
          NODE_ENV: production
          CACHE_TYPE: redis
          REDIS_URL: 'redis://redis:6379/'
        depends_on:
          - redis
    
      redis:
        image: redis:alpine
        restart: always
        volumes:
          - redis_data:/data
    
    volumes:
      redis_data:
    
  3. 启动 RSSHub:
    docker-compose up -d
    
  4. 访问 http://localhost:1200,确认 RSSHub 已正常运行。

方法二:直接运行(Node.js 环境)

  1. 安装 Node.js(版本 >= 16)。
  2. 克隆 RSSHub 仓库:
    git clone https://github.com/DIYgod/RSSHub.git
    cd RSSHub
    
  3. 安装依赖:
    npm install
    
  4. 启动 RSSHub:
    npm start
    
  5. 访问 http://localhost:1200,确认 RSSHub 已正常运行。

3. 为项目创建 RSS 源

方法一:使用现有路由

RSSHub 已经支持许多网站和平台。你可以查看 RSSHub 文档 找到适合你的项目的路由。

例如:

  • 如果你的项目是一个 GitHub 仓库,可以使用以下路由:
    /github/issue/user/repo
    
    示例:
    https://rsshub.app/github/issue/DIYgod/RSSHub
    

方法二:自定义路由

如果你的项目没有被 RSSHub 默认支持,可以编写自定义路由。

  1. 在 RSSHub 的 lib/routes 目录下创建一个新的 JavaScript 文件,例如 myproject.js
  2. 编写路由逻辑。以下是一个简单的示例:
    const got = require('@/utils/got');
    const { parseDate } = require('@/utils/parse-date');
    
    module.exports = async (ctx) => {
        const response = await got.get('https://api.myproject.com/posts'); // 替换为你的项目 API 或网页
        const data = response.data;
    
        const items = data.map((item) => ({
            title: item.title,
            description: item.description,
            pubDate: parseDate(item.published_at), // 根据实际情况调整日期字段
            link: item.url,
        }));
    
        ctx.state.data = {
            title: 'My Project Updates', // RSS 源标题
            link: 'https://myproject.com', // 项目主页
            description: 'Latest updates from My Project', // RSS 源描述
            item: items,
        };
    };
    
  3. lib/router.js 中注册路由:
    module.exports = {
        '/myproject': require('./routes/myproject'),
    };
    
  4. 重启 RSSHub 服务,访问以下链接查看生成的 RSS 源:
    http://localhost:1200/myproject
    

4. 发布和分享 RSS 源

  1. 将生成的 RSS 源链接(例如 https://rsshub.app/myproject)发布到你的网站或社交媒体。
  2. 用户可以使用 RSS 阅读器(如 Feedly、Inoreader)订阅你的 RSS 源。

5. 高级功能

  • 缓存:RSSHub 支持 Redis 缓存,可以提高性能。
  • 访问控制:可以通过配置环境变量限制访问。
  • 部署:可以将 RSSHub 部署到云服务(如 Vercel、Heroku)以便公开访问。

如果你不想使用RSSHub

使用RSSHub需要你提供一个数据接口,但是如果你直接想使用简单的代码对外提供RSS源的功能,你可以参考下面这段Python代码,你需要安装 Flask==1.1.2 和 feedgen==1.0.0

@app.route('/notfresh/rss/<string:category>')
def get_rss_feed(category):
    fg = FeedGenerator()
    
    # 设置feed的基本信息,使用完整的当前URL作为feed链接
    request_url = request.url  # 获取当前请求的完整URL
    fg.id(request_url)
    fg.title(f'NotFresh {category} RSS Feed')
    fg.description(f'最新的{category}相关技术资讯')
    fg.link(href=request_url, rel='self')
    fg.language('zh-CN')
    
    # 添加feed的其他必要信息
    fg.author({'name': 'NotFresh Team', 'email': 'admin@notfresh.com'})
    fg.logo('https://notfresh.com/static/logo.png')  # 可选
    fg.subtitle('技术资讯RSS订阅')
    
    # 获取当前时间并添加时区信息
    current_time = datetime.now().astimezone()
    fg.updated(current_time)  # 设置feed更新时间
    
    # RSS条目数据
    entries = [
        {
            "title": f"{category} - GitHub Copilot 新功能发布",
            "description": "GitHub Copilot推出了新的代码补全和AI辅助功能,提升开发效率...",
            "published_at": current_time - timedelta(hours=2),
            "url": "https://github.blog/2024-01-30-github-copilot-chat-beta-now-available-for-individuals/",
            "author": "GitHub Team",
            "guid": "github-copilot-2024-01-30"  # 添加唯一标识符
        },
        {
            "title": f"{category} - Stack Overflow 2023年度调查报告",
            "description": "Stack Overflow发布2023年度开发者调查报告,揭示最新技术趋势...",
            "published_at": current_time - timedelta(hours=5),
            "url": "https://survey.stackoverflow.co/2023/",
            "author": "Stack Overflow",
            "guid": "stackoverflow-survey-2023"
        },
        {
            "title": f"{category} - Python 3.12新特性解析",
            "description": "Python 3.12版本发布,带来性能提升和新语言特性...",
            "published_at": current_time - timedelta(hours=8),
            "url": "https://docs.python.org/3.12/whatsnew/3.12.html",
            "author": "Python Core Team",
            "guid": "python-312-whatsnew"
        }
    ]
    
    for entry in entries:
        fe = fg.add_entry()
        fe.id(entry["guid"])  # 使用唯一标识符
        fe.title(entry["title"])
        fe.description(entry["description"])
        fe.link(href=entry["url"])
        fe.author(name=entry["author"])
        fe.published(entry["published_at"])
        fe.updated(entry["published_at"])  # 添加更新时间
    
    # 生成RSS feed并指定编码
    rssfeed = fg.rss_str(pretty=True, encoding='UTF-8')
    
    # 创建响应并设置正确的Content-Type和编码
    response = make_response(rssfeed)
    response.headers.set('Content-Type', 'application/rss+xml; charset=UTF-8')
    
    return response

总结

通过 RSSHub,你可以轻松将自己的项目或网站内容转换为 RSS 源。无论是使用现有路由还是编写自定义路由,RSSHub 都提供了灵活的工具来满足你的需求。如果需要进一步帮助,可以参考 RSSHub 官方文档 或加入其社区。


相关链接