pub / yt2rss

Transforms a youtube channel into a podcast RSS feed to insert into a podcatcher
git clone https://src.jayvii.de/pub/yt2rss.git
Home | Log | Files | Exports | Refs | Submodules | README | LICENSE | RSS

commit 662aa3c36a67d9e80df387840bd384c71f677e9c
parent 35835d652211783a44049222c38f97cb6caf1f4e
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sat,  7 Dec 2024 11:53:32 +0100

feat: fetch channel profile image instead of first video thumbnail

Diffstat:
Mindex.php | 49+++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 41 insertions(+), 8 deletions(-)

diff --git a/index.php b/index.php @@ -20,6 +20,32 @@ function ytdl_status($video_id) { return strlen($pid) > 0; } +function get_yt_profilepic($channel_id) { + $profile_pic_link = null; + if (empty($profile_pic_link)) { + $html = explode( + PHP_EOL, + file_get_contents("https://www.youtube.com/channel/" . $channel_id) + ); + if (!empty($html)) { + $profile_pic_lines = preg_grep( + '/yt3\.googleusercontent\.com\//', + $html + ); + $profile_pic_links = preg_replace( + '/^.*\"(https:\/\/yt3\.googleusercontent\.com\/[^\"]+).*$/', + '${1}', + $profile_pic_lines + ); + if (count($profile_pic_links) > 0) { + $key = array_keys($profile_pic_links)[0]; + $profile_pic_link = $profile_pic_links[$key]; + } + } + } + return $profile_pic_link; +} + // Authentification if ($_GET["auth"] != $auth_key && !is_null($auth_key)) { $auth = false; @@ -96,14 +122,21 @@ if (!empty($channel) && $auth) { $rss_xml = $rss_xml . "<pubDate>" . $channel_xml["published"] . "</pubDate>\n"; - // FIXME: fetch channel image rather than first video image - $video_id = str_replace( - array("yt_video:"), - "", - $channel_xml["entry"][0]["id"] - ); - $rss_xml = $rss_xml . "<itunes:image href=\"https://i4.ytimg.com/vi/" . - $video_id . "/hqdefault.jpg\"/>\n"; + // fetch channel image from HTML of channel page + // As fallback, use latest video thumbnail + $profile_pic_link = get_yt_profilepic($channel); + if (empty($profile_pic_link)) { + $video_id = str_replace( + array("yt_video:"), + "", + $channel_xml["entry"][0]["id"] + ); + $profile_pic_link = "https://i4.ytimg.com/vi/" . $video_id . + "/hqdefault.jpg"; + } + $rss_xml = $rss_xml . "<itunes:image href=\"" . + $profile_pic_link . + "\"/>\n"; $rss_xml = $rss_xml . "<atom:link href=\"https://" . $_SERVER["SERVER_NAME"] . "/?channel=" . $channel;