kaishiyoku/hera-rss-crawler 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kaishiyoku/hera-rss-crawler

Composer 安装命令:

composer require kaishiyoku/hera-rss-crawler

包简介

Modern library to handle RSS/Atom feeds

README 文档

README

This project tries to make fetching and parsing RSS feeds easier. With Hera RSS you can discover, fetch and parse RSS feeds.

Installation

  1. simply run composer require kaishiyoku/hera-rss-crawler
  2. create a new crawler instance using $heraRssCrawler = new HeraRssCrawler()
  3. discover a feed, for example $feedUrls = $heraRssCrawler->discoverFeedUrls('https://laravel-news.com/')
  4. pick the feed you like to use; if there were multiple feeds discovered pick one
  5. fetch the feed: $feed = $heraRssCrawler->parseFeed($feedUrls->get(0))
  6. fetch the articles: $feedItems = $feed->getFeedItems()

Breaking Changes

Version 6.x

  • dropped support for PHP 8.0

Version 5.x

  • dropped support for PHP 7.4

Version 4.x

  • dropped support for Laravel 8

Version 3.x

  • FeedItem-method jsonSerialize has been renamed to toJson and doesn't return null anymore but throws a JsonException if the serialized JSON is invalid.

Available crawler options

setRetryCount(int $retryCount): void

Determines how many retries parsing or discovering feeds will be made when an exception occurs, e.g. if the feed was unreachable.

setLogger(LoggerInterface $logger): void

Set your own logger instance, e.g. a simple file logger.

setUrlReplacementMap(array $urlReplacementMap): void

Useful for websites which redirect to another subdomain when visiting the site, e.g. for Reddit.

setFeedDiscoverers(Collection $feedDiscoverers): void

With that you can set your own feed discoverers.

You can even write your own, just make sure to implement the FeedDiscoverer interface:

<?php

namespace Kaishiyoku\HeraRssCrawler\FeedDiscoverers;

use GuzzleHttp\Client;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Kaishiyoku\HeraRssCrawler\Models\ResponseContainer;

/**
 * Discover feed URL by parsing a direct RSS feed url.
 */
class FeedDiscovererByContentType implements FeedDiscoverer
{
    public function discover(Client $httpClient, ResponseContainer $responseContainer): Collection
    {
        $contentTypeMixedValue = Arr::get($responseContainer->getResponse()->getHeaders(), 'Content-Type');

        $contentType = is_array($contentTypeMixedValue) ? Arr::first($contentTypeMixedValue) : $contentTypeMixedValue;

        // the given url is no valid RSS feed
        if (!$contentType || !Str::startsWith($contentType, ['application/rss+xml', 'application/atom+xml'])) {
            return new Collection();
        }

        return new Collection([$responseContainer->getRequestUrl()]);
    }
}

The default feed discoverers are as follows:

new Collection([
    new FeedDiscovererByContentType(),
    new FeedDiscovererByHtmlHeadElements(),
    new FeedDiscovererByHtmlAnchorElements(),
    new FeedDiscovererByFeedly(),
])

The ordering is important here because the discoverers will be called sequentially until at least one feed URL has been found and then stops.

That means that once the discoverer found a feed remaining discoverers won't be called.

If you want to mainly discover feeds by using HTML anchor elements, the FeedDiscovererByHtmlAnchorElements discoverer should be the first discoverer in the collection.

Available crawler methods

parseFeed(string $url): ?Feed

Simply fetch and parse the feed of a given feed url. If no consumable RSS feed is being found null is being returned.

discoverAndParseFeeds(string $url): Collection

Discover feeds from a website url and return all parsed feeds in a collection.

discoverFeedUrls(string $url): Collection

Discover feeds from a website url and return all found feed urls in a collection. There are multiple ways the crawler tries to discover feeds. The order is as follows:

  1. discover feed urls by content type
    if the given url is already a valid feed return this url
  2. discover feed urls by HTML head elements
    find all feed urls inside a HTML document
  3. discover feed urls by HTML anchor elements
    get all anchor elements of a HTML element and return the urls of those which include rss in its urls
  4. discover feed urls by Feedly
    fetch feed urls using the Feedly API
discoverFavicon(string $url): ?string

Fetch the favicon of the feed's website. If none is found then null is being returned.

checkIfConsumableFeed(string $url): bool

Check if a given url is a consumable RSS feed.

Contribution

Found any issues or have an idea to improve the crawler? Feel free to open an issue or submit a pull request.

Plans for the future

  • add a Laravel facade

Author

Email: dev@andreas-wiedel.de
Website: https://andreas-wiedel.de

kaishiyoku/hera-rss-crawler 适用场景与选型建议

kaishiyoku/hera-rss-crawler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4.5k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2019 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「parser」 「atom」 「feed」 「rss」 「crawler」 「reader」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kaishiyoku/hera-rss-crawler 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kaishiyoku/hera-rss-crawler 我们能提供哪些服务?
定制开发 / 二次开发

基于 kaishiyoku/hera-rss-crawler 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4.5k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-12-23