chrishardie/laravel-feedmaker

Laravel 包,用于爬取/解析 HTML 页面并生成相应的 RSS 订阅源

v1.0.6 2024-01-03 17:41 UTC

This package is auto-updated.

Last update: 2024-09-11 03:24:01 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Laravel 包,用于爬取/解析 HTML 页面并生成相应的 RSS 订阅源

安装

您可以通过 composer 安装此包

composer require chrishardie/laravel-feedmaker

您可以使用以下命令发布和运行迁移

php artisan vendor:publish --provider="ChrisHardie\Feedmaker\FeedmakerServiceProvider" --tag="feedmaker-migrations"
php artisan migrate

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="ChrisHardie\Feedmaker\FeedmakerServiceProvider" --tag="feedmaker-config"

这是已发布配置文件的内容

return [
    // How often to update feeds from sources, in minutes
    'default_update_frequency' => 60,

    // Feed index web route
    'url' => '/',
];

在您的 config/filesystems.php 文件中添加一个新的磁盘,以定义生成的 RSS 订阅源将存储的位置

    'disks' => [
        ...
        'feedmaker' => [
            'driver' => 'local',
            'root' => storage_path('app/feeds'),
            'url' => env('APP_URL').'/feeds',
            'visibility' => 'public',
        ],
        ...
    'links' => [
        ...
        public_path('feeds') => storage_path('app/feeds'),

然后,运行 artisan storage:link 确保存储磁盘已就位。

要显示可用订阅源的索引,请在配置文件中配置 $url 变量,并在您的 routes/web.php 文件中添加以下内容

Route::feedsindex();

如果您想获取有关从源更新订阅源的问题通知,请确保您已定义日志目标。例如,要接收 Slack 通知,请确保在 .env 中定义了 LOG_SLACK_WEBHOOK_URL,然后设置您的 LOG_CHANNEL 以包含一个包含 Slack 的日志堆栈。

用法

将新源添加到包含中的步骤有两个

  1. 创建一个新的 Source 模型。如果您没有管理界面,您可以通过 tinker 来完成此操作
$ artisan tinker
>>> $s = new ChrisHardie\Feedmaker\Models\Source
>>> $s->class_name = 'YourSource'
>>> $s->source_url = 'https://www.example.com/news'
>>> $s->name = 'Source Name'
>>> $s->home_url = 'https://example.com/'
>>> $s->frequency = 60
>>> $s->save();

这告诉应用程序有关您的源的基本信息,包括将定义如何抓取/爬取它的 PHP 类、要爬取的 URL、面向人类的名称和主要 URL。

  1. app/Sources/YourSource/YourSource.php 中创建一个源类,该类定义一个 generateRssItems() 方法,返回要包含在 RSS 订阅源中的项目集合。以下是一个示例
<?php

namespace App\Sources\YourSource;

use ChrisHardie\Feedmaker\Sources\BaseSource;
use ChrisHardie\Feedmaker\Sources\RssItemCollection;
use ChrisHardie\Feedmaker\Models\Source;

class YourSource extends BaseSource
{
    /**
     * @param Source $source
     * @return RssItemCollection
     * @throws \JsonException
     * @throws SourceNotCrawlable
     */
    public function generateRssItems(Source $source) : RssItemCollection
    {
        $items = array();
        $html = HTTP::get($source->source_url);
        ...
        return RssItemCollection::make($items);    
    }
}

如果您将通过 CSS 或 XPath 选择器抓取 URL 的 DOM,您可以使用 scraper 特性来简化此操作。它为您处理 generateRssItems 方法,您只需定义一个返回 RssItemCollection 的 parse() 方法即可。

<?php

namespace App\Sources\YourSource;

use ChrisHardie\Feedmaker\Sources\BaseSource;
use ChrisHardie\Feedmaker\Sources\RssItemCollection;
use ChrisHardie\Feedmaker\Models\Source;

class YourSource extends BaseSource
{
    use ScraperTrait;

    /**
     * @throws SourceNotCrawlable
     */
    public function parse(Crawler $crawler, Source $source) : RssItemCollection
    {
        $items = array();
        $nodes = $crawler->filter('.news-items');
        foreach ($nodes as $node) {
            ...
        }
        return RssItemCollection::make($items);
    }

RssItemCollection 必须包含以下键,每个项目一个

  • pubDate: Carbon 日期对象
  • title: 字符串
  • url: 字符串
  • description: 字符串

可选地,它还可以包含以下键

  • guid: 一个将成为 RSS 项目的唯一/GUID 的 URL,而不是 URL

然后,您可以强制检查您的源并生成相应的订阅源

$ artisan feeds:update YourSource

https://yoursite.com/feeds/yoursource.rss 查看结果

所有生成的订阅源的索引应可在配置文件中定义的 URI 上找到。

变更日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

问题和拉取请求都欢迎。

致谢

许可

MIT 许可证 (MIT)。有关更多信息,请参阅许可文件