diff options
author | Benjamin Brahmer <info@b-brahmer.de> | 2021-11-15 19:11:33 +0100 |
---|---|---|
committer | Benjamin Brahmer <info@b-brahmer.de> | 2021-11-15 19:11:33 +0100 |
commit | 1e9225c662a0d4418f03a52a50a0cf4ad4259b34 (patch) | |
tree | bf6a89216fd3dd90fc2722f3acbd41678d29e35c /lib | |
parent | 9e2c2b5f541893565a2af3ca9e34d83156ed82ab (diff) | |
parent | 40b49b77de50b8adedbe7da93cb0c63c62187b3f (diff) |
Merge branch 'master' into stable16stable16
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/Updater/UpdateUser.php | 70 | ||||
-rwxr-xr-x | lib/Fetcher/FeedFetcher.php | 61 | ||||
-rw-r--r-- | lib/Scraper/Scraper.php | 2 |
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; |