From 549bed64d2b2ed37e7d6b888d83795ec1ee31603 Mon Sep 17 00:00:00 2001 From: July Date: Tue, 4 Feb 2025 14:11:43 -0500 Subject: [PATCH] [YouTubeFeedExpanderBridge] Add bridge (#4430) --- bridges/YouTubeFeedExpanderBridge.php | 86 +++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 bridges/YouTubeFeedExpanderBridge.php diff --git a/bridges/YouTubeFeedExpanderBridge.php b/bridges/YouTubeFeedExpanderBridge.php new file mode 100644 index 00000000..953674ec --- /dev/null +++ b/bridges/YouTubeFeedExpanderBridge.php @@ -0,0 +1,86 @@ + [ + 'name' => 'Channel ID', + 'required' => true, + // Example: vinesauce + 'exampleValue' => 'UCzORJV8l3FWY4cFO8ot-F2w', + ], + 'embed' => [ + 'name' => 'Add embed to entry', + 'type' => 'checkbox', + 'required' => false, + 'title' => 'Add embed to entry', + 'defaultValue' => 'checked', + ], + 'embedurl' => [ + 'name' => 'Use embed page as entry url', + 'type' => 'checkbox', + 'required' => false, + 'title' => 'Use embed page as entry url', + ], + 'nocookie' => [ + 'name' => 'Use nocookie embed page', + 'type' => 'checkbox', + 'required' => false, + 'title' => 'Use nocookie embed page' + ], + ]]; + + public function getIcon() + { + if ($this->getInput('channel') != null) { + $html = getSimpleHTMLDOMCached($this->getURI()); + $scriptRegex = '/var ytInitialData = (.*?);<\/script>/'; + $result = preg_match($scriptRegex, $html, $matches); + if (isset($matches[1])) { + $json = json_decode($matches[1]); + return $json->metadata->channelMetadataRenderer->avatar->thumbnails[0]->url; + } + } + return parent::getIcon(); + } + + public function collectData() + { + $url = 'https://www.youtube.com/feeds/videos.xml?channel_id=' . $this->getInput('channel'); + $this->collectExpandableDatas($url); + } + + protected function parseItem(array $item) + { + $id = $item['yt']['videoId']; + $item['comments'] = $item['uri'] . '#comments'; + $item['uid'] = $item['id']; + + $thumbnail = sprintf('https://img.youtube.com/vi/%s/maxresdefault.jpg', $id); + $item['enclosures'] = [$thumbnail]; + + $item['content'] = $item['media']['group']['description']; + $item['content'] = str_replace("\n", '
', $item['content']); + unset($item['media']); + + $embedURI = self::URI; + if ($this->getInput('nocookie')) { + $embedURI = 'https://www.youtube-nocookie.com/'; + } + $embed = $embedURI . 'embed/' . $id; + if ($this->getInput('embed')) { + $iframe_fmt = ''; //phpcs:ignore + $iframe = sprintf($iframe_fmt, $embed, $item['title']) . '
'; + $item['content'] = $iframe . $item['content']; + } + if ($this->getInput('embedurl')) { + $item['uri'] = $embed; + } + + return $item; + } +}