From 5f37c72be0a994257b01a8c43c4e488611f092f2 Mon Sep 17 00:00:00 2001 From: Dag Date: Fri, 13 Oct 2023 20:48:08 +0200 Subject: [PATCH] fix(binance): plus some other tweaks (#3753) --- bridges/BinanceBridge.php | 59 +++++++++------------------- bridges/CssSelectorComplexBridge.php | 4 +- bridges/FallGuysBridge.php | 2 +- bridges/InstagramBridge.php | 11 ++++-- bridges/MastodonBridge.php | 2 +- bridges/PokemonTVBridge.php | 9 ++++- lib/FeedExpander.php | 1 + lib/FeedItem.php | 10 +++-- lib/logger.php | 1 + 9 files changed, 46 insertions(+), 53 deletions(-) diff --git a/bridges/BinanceBridge.php b/bridges/BinanceBridge.php index 03449fb1..5aec0604 100644 --- a/bridges/BinanceBridge.php +++ b/bridges/BinanceBridge.php @@ -8,48 +8,27 @@ class BinanceBridge extends BridgeAbstract const MAINTAINER = 'thefranke'; const CACHE_TIMEOUT = 3600; // 1h + public function collectData() + { + $url = 'https://www.binance.com/bapi/composite/v1/public/content/blog/list?category=&tag=&page=1&size=12'; + $json = getContents($url); + $data = Json::decode($json, false); + foreach ($data->data->blogList as $post) { + $item = []; + $item['title'] = $post->title; + // Url slug not in json + //$item['uri'] = $uri; + $item['timestamp'] = $post->postTimeUTC / 1000; + $item['author'] = 'Binance'; + $item['content'] = $post->brief; + //$item['categories'] = $category; + $item['uid'] = $post->idStr; + $this->items[] = $item; + } + } + public function getIcon() { return 'https://bin.bnbstatic.com/static/images/common/favicon.ico'; } - - public function collectData() - { - $html = getSimpleHTMLDOM(self::URI) - or returnServerError('Could not fetch Binance blog data.'); - - $appData = $html->find('script[id="__APP_DATA"]'); - $appDataJson = json_decode($appData[0]->innertext); - $allposts = $appDataJson->routeProps->f3ac->blogListRes->list; - - foreach ($allposts as $element) { - $date = $element->releasedTime; - $title = $element->title; - $category = $element->category->name; - - $suburl = strtolower($category); - $suburl = str_replace(' ', '_', $suburl); - - $uri = self::URI . '/' . $suburl . '/' . $element->idStr; - - $contentHTML = getSimpleHTMLDOMCached($uri); - $contentAppData = $contentHTML->find('script[id="__APP_DATA"]'); - $contentAppDataJson = json_decode($contentAppData[0]->innertext); - $content = $contentAppDataJson->routeProps->a106->blogDetail->content; - - $item = []; - $item['title'] = $title; - $item['uri'] = $uri; - $item['timestamp'] = substr($date, 0, -3); - $item['author'] = 'Binance'; - $item['content'] = $content; - $item['categories'] = $category; - - $this->items[] = $item; - - if (count($this->items) >= 10) { - break; - } - } - } } diff --git a/bridges/CssSelectorComplexBridge.php b/bridges/CssSelectorComplexBridge.php index c09db74a..e661fe18 100644 --- a/bridges/CssSelectorComplexBridge.php +++ b/bridges/CssSelectorComplexBridge.php @@ -224,7 +224,7 @@ class CssSelectorComplexBridge extends BridgeAbstract { if (!empty($url_pattern)) { $url_pattern = '/' . str_replace('/', '\/', $url_pattern) . '/'; - $links = array_filter($links, function ($url) { + $links = array_filter($links, function ($url) use ($url_pattern) { return preg_match($url_pattern, $url) === 1; }); } @@ -359,7 +359,7 @@ class CssSelectorComplexBridge extends BridgeAbstract $article_content = $entry_html->find($content_selector, 0); if (is_null($article_content)) { - returnClientError('Could not article content at URL: ' . $entry_url); + returnClientError('Could not get article content at URL: ' . $entry_url); } $article_content = defaultLinkTo($article_content, $entry_url); diff --git a/bridges/FallGuysBridge.php b/bridges/FallGuysBridge.php index dbb34792..1e527484 100644 --- a/bridges/FallGuysBridge.php +++ b/bridges/FallGuysBridge.php @@ -85,7 +85,7 @@ class FallGuysBridge extends BridgeAbstract for ($i = 0; $i < count($mediaOptions); $i++) { if (property_exists($mediaOptions[$i], 'youtubeVideo')) { $videoUrl = 'https://youtu.be/' . $mediaOptions[$i]->youtubeVideo->contentId; - $image = $mainContentOptions[$i]->image->src; + $image = $mainContentOptions[$i]->image->src ?? ''; $content .= '

'; diff --git a/bridges/InstagramBridge.php b/bridges/InstagramBridge.php index 1714a691..633d6080 100644 --- a/bridges/InstagramBridge.php +++ b/bridges/InstagramBridge.php @@ -121,6 +121,9 @@ class InstagramBridge extends BridgeAbstract $directLink = !is_null($this->getInput('direct_links')) && $this->getInput('direct_links'); $data = $this->getInstagramJSON($this->getURI()); + if (!$data) { + return; + } if (!is_null($this->getInput('u'))) { $userMedia = $data->data->user->edge_owner_to_timeline_media->edges; @@ -286,9 +289,11 @@ class InstagramBridge extends BridgeAbstract $html = getContents($uri); $scriptRegex = '/window\._sharedData = (.*);<\/script>/'; - preg_match($scriptRegex, $html, $matches, PREG_OFFSET_CAPTURE, 0); - - return json_decode($matches[1][0]); + $ret = preg_match($scriptRegex, $html, $matches, PREG_OFFSET_CAPTURE); + if ($ret) { + return json_decode($matches[1][0]); + } + return null; } } diff --git a/bridges/MastodonBridge.php b/bridges/MastodonBridge.php index 01d425b4..e673bf14 100644 --- a/bridges/MastodonBridge.php +++ b/bridges/MastodonBridge.php @@ -193,7 +193,7 @@ class MastodonBridge extends BridgeAbstract && preg_match('/^http(s|):\/\//', $attachment['url'], $match) ) { $item['content'] = $item['content'] . '
', $attachment['url']); diff --git a/bridges/PokemonTVBridge.php b/bridges/PokemonTVBridge.php index 2c6b8bdc..a4c0a4be 100644 --- a/bridges/PokemonTVBridge.php +++ b/bridges/PokemonTVBridge.php @@ -64,15 +64,20 @@ class PokemonTVBridge extends BridgeAbstract continue; } } - switch ($element->{'media_type'}) { + switch ($element->media_type) { case 'movie': - $itemtitle = $element->{'channel_name'}; + case 'junior': + case 'original': + case 'non-animation': + $itemtitle = $element->channel_name; break; case 'episode': $season = str_pad($mediaelement->{'season'}, 2, '0', STR_PAD_LEFT); $episode = str_pad($mediaelement->{'episode'}, 2, '0', STR_PAD_LEFT); $itemtitle = $element->{'channel_name'} . ' - S' . $season . 'E' . $episode; break; + default: + $itemtitle = ''; } $streamurl = 'https://watch.pokemon.com/' . $this->getCountryCode() . '/#/player?id=' . $mediaelement->{'id'}; $item = []; diff --git a/lib/FeedExpander.php b/lib/FeedExpander.php index 361df4d9..056578e9 100644 --- a/lib/FeedExpander.php +++ b/lib/FeedExpander.php @@ -24,6 +24,7 @@ abstract class FeedExpander extends BridgeAbstract } // prepare/massage the xml to make it more acceptable $badStrings = [ + ' ', '»', ]; $xmlString = str_replace($badStrings, '', $xmlString); diff --git a/lib/FeedItem.php b/lib/FeedItem.php index 0bc22ee2..bd37f119 100644 --- a/lib/FeedItem.php +++ b/lib/FeedItem.php @@ -260,13 +260,15 @@ class FeedItem return $this->uid; } - public function setUid($uid) + public function setUid($uid): void { $this->uid = null; if (!is_string($uid)) { - Debug::log('Unique id must be a string!'); - } elseif (preg_match('/^[a-f0-9]{40}$/', $uid)) { - // keep id if it already is SHA-1 hash + Debug::log(sprintf('uid must be string: %s (%s)', (string) $uid, var_export($uid, true))); + return; + } + if (preg_match('/^[a-f0-9]{40}$/', $uid)) { + // Preserve sha1 hash $this->uid = $uid; } else { $this->uid = sha1($uid); diff --git a/lib/logger.php b/lib/logger.php index e41de34b..7a902b5b 100644 --- a/lib/logger.php +++ b/lib/logger.php @@ -149,6 +149,7 @@ final class StreamHandler ); error_log($text); if ($record['level'] < Logger::ERROR && Debug::isEnabled()) { + // Not a good idea to print here because http headers might not have been sent print sprintf("

%s
\n", e($text)); } //$bytes = file_put_contents('/tmp/rss-bridge.log', $text, FILE_APPEND | LOCK_EX);