pub / yt2html

Fetches Youtube content via RSS and provides a chronological timeline
git clone https://src.jayvii.de/pub/yt2html.git
Home | Log | Files | Exports | Refs | README | RSS

commit 788e9ffab9056ff734cec9d0109b37662c61c753
parent 3c2f61feafd4554ba1cacf55fd9712c33a3f709c
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Sat,  5 Apr 2025 16:08:59 +0200

feat: resolve usernames to channel-ids

Diffstat:
Mindex.php | 46+++++++++++++++++++++++++++++++++++++++++++---
1 file changed, 43 insertions(+), 3 deletions(-)

diff --git a/index.php b/index.php @@ -31,6 +31,30 @@ return strcmp(strtolower($a[$col]), strtolower($b[$col])); } + /* Get Channel ID */ + function get_yt_channelid($channel_name) { + // set fallback for channel id + $channelid = $channel_name; + // fetch HTML for given channel ID + $html = explode( + PHP_EOL, + file_get_contents("https://www.youtube.com/" . $channel_name) + ); + // If HTML could be fetched, extract lines with probable profile picture + if (!empty($html)) { + $channel_id_lines = preg_grep( + '/videos.xml\?channel_id\=/', + $html + ); + $channelid = preg_replace( + '/^.*\"https:\/\/www\.youtube\.com\/feeds\/videos\.xml\?channel_id\=([^\"]+).*$/', + '${1}', + $channel_id_lines + ); + } + // return the channel id + return array_values($channelid)[0]; + } /* Initilise videos template */ $video_template = array( @@ -38,6 +62,7 @@ "title" => array(), "desc" => array(), "time" => array(), + "views" => array(), "aid" => array(), "author" => array() ); @@ -57,13 +82,20 @@ } if (!is_null($_POST["channels"])) { $channels = urldecode($_POST["channels"]); - $channels = preg_replace('/[^A-Za-z0-9\-\_\,]+/', '', $channels); + $channels = preg_replace('/[^A-Za-z0-9\-\_\,\@]+/', '', $channels); $channels = explode(",", $channels); } if (!is_null($_GET["channels"])) { $channels = explode(",", $_GET["channels"]); } + // replace usernames with channel-ids + $channel_names = preg_grep('/^@/', $channels); + foreach ($channel_names as $channel_name) { + array_push($channels, get_yt_channelid($channel_name)); + } + $channels = array_values(preg_grep('/^[^@]/', $channels)); + // Sort channels by alphabet and ensure each channel is unique $channels = array_unique($channels); @@ -144,6 +176,11 @@ // Get Time $video["time"] = strtotime($entry["published"]); + // Get Views + $video["views"] = number_format( + $entry["media_group"]["media_community"]["media_statistics"]["@attributes"]["views"] + ); + // Get Channel ID $video["aid"] = $channel; @@ -229,7 +266,8 @@ <summary>Import / Export</summary> <p> Please enter the YouTube channel-IDs you want to check here, each - separated with a <code>,</code>: + separated with a <code>,</code>. You may also apply Usernames, + starting with a <code>@</code>: </p> <input name="channels" @@ -272,6 +310,7 @@ name="channel<?php echo ($i); ?>" class="channels_input" type="text" + placeholder="@username" oninput="yt2html_update_channels_list();" > </div> @@ -298,7 +337,8 @@ <!-- Video Information --> <div style="width:100%;margin-bottom:1em;color:var(--border);"> <span style="margin-right:0.25em;"><?php echo $video["author"]; ?></span> - <span style="margin-left:0.25em;"><?php echo date("Y-m-d, H:i:s", $video["time"]); ?></span> + <span style="margin-left:0.25em;margin-right:0.25em;"><?php echo date("Y-m-d, H:i:s", $video["time"]); ?></span> + <span style="margin-left:0.25em;"><?php echo "(" . $video["views"] . " Views)"; ?></span> </div> <!-- player Container -->