diff options
Diffstat (limited to 'providers/Rss.php')
-rw-r--r-- | providers/Rss.php | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/providers/Rss.php b/providers/Rss.php new file mode 100644 index 0000000..6fdae6c --- /dev/null +++ b/providers/Rss.php @@ -0,0 +1,69 @@ +<?php + +namespace Providers; + +require_once('Provider.php'); +require_once('Item.php'); + +class Rss extends \Providers\Provider { + + protected $_cacheTimeout = '1 second'; + + private $_feedUrl; + private $_feedXml; + + public function __construct($feed, $options=[]) { + parent::__construct($feed, $options); + $config = json_decode(file_get_contents('../config/rss.json'), TRUE); + if (isset($config[$feed])) { + $this->_feedUrl = $config[$feed]; + } else { + throw new Exception('RSS feed "' . $feed . '" undefined'); + } + } + + protected function _getCachePath() { + return '../cache/rss.%s'; + } + + protected function _fetchItems() { + $this->_feedXml = new \SimpleXMLElement($this->_feedUrl, 0, TRUE); + $feedItems = $this->_feedXml->channel->item ?: $this->_feedXml->entry; + $items = []; + foreach ($feedItems as $item) { + $items[] = $item->asXML(); + } + return $items; + } + + protected function _mapItems($content) { + $items = []; + foreach ($content as $contentString) { + $item = new \SimpleXMLElement($contentString); + $itemObject = new Item(); + $itemObject->ID = strval($item->id ?: $item->guid); + $itemObject->Title = strval($item->title); + $itemObject->Time = strval($item->updated ?: $item->pubDate); + $itemObject->Text = strval($item->summary ?: $item->description); + $itemObject->Link = strval(is_string($item->link) ? $item->link : $item->link->attributes()['href']); + $itemObject->Author = strval(is_string($item->author) ? $item->author : $item->author->name); + $items[] = $itemObject; + } + return $items; + } + + protected function _spamFilter($content) { + return $content; + } + + protected function _sortContent($content) { + return $content; + } + + public function title() { + return strval($this->_feedXml->title ?: $this->_feedXml->channel->title); + } + +} + +?> |