baraveli / rss-scraper
用于从马尔代夫新闻网站抓取RSS的Rss抓取器
Requires
- guzzlehttp/guzzle: ^6.0|^7.0.1
Requires (Dev)
- phpunit/phpunit: ^8.5
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2024-09-29 05:22:15 UTC
README
🚀 安装
composer require baraveli/rss-scraper
使用方法
要使用此包,在安装时请确保在您的应用程序中创建一个config.json文件,并指定您要索引的网站。
📡 Rss Scraper 规范
此文档描述了rss抓取器的结构、用法以及库中各个组件如何工作。
🔮 通用说明
rss抓取器从配置中获取新闻的rss源,获取rss源条目,并将数据作为json响应或数组返回。
Rss抓取器的配置存储在configs目录下的config.json文件中。配置文件包含rss抓取器调用来抓取rss源的信息。
示例配置
{ "mihaaru":"https://mihaaru.com/rss", "vaguthu" "https://vaguthu.mv/feed" }
此配置文件正在加载来自mihaaru和vaguthu的rss源。
配置文件的配置就是这样。rss抓取器有一个名为ConfigLoader的实用工具类,用于从configs目录加载配置数据,并将rss源url作为数组返回。
ConfigLoader类有一个静态load方法,该方法将一个字符串作为参数传递给方法。filename将是configs目录中json文件的名称。在这种情况下,文件名将是config。如果找不到指定的文件,load方法将抛出一个异常,表示"无法读取配置文件或它为空"。
以下是如何显示ConfigLoader类的示例
<?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; } }
位于RSS抓取器Http目录中的Client类用于向配置中指定的RSS源URL发送HTTP请求以获取内容。类的get方法获取RSS URL的内容,并检查返回的数据是否是有效的xml内容。isValidXmL()
是helper trait提供的辅助方法。如果isvalidxml检查通过,则xml文件被传递到PHP内置的simplexml_load_string()
函数。加载后的字符串传递给parseXML
方法以返回xml文件的解码版本到PHP数组。然后返回数据。
此类使用guzzle来发送HTTP请求。
以下是如何显示Client类的示例
<?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; } }
文章集合是一个类,负责将所有内容添加到集合中,以便集合可以轻松地作为数组或JSON进行操作。文章集合类有一个项目数组,用于保存所有项目。项目通过add方法添加,给定一个值。类还有一个名为jsonify()的方法,用于将响应转换为JSON,以及一个toArray()方法,用于将响应转换为数组。Count方法允许您计算项目数组中的项目数量。
以下是如何显示文章集合类的示例
<?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); } }