From fe23a611b9479723c6c654c3fe4daba9e870cf0c Mon Sep 17 00:00:00 2001 From: emkael Date: Thu, 31 Jan 2019 17:43:30 +0100 Subject: Switching Facebook and Rss providers to universal classes --- providers/Facebook.php | 45 +++++---------------------------------------- providers/Rss.php | 29 +++++++---------------------- 2 files changed, 12 insertions(+), 62 deletions(-) (limited to 'providers') diff --git a/providers/Facebook.php b/providers/Facebook.php index 6514cfe..6ef7898 100644 --- a/providers/Facebook.php +++ b/providers/Facebook.php @@ -2,50 +2,23 @@ namespace Providers; -require_once('Provider.php'); +require_once('HtmlFeed.php'); require_once('Item.php'); -require_once('../lib/querypath/src/qp.php'); -class Facebook extends \Providers\Provider { +class Facebook extends \Providers\HtmlFeed { protected $_cacheTimeout = '5 minutes'; - //protected $_cacheTimeout = '1 second'; - private $_encoding; - - public function __construct($feed, $options=[]) { - parent::__construct($feed, $options); - } protected function _getCachePath() { return '../cache/facebook.%s'; } - private function __getWWWContent() { - $header = array(); - $header[] = 'Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5'; - $header[] = 'Cache-Control: max-age=0'; - $header[] = 'Connection: keep-alive'; - $header[] = 'Keep-Alive: 300'; - $header[] = 'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7'; - $header[] = 'Pragma: '; - $ch = curl_init(sprintf('https://m.facebook.com/%s/', $this->_feed)); - curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)'); - curl_setopt($ch, CURLOPT_HTTPHEADER, $header); - curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); - curl_setopt($ch, CURLOPT_ENCODING, ''); - curl_setopt($ch, CURLOPT_TIMEOUT, 20); - $page = curl_exec($ch); - curl_close ($ch); - $this->_encoding = mb_detect_encoding($page); - return $page; + protected function _getFeedUrl($feed) { + return sprintf('https://m.facebook.com/%s', $feed); } - protected function _fetchItems() { + protected function _parseFeedContent($tree) { $items = []; - $page = $this->__getWWWContent(); - $tree = htmlqp($page, NULL, ['convert_from_encoding' => $this->_encoding, 'convert_to_encoding' => $this->_encoding]); $parents = []; foreach ($tree->find('div[data-ft]') as $div) { $data = json_decode($div->attr()['data-ft'], TRUE); @@ -72,10 +45,6 @@ class Facebook extends \Providers\Provider { return array_values($items); } - protected function _spamFilter($items) { - return $items; - } - protected function _mapItems($content) { return array_map( function ($obj) { @@ -94,10 +63,6 @@ class Facebook extends \Providers\Provider { ); } - protected function _sortContent($content) { - return $content; - } - public function title() { return sprintf("%s's Facebook page posts", $this->_feed); } diff --git a/providers/Rss.php b/providers/Rss.php index 4937f57..f303ee8 100644 --- a/providers/Rss.php +++ b/providers/Rss.php @@ -2,31 +2,25 @@ namespace Providers; -require_once('Provider.php'); +require_once('XmlFeed.php'); require_once('Item.php'); -class Rss extends \Providers\Provider { +class Rss extends \Providers\XmlFeed { - private $_feedUrl; - private $_feedXml; - - public function __construct($feed, $options=[]) { - parent::__construct($feed, $options); + protected function _getFeedUrl($feed) { $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'); + return $config[$feed]; } + return NULL; } 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; + protected function _parseFeedContent($feed) { + $feedItems = $feed->channel->item ?: $feed->entry; $items = []; foreach ($feedItems as $item) { $items[] = $item->asXML(); @@ -38,7 +32,6 @@ class Rss extends \Providers\Provider { $items = []; foreach ($content as $contentString) { $itemString = str_replace(['content:encoded>', '', '<', 'ID = strval($item->id ?: $item->guid); @@ -52,14 +45,6 @@ class Rss extends \Providers\Provider { return $items; } - protected function _spamFilter($content) { - return $content; - } - - protected function _sortContent($content) { - return $content; - } - public function title() { $cacheFile = sprintf($this->_getCachePath() . '.title', $this->_feed); if (!file_exists($cacheFile)) { -- cgit v1.2.3