Skip to content

Commit

Permalink
spouts\rss\feed: inject SimplePie
Browse files Browse the repository at this point in the history
  • Loading branch information
jtojnar committed Apr 19, 2020
1 parent 4f62d52 commit 85f8de5
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 44 deletions.
6 changes: 6 additions & 0 deletions src/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,12 @@
'constructParams' => ['selfoss'],
]);

$dice->addRule(helpers\FeedHelper::class, [
'constructParams' => [
\F3::get('cache'),
],
]);

// init logger
$log = $dice->create(Logger::class);
if ($f3->get('logger_level') === 'NONE') {
Expand Down
82 changes: 82 additions & 0 deletions src/helpers/FeedReader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace helpers;

use SimplePie;

/**
* Helper class for obtaining feeds
*/
class FeedReader {
/** @var SimplePie */
private $simplepie;

/**
* @param ?string $cacheLocation
* @param int $cacheTimeout
*/
public function __construct(SimplePie $simplepie, WebClient $webClient, $cacheLocation = null, $cacheTimeout = 1800) {
$this->simplepie = $simplepie;

// initialize simplepie feed loader
$this->simplepie->set_cache_location($cacheLocation);
$this->simplepie->set_cache_duration($cacheTimeout);

// abuse set_curl_options since there is no sane way to pass data to SimplePie\File
$this->simplepie->set_curl_options([
WebClient::class => $webClient
]);

$this->simplepie->set_file_class(SimplePieFileGuzzle::class);
$this->simplepie->set_autodiscovery_level(SIMPLEPIE_LOCATOR_AUTODISCOVERY | SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY);
$this->simplepie->set_useragent($webClient->getUserAgent());
}

/**
* Load the feed for provided URL using SimplePie.
*
* @param string $url URL of the feed
*
* @return array
*/
public function load($url) {
@$this->simplepie->set_feed_url($url);
// fetch items
@$this->simplepie->init();

// on error retry with force_feed
if (@$this->simplepie->error()) {
@$this->simplepie->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
@$this->simplepie->force_feed(true);
@$this->simplepie->init();
}

// check for error
if (@$this->simplepie->error()) {
throw new \Exception($this->simplepie->error());
}

return [
// save fetched items
'items' => $this->simplepie->get_items(),
'htmlUrl' => @$this->simplepie->get_link(),
'spoutTitle' => $this->simplepie->get_title(),
];
}

/**
* Get the URL of the feed’s logo.
*
* @return ?string
*/
public function getImageUrl() {
return $this->simplepie->get_image_url();
}

/**
* @return void
*/
public function __destruct() {
return $this->simplepie->__destruct();
}
}
53 changes: 11 additions & 42 deletions src/spouts/rss/feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace spouts\rss;

use helpers\FeedReader;
use helpers\Image;
use helpers\WebClient;
use Monolog\Logger;

/**
Expand Down Expand Up @@ -42,58 +42,27 @@ class feed extends \spouts\spout {
/** @var Logger */
private $logger;

/** @var FeedReader */
private $feed;

/** @var Image image helper */
private $imageHelper;

/** @var WebClient */
private $webClient;

public function __construct(Image $imageHelper, Logger $logger, WebClient $webClient) {
public function __construct(FeedReader $feed, Image $imageHelper, Logger $logger) {
$this->imageHelper = $imageHelper;
$this->logger = $logger;
$this->webClient = $webClient;
$this->feed = $feed;
}

//
// Source Methods
//

public function load(array $params) {
// initialize simplepie feed loader
$this->feed = @new \SimplePie();
@$this->feed->set_cache_location(\F3::get('cache'));
@$this->feed->set_cache_duration(1800);
// abuse set_curl_options since there is no sane way to pass data to SimplePie\File
$this->feed->set_curl_options([
WebClient::class => $this->webClient
]);
@$this->feed->set_file_class(\helpers\SimplePieFileGuzzle::class);
@$this->feed->set_feed_url(htmlspecialchars_decode($params['url']));
@$this->feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_AUTODISCOVERY | SIMPLEPIE_LOCATOR_LOCAL_EXTENSION | SIMPLEPIE_LOCATOR_LOCAL_BODY);
$this->feed->set_useragent($this->webClient->getUserAgent());

// fetch items
@$this->feed->init();

// on error retry with force_feed
if (@$this->feed->error()) {
@$this->feed->set_autodiscovery_level(SIMPLEPIE_LOCATOR_NONE);
@$this->feed->force_feed(true);
@$this->feed->init();
}

// check for error
if (@$this->feed->error()) {
throw new \Exception($this->feed->error());
} else {
// save fetched items
$this->items = @$this->feed->get_items();
}

// set html url
$this->htmlUrl = @$this->feed->get_link();

$this->spoutTitle = $this->feed->get_title();
$feedData = $this->feed->load(htmlspecialchars_decode($params['url']));
$this->items = $feedData['items'];
$this->htmlUrl = $feedData['htmlUrl'];
$this->spoutTitle = $feedData['spoutTitle'];
}

public function getXmlUrl(array $params) {
Expand Down Expand Up @@ -144,7 +113,7 @@ public function getIcon() {
$this->faviconUrl = $this->imageHelper->getFaviconUrl();
$this->logger->debug('icon: using feed homepage favicon: ' . $this->faviconUrl);
} else {
$feedLogoUrl = $this->feed->get_image_url();
$feedLogoUrl = $this->feed->getImageUrl();
if ($feedLogoUrl && $this->imageHelper->fetchFavicon($feedLogoUrl)) {
$this->faviconUrl = $this->imageHelper->getFaviconUrl();
$this->logger->debug('icon: using feed logo: ' . $this->faviconUrl);
Expand Down
5 changes: 3 additions & 2 deletions src/spouts/rss/fulltextrss.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace spouts\rss;

use Graby\Graby;
use helpers\FeedReader;
use helpers\Image;
use helpers\WebClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;
Expand Down Expand Up @@ -45,8 +46,8 @@ class fulltextrss extends feed {
/** @var WebClient */
private $webClient;

public function __construct(Image $imageHelper, Logger $logger, WebClient $webClient) {
parent::__construct($imageHelper, $logger, $webClient);
public function __construct(FeedReader $feed, Image $imageHelper, Logger $logger, WebClient $webClient) {
parent::__construct($feed, $imageHelper, $logger);

$this->imageHelper = $imageHelper;
$this->logger = $logger;
Expand Down

0 comments on commit 85f8de5

Please sign in to comment.