summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Command/Updater/UpdateUser.php70
-rwxr-xr-xlib/Fetcher/FeedFetcher.php61
-rw-r--r--lib/Scraper/Scraper.php2
3 files changed, 122 insertions, 11 deletions
diff --git a/lib/Command/Updater/UpdateUser.php b/lib/Command/Updater/UpdateUser.php
new file mode 100644
index 000000000..050641972
--- /dev/null
+++ b/lib/Command/Updater/UpdateUser.php
@@ -0,0 +1,70 @@
+<?php
+/**
+ * Nextcloud - News
+ *
+ * This file is licensed under the Affero General Public License version 3 or
+ * later. See the COPYING file.
+ */
+
+namespace OCA\News\Command\Updater;
+
+use Exception;
+use OCA\News\Service\FeedServiceV2;
+use Symfony\Component\Console\Command\Command;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class UpdateUser extends Command
+{
+ /**
+ * @var FeedServiceV2 Feed service
+ */
+ private $feedService;
+
+ public function __construct(FeedServiceV2 $feedService)
+ {
+ parent::__construct();
+ $this->feedService = $feedService;
+ }
+
+ /**
+ * @return void
+ */
+ protected function configure()
+ {
+ $this->setName('news:updater:update-user')
+ ->addArgument(
+ 'user-id',
+ InputArgument::REQUIRED,
+ 'user id of a user, string'
+ )
+ ->setDescription('Console API for updating a single user\'s feed');
+ }
+
+ protected function execute(InputInterface $input, OutputInterface $output): int
+ {
+ $userId = $input->getArgument('user-id');
+ $feeds = $this->feedService->findAllForUser($userId);
+ $updateErrors = false;
+ foreach ($feeds as $feed) {
+ try {
+ $updated_feed = $this->feedService->fetch($feed);
+ if ($updated_feed->getUpdateErrorCount() !== 0) {
+ $output->writeln($updated_feed->getLastUpdateError());
+ $updateErrors = true;
+ }
+ } catch (Exception $e) {
+ $output->writeln(
+ '<error>Could not update feed with id ' . $feed->getId() .
+ '. ' . $e->getMessage() . '</error> '
+ );
+ return 1;
+ }
+ }
+ if ($updateErrors) {
+ return 255;
+ }
+ return 0;
+ }
+}
diff --git a/lib/Fetcher/FeedFetcher.php b/lib/Fetcher/FeedFetcher.php
index ef232eece..5b48070d5 100755
--- a/lib/Fetcher/FeedFetcher.php
+++ b/lib/Fetcher/FeedFetcher.php
@@ -18,6 +18,9 @@ use Favicon\Favicon;
use FeedIo\Feed\ItemInterface;
use FeedIo\FeedInterface;
use FeedIo\FeedIo;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\RequestException;
+use GuzzleHttp\Exception\ConnectException;
use Net_URL2;
use OCP\IL10N;
@@ -318,14 +321,15 @@ class FeedFetcher implements IFeedFetcher
/**
* Return the favicon for a given feed and url
*
- * @param FeedInterface $feed Feed to check for a logo
- * @param string $url Original URL for the feed
+ * @param FeedInterface $feed Feed to check for a logo
+ * @param string $url Original URL for the feed
*
* @return string|mixed|bool
*/
protected function getFavicon(FeedInterface $feed, string $url)
{
- $favicon = $feed->getLogo();
+ // trim the string because authors do funny things
+ $favicon = trim($feed->getLogo());
ini_set('user_agent', 'NextCloud-News/1.0');
@@ -334,17 +338,54 @@ class FeedFetcher implements IFeedFetcher
$base_url = $base_url->getNormalizedURL();
// check if feed has a logo entry
- if (is_null($favicon) || trim($favicon) === '') {
+ if (is_null($favicon) || $favicon === '') {
return $this->faviconFactory->get($base_url);
}
- $favicon_path = join("/", [$this->ITempManager->getTempBaseDir(), basename($favicon)]);
+ // logo will be saved in the tmp folder provided by Nextcloud, file is named as md5 of the url
+ $favicon_path = join(DIRECTORY_SEPARATOR, [$this->ITempManager->getTempBaseDir(), md5($favicon)]);
+ $downloaded = false;
- $downloaded = copy(
- $favicon,
- $favicon_path,
- stream_context_create([ 'http' => [ 'ignore_errors' => true ] ])
- );
+ if (file_exists($favicon_path)) {
+ $last_modified = filemtime($favicon_path);
+ } else {
+ $last_modified = 0;
+ }
+
+ try {
+ // Base_uri can only be set on creation, will be used when link is relative.
+ $client = new Client(['base_uri' => $base_url]);
+ $response = $client->request(
+ 'GET',
+ $favicon,
+ [
+ 'sink' => $favicon_path,
+ 'headers' => [
+ 'User-Agent' => 'NextCloud-News/1.0',
+ 'Accept' => 'image/*',
+ 'If-Modified-Since' => date(DateTime::RFC7231, $last_modified)
+ ]
+ ]
+ );
+ $downloaded = true;
+
+ $this->logger->debug(
+ "Feed:{url} Logo:{logo} Status:{status}",
+ [
+ 'status' => $response->getStatusCode(),
+ 'url' => $favicon_path,
+ 'logo' => $favicon
+ ]
+ );
+ } catch (RequestException | ConnectException $e) {
+ $this->logger->info(
+ 'An error occurred while trying to download the feed logo of {url}: {error}',
+ [
+ 'url' => $url,
+ 'error' => $e->getResponse() ?? 'Unknown'
+ ]
+ );
+ }
$is_image = $downloaded && substr(mime_content_type($favicon_path), 0, 5) === "image";
diff --git a/lib/Scraper/Scraper.php b/lib/Scraper/Scraper.php
index 65a138799..9d1c3ffe7 100644
--- a/lib/Scraper/Scraper.php
+++ b/lib/Scraper/Scraper.php
@@ -61,7 +61,7 @@ class Scraper implements IScraper
{
list($content, $redirected_url) = $this->getHTTPContent($url);
if ($content === false) {
- $this->logger->error('Unable to recive content from {url}', [
+ $this->logger->error('Unable to receive content from {url}', [
'url' => $url,
]);
$this->readability = null;