承接 baraveli/rss-scraper 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

baraveli/rss-scraper

Composer 安装命令:

composer require baraveli/rss-scraper

包简介

Rss Scraper to scrap rss from dhivehi news sites

README 文档

README

Build Status Latest Stable Version License

Rss Scraper logo Rss Scraper to scrap rss feed from news websites.

🚀 Installation

composer require baraveli/rss-scraper

Usage

To use this package when you install it be sure to create a config.json file inside your application and specify the sites you want to index.

📡 Rss Scraper Specs

This documentation decribe the rss scraper structure,usage and how the individual components work in the libary.

🔮 General Explanation

The rss scraper get the rss feed of the news from the configuration and get the rss feed items and return the data as a json response or an array.

  • 🔨 Config loader

Rss scraper configurations are stored in the configs directory as config.json file. The config file has the information about the rss feeds that the rss scraper calls to scrap the rss feed.

Example config:

{
    "mihaaru":"https://mihaaru.com/rss",
    "vaguthu" "https://vaguthu.mv/feed"
}

This configuration file is loading the rss feed of mihaaru and vaguthu.

Thats pretty much it for the configuration file. Rss scraper has a util ConfigLoader class to load configuration data from the configs directory and return the rss feed url as an array.

The ConfigLoader class has one static load method which takes a filename as an argument to the method as a string. filename will be the name of the json file inside the configs directory. In this case the file name will be config. If a given file is not found load method throws an execption saying "Error reading the config file or it is empty."

Config loader class is shown below:

<?php

namespace Baraveli\RssScraper\Util;

use Baraveli\RssScraper\Interfaces\IConfigLoader;

class ConfigLoader implements IConfigLoader
{
    /**
     * load
     *
     * @param  mixed $filename
     *
     * This static method loads configuration files from the configs directory
     *
     * @return array
     */
    public static function load(string $filename): array
    {
        $path = IConfigLoader::DIRECTORY_PATH . $filename .  '.json';
        if (!file_exists($path)) {
            $path = getcwd() . '/'. $filename .  '.json';
        }

        $file = file_get_contents($path, FILE_USE_INCLUDE_PATH);
        $urls = json_decode($file, true);

        if (!isset($file, $urls)) {
            throw new \Exception("Error reading the config file or it it is empty");
        }

        return $urls;
    }
}
  • 🔦 Http Client

Client class inside the Http directory of the RSS scraper is used to send HTTP request to the RSS feed URL specified in the config to get the content. The class get method gets the content of the RSS URL and check if the returned data is a validxml content. isValidXmL() is helper method that is provided by the helper trait. if the isvalidxml check passes the xml file is then pass to the simplexml_load_string() function that is built into php. the returned loaded string get passed to parseXML method to return the decoded version of the xml file to php array. The data is then returned.

This classes uses guzzle to make the http request.

Client class is shown below:

<?php

namespace Baraveli\RssScraper\Http;

use GuzzleHttp\Client as GuzzleClient;
use Baraveli\RssScraper\Util\Helper;

class Client
{
    use Helper;

    private $client;

    public function __construct()
    {
        $this->client = new GuzzleClient();
    }

    /**
     * get
     *
     * Method to get the rss feed.
     * 
     * This method does parsing of xml to php array and validation checks before returning data.
     * 
     * @param  mixed $link
     *
     * @return void
     */
    public function get($link)
    {
        $response = $this->client->request('GET', $link);

        $responseBody = $response->getBody();
        if (!$this->isValidXml($responseBody)) {
            throw new \Exception("The file doesn't contain valid XML");
        }

        $xmlfile = simplexml_load_string($responseBody);
        $data = $this->parseXML($xmlfile);

        return $data;
    }

    /**
     * parseXML
     * 
     * This method decode the xml data to php array
     *
     * @param  mixed $xmlfile
     *
     * @return void
     */
    protected function parseXML($xmlfile)
    {
        $json = json_encode($xmlfile);
        $data = json_decode($json, true);

        return $data;
    }
}
  • 📄 Article Collection

Article collection is a class that is responsible for adding everything to a collection so that the collection can easily be manipluated as a array or json. Article collection class has an item array which holds all the items. Items are added through the add method given a value. Class also have a method called jsonify() which converts the responses to json and a toArray() method that converts the response to an array. Count method lets you to count the number of item inside the item array.

Article Collection class is shown below:

<?php

namespace Baraveli\RssScraper\Collections;

use Countable;

class ArticleCollection implements Countable
{

    protected $items = [];

    /**
     * __toString
     *
     * Jsonify the collection automatically when the trying to output as a string.
     * 
     * @return void
     */
    public function __toString()
    {
        return $this->jsonify();
    }


    /**
     * add
     *
     * @param  mixed $value
     * 
     * Method to add items to the collection array.
     *
     * @return void
     */
    public function add($value)
    {
        $this->items[] = $value;
    }

    /**
     * get
     *
     * @param  mixed $key
     * 
     * Method to get the items from the collection array given a (int)key value
     *
     * @return void
     */
    public function get($key)
    {
        return array_key_exists($key, $this->items) ? $this->items[$key] : null;
    }

    /**
     * jsonify
     * 
     * Method to convert the response to json
     * 
     * This method is chainable with the getrss() function.
     *
     * @return void
     */
    public function jsonify()
    {
        return json_encode($this->items);
    }

    /**
     * toArray
     *
     * Method to return the response as an array
     * 
     * This method is chainable with the getrss() function.
     * 
     * @return void
     */
    public function toArray()
    {
        return $this->items;
    }

    /**
     * count
     *
     * Method to count how many items are in the article collection array
     * 
     * @return void
     */
    public function count()
    {
        return count($this->items);
    }
}

baraveli/rss-scraper 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 24
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 7
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 3
  • 开发语言: PHP

其他信息

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