diff options
-rw-r--r-- | providers/JsonFeed.php | 39 | ||||
-rw-r--r-- | providers/Substack.php | 43 |
2 files changed, 82 insertions, 0 deletions
diff --git a/providers/JsonFeed.php b/providers/JsonFeed.php new file mode 100644 index 0000000..2d2ed9d --- /dev/null +++ b/providers/JsonFeed.php @@ -0,0 +1,39 @@ +<?php + +namespace Providers; + +require_once('Provider.php'); + +abstract class JsonFeed extends \Providers\Provider { + + protected $_feedUrl; + protected $_feedJson; + + abstract protected function _getFeedUrl($feed); + + public function __construct($feed, $options=[]) { + parent::__construct($feed, $options); + $this->_feedUrl = $this->_getFeedUrl($feed); + if (!$this->_feedUrl) { + throw new Exception('JSON feed "' . $feed . '" undefined'); + } + } + + abstract protected function _parseFeedContent($feed); + + protected function _fetchItems() { + $this->_feedJson = json_decode(file_get_contents($this->_feedUrl)); + return $this->_parseFeedContent($this->_feedJson); + } + + protected function _spamFilter($content) { + return $content; + } + + protected function _sortContent($content) { + return $content; + } + +} + +?> diff --git a/providers/Substack.php b/providers/Substack.php new file mode 100644 index 0000000..6dea64a --- /dev/null +++ b/providers/Substack.php @@ -0,0 +1,43 @@ +<?php + +namespace Providers; + +require_once('JsonFeed.php'); +require_once('Item.php'); + +class Substack extends \Providers\JsonFeed { + + protected function _getFeedUrl($feed) { + return sprintf('https://%s.substack.com/api/v1/posts', $feed); + } + + protected function _getCachePath() { + return '../cache/substack.%s'; + } + + protected function _parseFeedContent($feed) { + return $feed; + } + + protected function _mapItems($content) { + $items = []; + foreach ($content as $item) { + $itemObject = new Item(); + $itemObject->ID = sprintf($this->feedUrl . '/' . $item->publication_id); + $itemObject->Title = strval($item->title); + $itemObject->Time = $item->post_date; + $itemObject->Text = $item->body_html; + $itemObject->Link = $item->canonical_url; + $itemObject->Author = $item->publishedByLines[0]->name; + $items[] = $itemObject; + } + return $items; + } + + public function title() { + return sprintf("%s's substack", $this->_feed); + } + +} + +?> |