Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions xExtension-YouTube/extension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

/**
* Class YouTubeExtension
*
Expand Down Expand Up @@ -61,6 +63,20 @@ public function isYtFeed(string $website): bool {
return str_starts_with($website, 'https://www.youtube.com/');
}

public function isShort(string $website): bool {
return str_starts_with($website, 'https://www.youtube.com/shorts');
}
public function convertShortToWatch(string $shortUrl): string {
$prefix = 'https://www.youtube.com/shorts/';

if (str_starts_with($shortUrl, $prefix)) {
$videoId = str_replace($prefix, '', $shortUrl);
return 'https://www.youtube.com/watch?v=' . $videoId;
}

return $shortUrl;
}

public function iconBtnUrl(FreshRSS_Feed $feed): ?string {
if (!$this->isYtFeed($feed->website()) || $feed->attributeString('customFaviconExt') === $this->getName()) {
return null;
Expand Down Expand Up @@ -141,7 +157,7 @@ public function resetAllIcons(): void {
try {
$feed->resetCustomFavicon(values: $v);
} catch (FreshRSS_Feed_Exception $_) {
$this->warnLog('failed to reset favicon for feed "' . $feed->name(true) . '": feed error');
$this->warnLog('Failed to reset favicon for feed ' . $feed->name(true) . ': feed error!');
}
}
}
Expand Down Expand Up @@ -194,17 +210,17 @@ public function setIconForFeed(FreshRSS_Feed $feed, bool $setValues = false): Fr
$feed->_attributes($oldAttributes);
return $feed;
} elseif (file_exists($path)) {
$this->debugLog('icon had already been downloaded before for feed "' . $feed->name(true) . '" - returning early!');
$this->debugLog('Icon had already been downloaded before for feed ' . $feed->name(true) . '”: returning early!');
return $feed;
}
} catch (FreshRSS_Feed_Exception $_) {
$this->warnLog('failed to set custom favicon for feed "' . $feed->name(true) . '": feed error');
$this->warnLog('Failed to set custom favicon for feed ' . $feed->name(true) . ': feed error!');
$feed->_attributes($oldAttributes);
return $feed;
}

$feed->_attributes($oldAttributes);
$this->debugLog('downloading icon for feed "' . $feed->name(true) . '"');
$this->debugLog('downloading icon for feed ' . $feed->name(true) . '"');

$url = $feed->website();
/** @var array<int, bool|int|string> */
Expand All @@ -229,43 +245,44 @@ public function setIconForFeed(FreshRSS_Feed $feed, bool $setValues = false): Fr
$dom = new DOMDocument();

if (!is_string($html) || !@$dom->loadHTML($html, LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING)) {
$this->warnLog('fail while downloading icon for feed "' . $feed->name(true) . '": failed to load HTML');
$this->warnLog('Fail while downloading icon for feed ' . $feed->name(true) . ': failed to load HTML!');
return $feed;
}

$xpath = new DOMXPath($dom);
$iconElem = $xpath->query('//meta[@name="twitter:image"]');
$metaElem = $xpath->query('//meta[@name="twitter:image"]');

if ($iconElem === false) {
$this->warnLog('fail while downloading icon for feed "' . $feed->name(true) . '": icon URL couldn\'t be found');
if ($metaElem === false) {
$this->warnLog('Fail while downloading icon for feed ' . $feed->name(true) . ': icon URL couldnt be found!');
return $feed;
}
$iconElem = $metaElem->item(0);

if (!($iconElem->item(0) instanceof DOMElement)) {
$this->warnLog('fail while downloading icon for feed "' . $feed->name(true) . '": icon URL couldn\'t be found');
if (!($iconElem instanceof DOMElement)) {
$this->warnLog('Fail while downloading icon for feed ' . $feed->name(true) . ': icon URL couldnt be found!');
return $feed;
}

$iconUrl = $iconElem->item(0)->getAttribute('content');
$iconUrl = $iconElem->getAttribute('content');
if ($iconUrl == '') {
$this->warnLog('fail while downloading icon for feed "' . $feed->name(true) . '": icon URL is empty');
$this->warnLog('Fail while downloading icon for feed ' . $feed->name(true) . ': icon URL is empty!');
return $feed;
}

curl_setopt($ch, CURLOPT_URL, $iconUrl);
$contents = curl_exec($ch);
if (!is_string($contents)) {
$this->warnLog('fail while downloading icon for feed "' . $feed->name(true) . '": empty contents');
$this->warnLog('Fail while downloading icon for feed ' . $feed->name(true) . ': empty contents!');
return $feed;
}

try {
$feed->setCustomFavicon($contents, extName: $this->getName(), disallowDelete: true, values: $v, overrideCustomIcon: true);
} catch (FreshRSS_UnsupportedImageFormat_Exception $_) {
$this->warnLog('failed to set custom favicon for feed "' . $feed->name(true) . '": unsupported image format');
$this->warnLog('Failed to set custom favicon for feed ' . $feed->name(true) . ': unsupported image format!');
return $feed;
} catch (FreshRSS_Feed_Exception $_) {
$this->warnLog('failed to set custom favicon for feed "' . $feed->name(true) . '": feed error');
$this->warnLog('Failed to set custom favicon for feed ' . $feed->name(true) . ': feed error!');
return $feed;
}

Expand Down Expand Up @@ -377,6 +394,10 @@ public function embedYouTubeVideo(FreshRSS_Entry $entry): FreshRSS_Entry
{
$link = $entry->link();

if ($this->isShort($link)) {
$link = $this->convertShortToWatch($link);
}

if (preg_match('#^https?://www\.youtube\.com/watch\?v=|/videos/watch/[0-9a-f-]{36}$#', $link) !== 1) {
return $entry;
}
Expand All @@ -386,6 +407,7 @@ public function embedYouTubeVideo(FreshRSS_Entry $entry): FreshRSS_Entry
if (stripos($entry->content(), '<iframe class="youtube-plugin-video"') !== false) {
return $entry;
}

if (stripos($link, 'www.youtube.com/watch?v=') !== false) {
$html = $this->getHtmlContentForLink($entry, $link);
}
Expand All @@ -394,7 +416,6 @@ public function embedYouTubeVideo(FreshRSS_Entry $entry): FreshRSS_Entry
}

$entry->_content($html);

return $entry;
}

Expand Down
2 changes: 1 addition & 1 deletion xExtension-YouTube/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "YouTube Video Feed",
"author": "Kevin Papst, Inverle",
"description": "Embed YouTube feeds inside article content.",
"version": "0.14",
"version": "0.2",
"entrypoint": "YouTube",
"type": "user"
}