1
0
mirror of https://github.com/RSS-Bridge/rss-bridge.git synced 2025-01-17 14:18:35 +01:00

[YouTubeCommunityTabBridge] Improve building of content & title (#4089)

* [YouTubeCommunityTabBridge] Improve building of content & title

Fixes truncated link hrefs in content and adds some general
improvements regarding the building of item content and item title

* [YouTubeCommunityTabBridge] Fix PHP deprecation warnings

Fixes the following deprecation warnings:

substr(): Passing null to parameter #1 ($string) of type string is
deprecated
This commit is contained in:
Thomas 2024-04-26 18:47:06 +02:00 committed by GitHub
parent 154b8b9cdb
commit d31f20758c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -204,7 +204,15 @@ class YouTubeCommunityTabBridge extends BridgeAbstract
$text = '';
foreach ($runs as $part) {
$text .= $this->formatUrls($part->text);
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);
}
}
return nl2br($text);
@ -275,6 +283,7 @@ EOD;
{
$length = 100;
$text = strip_tags($text);
if (strlen($text) > $length) {
$text = explode('<br>', wordwrap($text, $length, '<br>'));
return $text[0] . '...';
@ -283,12 +292,26 @@ EOD;
return $text;
}
private function formatUrls($content)
private function formatUrls($content, $url)
{
return preg_replace(
'/(http[s]{0,1}\:\/\/[a-zA-Z0-9.\/\?\&=\-_]{4,})/ims',
'<a target="_blank" href="$1" target="_blank">$1</a> ',
$content
);
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;
}
}