From cb6c931b1f62b80634b73af63ace9693bc3cbe76 Mon Sep 17 00:00:00 2001 From: Dag Date: Sat, 23 Sep 2023 17:50:41 +0200 Subject: [PATCH] fix(duckduckgo): order by date (#3689) --- bridges/AwwwardsBridge.php | 38 ++++++++++++++++++------------------ bridges/DuckDuckGoBridge.php | 15 +++++++++++--- bridges/EngadgetBridge.php | 11 ++++++++--- bridges/JustWatchBridge.php | 13 ++++++------ bridges/YandexZenBridge.php | 10 ++++++---- bridges/YoutubeBridge.php | 5 ++++- 6 files changed, 55 insertions(+), 37 deletions(-) diff --git a/bridges/AwwwardsBridge.php b/bridges/AwwwardsBridge.php index a1adfede..849d04d5 100644 --- a/bridges/AwwwardsBridge.php +++ b/bridges/AwwwardsBridge.php @@ -14,29 +14,10 @@ class AwwwardsBridge extends BridgeAbstract private $sites = []; - public function getIcon() - { - return 'https://www.awwwards.com/favicon.ico'; - } - - private function fetchSites() - { - Debug::log('Fetching all sites'); - $sites = getSimpleHTMLDOM(self::SITESURI); - - Debug::log('Parsing all JSON data'); - foreach ($sites->find('.grid-sites li') as $site) { - $decode = html_entity_decode($site->attr['data-collectable-model-value'], ENT_QUOTES, 'utf-8'); - $decode = json_decode($decode, true); - $this->sites[] = $decode; - } - } - public function collectData() { $this->fetchSites(); - Debug::log('Building RSS feed'); foreach ($this->sites as $site) { $item = []; $item['title'] = $site['title']; @@ -56,4 +37,23 @@ class AwwwardsBridge extends BridgeAbstract } } } + + public function getIcon() + { + return 'https://www.awwwards.com/favicon.ico'; + } + + private function fetchSites() + { + $sites = getSimpleHTMLDOM(self::SITESURI); + foreach ($sites->find('.grid-sites li') as $li) { + $encodedJson = $li->attr['data-collectable-model-value'] ?? null; + if (!$encodedJson) { + continue; + } + $json = html_entity_decode($encodedJson, ENT_QUOTES, 'utf-8'); + $site = Json::decode($json); + $this->sites[] = $site; + } + } } diff --git a/bridges/DuckDuckGoBridge.php b/bridges/DuckDuckGoBridge.php index 5edf248b..6f9069fa 100644 --- a/bridges/DuckDuckGoBridge.php +++ b/bridges/DuckDuckGoBridge.php @@ -8,7 +8,7 @@ class DuckDuckGoBridge extends BridgeAbstract const CACHE_TIMEOUT = 21600; // 6h const DESCRIPTION = 'Returns results from DuckDuckGo.'; - const SORT_DATE = '+sort:date'; + const SORT_DATE = ' sort:date'; const SORT_RELEVANCE = ''; const PARAMETERS = [ [ @@ -31,13 +31,22 @@ class DuckDuckGoBridge extends BridgeAbstract public function collectData() { - $html = getSimpleHTMLDOM(self::URI . 'html/?kd=-1&q=' . $this->getInput('u') . $this->getInput('sort')); + $query = [ + 'kd' => '-1', + 'q' => $this->getInput('u') . $this->getInput('sort'), + ]; + $url = 'https://duckduckgo.com/html/?' . http_build_query($query); + $html = getSimpleHTMLDOM($url); foreach ($html->find('div.result') as $element) { $item = []; $item['uri'] = $element->find('a.result__a', 0)->href; $item['title'] = $element->find('h2.result__title', 0)->plaintext; - $item['content'] = $element->find('a.result__snippet', 0)->plaintext; + + $snippet = $element->find('a.result__snippet', 0); + if ($snippet) { + $item['content'] = $snippet->plaintext; + } $this->items[] = $item; } } diff --git a/bridges/EngadgetBridge.php b/bridges/EngadgetBridge.php index 2bed4a4a..c219c0ff 100644 --- a/bridges/EngadgetBridge.php +++ b/bridges/EngadgetBridge.php @@ -10,14 +10,19 @@ class EngadgetBridge extends FeedExpander public function collectData() { - $this->collectExpandableDatas(static::URI . 'rss.xml', 15); + $max = 10; + $this->collectExpandableDatas(static::URI . 'rss.xml', $max); } protected function parseItem($newsItem) { $item = parent::parseItem($newsItem); - // $articlePage gets the entire page's contents - $articlePage = getSimpleHTMLDOM($newsItem->link); + $url = (string) $newsItem->link; + if (!$url) { + return $item; + } + // todo: remove querystring tracking + $articlePage = getSimpleHTMLDOM($url); // figure contain's the main article image $article = $articlePage->find('figure', 0); // .article-text has the actual article diff --git a/bridges/JustWatchBridge.php b/bridges/JustWatchBridge.php index 66b61aff..98dcd338 100644 --- a/bridges/JustWatchBridge.php +++ b/bridges/JustWatchBridge.php @@ -170,22 +170,21 @@ class JustWatchBridge extends BridgeAbstract $item = []; $item['uri'] = $title->find('a', 0)->href; + $posterImage = $title->find('.title-poster__image > img', 0); $itemTitle = sprintf( '%s - %s', $provider->find('picture > img', 0)->alt ?? '', - $title->find('.title-poster__image > img', 0)->alt ?? '' + $posterImage->alt ?? '' ); $item['title'] = $itemTitle; - $imageUrl = $title->find('.title-poster__image > img', 0)->attr['src'] ?? ''; + $imageUrl = $posterImage->attr['src'] ?? ''; if (str_starts_with($imageUrl, 'data')) { - $imageUrl = $title->find('.title-poster__image > img', 0)->attr['data-src']; + $imageUrl = $posterImage->attr['data-src']; } - $content = 'Provider: ' - . $provider->find('picture > img', 0)->alt . '
'; - $content .= 'Media: ' - . $title->find('.title-poster__image > img', 0)->alt . '
'; + $content = 'Provider: ' . $provider->find('picture > img', 0)->alt . '
'; + $content .= 'Media: ' . ($posterImage->alt ?? '') . '
'; if (isset($title->find('.title-poster__badge', 0)->plaintext)) { $content .= 'Type: Series
'; diff --git a/bridges/YandexZenBridge.php b/bridges/YandexZenBridge.php index ee1ff506..8a3db48b 100644 --- a/bridges/YandexZenBridge.php +++ b/bridges/YandexZenBridge.php @@ -45,10 +45,12 @@ class YandexZenBridge extends BridgeAbstract $item['timestamp'] = date(DateTimeInterface::ATOM, $publicationDateUnixTimestamp); } - $item['content'] = $post->text . "
"; - $item['enclosures'] = [ - $post->image, - ]; + $postImage = $post->image ?? null; + $item['content'] = $post->text; + if ($postImage) { + $item['content'] .= "
"; + $item['enclosures'] = [$postImage]; + } $this->items[] = $item; } diff --git a/bridges/YoutubeBridge.php b/bridges/YoutubeBridge.php index 66b7614f..418e715e 100644 --- a/bridges/YoutubeBridge.php +++ b/bridges/YoutubeBridge.php @@ -228,7 +228,10 @@ class YoutubeBridge extends BridgeAbstract return; } - $jsonData = $jsonData->contents->twoColumnWatchNextResults->results->results->contents; + $jsonData = $jsonData->contents->twoColumnWatchNextResults->results->results->contents ?? null; + if (!$jsonData) { + throw new \Exception('Unable to find json data'); + } $videoSecondaryInfo = null; foreach ($jsonData as $item) { if (isset($item->videoSecondaryInfoRenderer)) {