2022-03-25 23:31:39 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class YouTubeCommunityTabBridge extends BridgeAbstract
|
|
|
|
{
|
|
|
|
const NAME = 'YouTube Community Tab Bridge';
|
|
|
|
const URI = 'https://www.youtube.com';
|
|
|
|
const DESCRIPTION = 'Returns posts from a channel\'s community tab';
|
|
|
|
const MAINTAINER = 'VerifiedJoseph';
|
|
|
|
const PARAMETERS = [
|
|
|
|
'By channel ID' => [
|
|
|
|
'channel' => [
|
|
|
|
'name' => 'Channel ID',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'UCULkRHBdLC5ZcEQBaL0oYHQ'
|
2022-07-01 16:10:30 +03:00
|
|
|
]
|
|
|
|
],
|
2022-03-25 23:31:39 +03:00
|
|
|
'By username' => [
|
|
|
|
'username' => [
|
|
|
|
'name' => 'Username',
|
|
|
|
'type' => 'text',
|
|
|
|
'required' => true,
|
|
|
|
'exampleValue' => 'YouTubeUK'
|
2022-07-01 16:10:30 +03:00
|
|
|
],
|
|
|
|
]
|
|
|
|
];
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
const CACHE_TIMEOUT = 3600; // 1 hour
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
private $feedUrl = '';
|
|
|
|
private $feedName = '';
|
|
|
|
private $itemTitle = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
private $urlRegex = '/youtube\.com\/(channel|user|c)\/([\w]+)\/community/';
|
2024-06-24 23:32:03 +03:00
|
|
|
private $jsonRegex = '/var ytInitialData = ([^<]*);<\/script>/';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
public function detectParameters($url)
|
|
|
|
{
|
|
|
|
$params = [];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
if (preg_match($this->urlRegex, $url, $matches)) {
|
|
|
|
if ($matches[1] === 'channel') {
|
|
|
|
$params['context'] = 'By channel ID';
|
|
|
|
$params['channel'] = $matches[2];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
if ($matches[1] === 'user') {
|
|
|
|
$params['context'] = 'By username';
|
|
|
|
$params['username'] = $matches[2];
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return $params;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return null;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
public function collectData()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-03-25 23:31:39 +03:00
|
|
|
if (is_null($this->getInput('username')) === false) {
|
|
|
|
try {
|
|
|
|
$this->feedUrl = $this->buildCommunityUri($this->getInput('username'), 'c');
|
|
|
|
$html = getSimpleHTMLDOM($this->feedUrl);
|
|
|
|
} catch (Exception $e) {
|
|
|
|
$this->feedUrl = $this->buildCommunityUri($this->getInput('username'), 'user');
|
|
|
|
$html = getSimpleHTMLDOM($this->feedUrl);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
} else {
|
2022-03-25 23:31:39 +03:00
|
|
|
$this->feedUrl = $this->buildCommunityUri($this->getInput('channel'), 'channel');
|
|
|
|
$html = getSimpleHTMLDOM($this->feedUrl);
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2024-06-24 23:32:03 +03:00
|
|
|
$json = $this->extractJson($html->find('html', 0)->innertext);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
$this->feedName = $json->header->c4TabbedHeaderRenderer->title;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
if ($this->hasCommunityTab($json) === false) {
|
|
|
|
returnServerError('Channel does not have a community tab');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 01:14:30 +03:00
|
|
|
$posts = $this->getCommunityPosts($json);
|
|
|
|
foreach ($posts as $key => $post) {
|
2022-03-25 23:31:39 +03:00
|
|
|
$this->itemTitle = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
if (!isset($post->backstagePostThreadRenderer)) {
|
|
|
|
continue;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2023-07-29 01:14:30 +03:00
|
|
|
if (isset($post->backstagePostThreadRenderer->post->backstagePostRenderer)) {
|
|
|
|
$details = $post->backstagePostThreadRenderer->post->backstagePostRenderer;
|
|
|
|
} elseif (isset($post->backstagePostThreadRenderer->post->sharedPostRenderer)) {
|
|
|
|
// todo: properly extract data from this shared post
|
|
|
|
$details = $post->backstagePostThreadRenderer->post->sharedPostRenderer;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
|
|
|
$item = [];
|
2022-05-17 10:35:16 +03:00
|
|
|
$item['uri'] = self::URI . '/post/' . $details->postId;
|
2023-07-29 01:14:30 +03:00
|
|
|
$item['author'] = $details->authorText->runs[0]->text ?? null;
|
|
|
|
$item['content'] = $item['uri'];
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-15 16:18:09 +03:00
|
|
|
if (isset($details->contentText->runs)) {
|
2022-03-25 23:31:39 +03:00
|
|
|
$text = $this->getText($details->contentText->runs);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
$this->itemTitle = $this->ellipsisTitle($text);
|
2022-03-25 23:31:39 +03:00
|
|
|
$item['content'] = $text;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
$item['content'] .= $this->getAttachments($details);
|
2022-03-25 23:31:39 +03:00
|
|
|
$item['title'] = $this->itemTitle;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2023-07-02 23:56:08 +03:00
|
|
|
$date = strtotime(str_replace(' (edited)', '', $details->publishedTimeText->runs[0]->text));
|
|
|
|
if (is_int($date)) {
|
|
|
|
// subtract an increasing multiple of 60 seconds to always preserve the original order
|
|
|
|
$item['timestamp'] = $date - $key * 60;
|
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
$this->items[] = $item;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
public function getURI()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2023-07-04 17:21:14 +03:00
|
|
|
if (!empty($this->feedUrl)) {
|
|
|
|
return $this->feedUrl;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return parent::getURI();
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
public function getName()
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-03-25 23:31:39 +03:00
|
|
|
if (!empty($this->feedName)) {
|
2022-05-17 10:35:16 +03:00
|
|
|
return $this->feedName . ' - YouTube Community Tab';
|
2022-03-25 23:31:39 +03:00
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return parent::getName();
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
/**
|
|
|
|
* Build Community URI
|
|
|
|
*/
|
|
|
|
private function buildCommunityUri($value, $type)
|
|
|
|
{
|
|
|
|
return self::URI . '/' . $type . '/' . $value . '/community';
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-25 23:31:39 +03:00
|
|
|
* Extract JSON from page
|
2022-07-01 16:10:30 +03:00
|
|
|
*/
|
2022-03-25 23:31:39 +03:00
|
|
|
private function extractJson($html)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-03-25 23:31:39 +03:00
|
|
|
if (!preg_match($this->jsonRegex, $html, $parts)) {
|
2022-05-17 10:35:16 +03:00
|
|
|
returnServerError('Failed to extract data from page');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
$data = json_decode($parts[1]);
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
if ($data === false) {
|
|
|
|
returnServerError('Failed to decode extracted data');
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-05-17 10:35:16 +03:00
|
|
|
return $data;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-17 10:35:16 +03:00
|
|
|
* Check if channel has a community tab
|
2022-07-01 16:10:30 +03:00
|
|
|
*/
|
2022-05-17 10:35:16 +03:00
|
|
|
private function hasCommunityTab($json)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-05-17 10:35:16 +03:00
|
|
|
foreach ($json->contents->twoColumnBrowseResultsRenderer->tabs as $tab) {
|
2022-07-01 16:10:30 +03:00
|
|
|
if (
|
2022-03-25 23:31:39 +03:00
|
|
|
isset($tab->tabRenderer)
|
|
|
|
&& str_ends_with($tab->tabRenderer->endpoint->commandMetadata->webCommandMetadata->url, 'community')
|
2022-07-01 16:10:30 +03:00
|
|
|
) {
|
2022-03-25 23:31:39 +03:00
|
|
|
return true;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
/**
|
|
|
|
* Get community tab posts
|
|
|
|
*/
|
|
|
|
private function getCommunityPosts($json)
|
|
|
|
{
|
|
|
|
foreach ($json->contents->twoColumnBrowseResultsRenderer->tabs as $tab) {
|
2022-07-01 16:10:30 +03:00
|
|
|
if (
|
2022-03-25 23:31:39 +03:00
|
|
|
isset($tab->tabRenderer)
|
|
|
|
&& str_ends_with($tab->tabRenderer->endpoint->commandMetadata->webCommandMetadata->url, 'community')
|
2022-07-01 16:10:30 +03:00
|
|
|
) {
|
2022-03-25 23:31:39 +03:00
|
|
|
return $tab->tabRenderer->content->sectionListRenderer->contents[0]->itemSectionRenderer->contents;
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-25 23:31:39 +03:00
|
|
|
* Get text content for a post
|
2022-07-01 16:10:30 +03:00
|
|
|
*/
|
2022-03-25 23:31:39 +03:00
|
|
|
private function getText($runs)
|
2022-07-01 16:10:30 +03:00
|
|
|
{
|
2022-03-25 23:31:39 +03:00
|
|
|
$text = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
foreach ($runs as $part) {
|
2024-04-26 19:47:06 +03:00
|
|
|
if (isset($part->navigationEndpoint->browseEndpoint->canonicalBaseUrl)) {
|
|
|
|
$text .= $this->formatUrls($part->text, $part->navigationEndpoint->browseEndpoint->canonicalBaseUrl);
|
|
|
|
} elseif (isset($part->navigationEndpoint->urlEndpoint->url)) {
|
|
|
|
$text .= $this->formatUrls($part->text, $part->navigationEndpoint->urlEndpoint->url);
|
|
|
|
} elseif (isset($part->navigationEndpoint->commandMetadata->webCommandMetadata->url)) {
|
|
|
|
$text .= $this->formatUrls($part->text, $part->navigationEndpoint->commandMetadata->webCommandMetadata->url);
|
|
|
|
} else {
|
|
|
|
$text .= $this->formatUrls($part->text, null);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
}
|
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return nl2br($text);
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
/**
|
|
|
|
* Get attachments for posts
|
|
|
|
*/
|
|
|
|
private function getAttachments($details)
|
|
|
|
{
|
|
|
|
$content = '';
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
if (isset($details->backstageAttachment)) {
|
|
|
|
$attachments = $details->backstageAttachment;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
if (isset($attachments->videoRenderer) && isset($attachments->videoRenderer->videoId)) {
|
2024-05-02 20:45:04 +03:00
|
|
|
// Video
|
2022-03-25 23:31:39 +03:00
|
|
|
if (empty($this->itemTitle)) {
|
|
|
|
$this->itemTitle = $this->feedName . ' posted a video';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
$content = <<<EOD
|
|
|
|
<iframe width="100%" height="410" src="https://www.youtube.com/embed/{$attachments->videoRenderer->videoId}"
|
|
|
|
frameborder="0" allow="encrypted-media;" allowfullscreen></iframe>
|
|
|
|
EOD;
|
2024-05-02 20:45:04 +03:00
|
|
|
} elseif (isset($attachments->backstageImageRenderer)) {
|
|
|
|
// Image
|
2022-03-25 23:31:39 +03:00
|
|
|
if (empty($this->itemTitle)) {
|
|
|
|
$this->itemTitle = $this->feedName . ' posted an image';
|
|
|
|
}
|
|
|
|
|
|
|
|
$lastThumb = end($attachments->backstageImageRenderer->image->thumbnails);
|
|
|
|
|
|
|
|
$content = <<<EOD
|
|
|
|
<p><img src="{$lastThumb->url}"></p>
|
|
|
|
EOD;
|
2024-05-02 20:45:04 +03:00
|
|
|
} elseif (isset($attachments->pollRenderer)) {
|
|
|
|
// Poll
|
2022-03-25 23:31:39 +03:00
|
|
|
if (empty($this->itemTitle)) {
|
|
|
|
$this->itemTitle = $this->feedName . ' posted a poll';
|
|
|
|
}
|
|
|
|
|
|
|
|
$pollChoices = '';
|
|
|
|
|
|
|
|
foreach ($attachments->pollRenderer->choices as $choice) {
|
|
|
|
$pollChoices .= <<<EOD
|
|
|
|
<li>{$choice->text->runs[0]->text}</li>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
$content = <<<EOD
|
|
|
|
<hr><p>Poll ({$attachments->pollRenderer->totalVotes->simpleText})<br><ul>{$pollChoices}</ul><p>
|
|
|
|
EOD;
|
2024-05-02 20:45:04 +03:00
|
|
|
} elseif (isset($attachments->postMultiImageRenderer->images)) {
|
|
|
|
// Multiple images
|
|
|
|
$images = $attachments->postMultiImageRenderer->images;
|
|
|
|
|
|
|
|
if (is_array($images)) {
|
|
|
|
if (empty($this->itemTitle)) {
|
|
|
|
$this->itemTitle = $this->feedName . ' posted ' . count($images) . ' images';
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($images as $image) {
|
|
|
|
$lastThumb = end($image->backstageImageRenderer->image->thumbnails);
|
|
|
|
|
|
|
|
$content .= <<<EOD
|
|
|
|
<p><img src="{$lastThumb->url}"></p>
|
|
|
|
EOD;
|
|
|
|
}
|
|
|
|
}
|
2022-03-25 23:31:39 +03:00
|
|
|
}
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return $content;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
/*
|
|
|
|
Ellipsis text for title
|
|
|
|
*/
|
|
|
|
private function ellipsisTitle($text)
|
|
|
|
{
|
|
|
|
$length = 100;
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-04-26 19:47:06 +03:00
|
|
|
$text = strip_tags($text);
|
2022-03-25 23:31:39 +03:00
|
|
|
if (strlen($text) > $length) {
|
|
|
|
$text = explode('<br>', wordwrap($text, $length, '<br>'));
|
|
|
|
return $text[0] . '...';
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2022-03-25 23:31:39 +03:00
|
|
|
return $text;
|
|
|
|
}
|
2022-07-01 16:10:30 +03:00
|
|
|
|
2024-04-26 19:47:06 +03:00
|
|
|
private function formatUrls($content, $url)
|
2022-03-25 23:31:39 +03:00
|
|
|
{
|
2024-04-26 19:47:06 +03:00
|
|
|
if (substr(strval($url), 0, 1) == '/') {
|
|
|
|
// fix relative URL
|
|
|
|
$url = 'https://www.youtube.com' . $url;
|
|
|
|
} elseif (substr(strval($url), 0, 33) == 'https://www.youtube.com/redirect?') {
|
|
|
|
// extract actual URL from YouTube redirect
|
|
|
|
parse_str(substr($url, 33), $params);
|
|
|
|
if (strpos(($params['q'] ?? ''), rtrim($content, '.')) === 0) {
|
|
|
|
$url = $params['q'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ensure all URLs are made clickable
|
|
|
|
$url = $url ?? $content;
|
|
|
|
|
|
|
|
if (filter_var($url, FILTER_VALIDATE_URL)) {
|
|
|
|
return '<a href="' . $url . '" target="_blank">' . $content . '</a>';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
2022-03-25 23:31:39 +03:00
|
|
|
}
|
|
|
|
}
|