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 fd4d1248c39f1dbee7712a8a9198f99935cf4563
parent 5f9299b36fc453f8a26762730e3c97eca069ccdc
Author: JayVii <jayvii[AT]posteo[DOT]de>
Date:   Mon,  9 Nov 2026 21:44:37 +0100

feat: fetch and insert videos via javascript

Diffstat:
M.htaccess | 2--
Massets/js/yt.js | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mindex.php | 183+++++++++++++++----------------------------------------------------------------
Alib/fetch_feed.php | 103+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 206 insertions(+), 152 deletions(-)

diff --git a/.htaccess b/.htaccess @@ -13,5 +13,3 @@ RedirectMatch 404 ^/.reuse/.*$ RedirectMatch 404 ^/LICENSES/.*$ RedirectMatch 404 ^/README.md$ - - diff --git a/assets/js/yt.js b/assets/js/yt.js @@ -102,3 +102,73 @@ function yt2html_add_channels_input(index) { document.getElementById("channels_list").append(label); document.getElementById("channels_list").append(input); } + +function create_video_box(data) { + + // Create section object from invisible template + var vid = document.querySelector("#video_template").cloneNode(true); + vid.id = "video_" + data["vid"]; + vid.data = data["time"]; + + // Update Headline Text and Link + vid.querySelector(".title").innerText = data["title"]; + vid.querySelector(".title").href = "https://www.youtube.com/watch?v=" + + data["vid"]; + + // Update Channel Information + vid.querySelector(".channel").innerText = data["author"]; + vid.querySelector(".channel").href = "/?channels=" + data["aid"]; + + // Update Timestamp Info + var timestamp = new Date(data["time"] * 1000); + vid.querySelector(".timestamp").innerText = timestamp; + + // Update Views Count Info + vid.querySelector(".views").innerText = "(" + data["views"] + " Views)"; + + // Update Arguments in button toggle + vid.querySelector(".toggle_video").onclick = "yt2html_toggle_player('" + + data["vid"] + "');"; + vid.querySelector(".play_button").onclick = "yt2html_toggle_player('" + + data["vid"] + "');"; + + // Update Preview Image + vid.querySelector(".preview").id = "preview_" + data["vid"]; + vid.querySelector(".preview_image").src = "https://i4.ytimg.com/vi/" + + data["vid"] + "/hqdefault.jpg"; + + // Update Description Box + vid.querySelector(".description").innerText = data["desc"]; + + // Return object + return vid; + +} + +async function fetch_and_insert_yt(channel) { + // gather feed information from API endpoint + const videos_raw = await fetch('/lib/fetch_feed.php?channel=' + channel); + + // parse gathered information + var videos = JSON.decode(videos_raw); + + // create and insert each video into the feed + for (let i=0; i < videos.length; i++) { + + // Create video object + var vid = create_video_box(videos[i]); + + // Check which videos are already present + var vids = document.querySelectorAll(".video"); + + // Run through all present videos from the top to bottom + for (let j = 0; j < vids.length; j++) { + + // If a video has a smaller (earlier) timestamp, put the video BEFORE it + if (vids[j].data < vid[data]) { + vids[j].prepend(vid); + break; + } + + } +} diff --git a/index.php b/index.php @@ -23,14 +23,6 @@ <?php - /* Comparing function for sorting */ - function compare_numeric(array $a, array $b, string $col) { - return $b[$col] - $a[$col]; - } - function compare_string(array $a, array $b, string $col) { - return strcmp(strtolower($a[$col]), strtolower($b[$col])); - } - /* Get Channel ID */ function get_yt_channelid($channel_name) { // set fallback for channel id @@ -110,108 +102,22 @@ ); } - for ($i = 0; $i < count($channels); $i++) { - $channel = $channels[$i]; - - // Fetch Youtube XML Feed - $channel_xml = file( - "https://www.youtube.com/feeds/videos.xml?channel_id=" . $channel - ); - - /* Skip to next entry, if channel could not be found */ - if ($channel_xml === false) { - /* Remove entry from channels list */ - array_splice($channels, $i, 1); - /* Skip item within loop */ - continue; - } - - // Replace un-parsable items - $channel_xml = str_replace( - array("yt:", "media:"), - array("yt_", "media_"), - $channel_xml - ); - - // Cast Array to string - $channel_xml = implode(PHP_EOL, $channel_xml); - - // Parse XML - $channel_xml = simplexml_load_string($channel_xml); - $channel_xml = json_encode($channel_xml); - $channel_xml = json_decode($channel_xml, true); - - // Get Channel name - $author = str_replace( - array("&"), - "&amp;", - $channel_xml["entry"][0]["author"]["name"] - ); - - /* Fill channels list array */ - array_push($channels_list, array("aid" => $channel, "author" => $author)); - - // Process Entries - foreach ($channel_xml["entry"] as $entry) { - - // Robustness: Skip entry if it is not an array (broken return) - if (!is_array($entry)) { - continue; - } - - // Skip entry if it is a short - $uri = $entry["link"]["@attributes"]["href"]; - if (preg_match('/\/shorts\//', $uri) > 0) { - continue; - } - - /* Copy video template array */ - $video = $video_template; - - // Get Video ID - $video["vid"] = str_replace(array("yt_video:"), "", $entry["id"]); - - // Get Video Title - $video["title"] = str_replace(array("&"), "&amp;", $entry["title"]); - - // Get Video Description - $video["desc"] = preg_replace( - "/\n+/", - "<br>", - $entry["media_group"]["media_description"] - ); - if (empty($video["desc"])) { - $video["desc"] = ""; - } - - // 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; - - // Get Channel Name - $video["author"] = $author; - - /* Add video to videos array */ - array_push($videos, $video); - - } + /* Comparing function for sorting */ + function compare_numeric(array $a, array $b, string $col) { + return $b[$col] - $a[$col]; + } + function compare_string(array $a, array $b, string $col) { + return strcmp(strtolower($a[$col]), strtolower($b[$col])); } - - /* Sort videos and channels arrays */ function cmp_time(array $a, array $b) { return compare_numeric($a, $b, "time"); } function cmp_name(array $a, array $b) { return compare_string($a, $b, "author"); } + + /* Sort videos and channels arrays */ usort($videos, "cmp_time"); usort($channels_list, "cmp_name"); @@ -313,82 +219,59 @@ </form> </details> - <!-- Video List --> + <!-- Fetch all videos via javascript and insert them into the feed --> + <script> <?php - foreach ($videos as $video) { + foreach ($channels as $channel) { + echo "fetch_and_insert_yt('" . $channel . "');"; + } ?> - <section id="video_<?php echo $video["vid"]; ?>"> + </script> + + <!-- Video Section template --> + <section class="video" id="video_template" data="0"> - <!-- Headline: streamer name --> + <!-- Headline: video name --> <h3 style="margin-bottom:5px;"> - <a - target="_blank" - href="https://www.youtube.com/watch?v=<?php echo $video["vid"]; ?>" - > - <?php echo $video["title"]; ?> - </a> + <a class="title" target="_blank" href="#"></a> </h3> <!-- Video Information --> <div style="width:100%;margin-bottom:1em;color:var(--border);"> <!-- Channel Name --> - <span style="margin-right:0.25em;"> - <a href="/?channels=<?php echo $video["aid"]; ?>"> - <?php echo $video["author"]; ?> - </a> + <span class="channel" style="margin-right:0.25em;"> + <a href="#"></a> </span> <!-- Timestamp --> - <span style="margin-left:0.25em;margin-right:0.25em;"> - <?php echo date("Y-m-d, H:i:s", $video["time"]); ?> + <span class="timestamp" style="margin-left:0.25em;margin-right:0.25em;"> </span> <!-- Views --> - <span style="margin-left:0.25em;margin-right:0.25em;"> - <?php echo "(" . $video["views"] . " Views)"; ?> + <span class="views" style="margin-left:0.25em;margin-right:0.25em;"> </span> </div> <!-- player Container --> <div class="player_container hidden"> - <?php $js_args = "'" . $video["vid"] . "'"; ?> - <button onclick="yt2html_toggle_player(<?php echo $js_args; ?>)"> - Toggle Player - </button> + <button>Toggle Player</button> </div> <!-- Preview Image --> - <div - id="preview_<?php echo $video["vid"]; ?>" - class="preview" - > - <img - src="https://i4.ytimg.com/vi/<?php echo $video["vid"]; ?>/hqdefault.jpg" - loading="lazy" - height="100%" - width="100%" - > - <div - class="play_button" - onclick="yt2html_toggle_player(<?php echo $js_args; ?>)" - > + <div class="preview"> + <img src="#" loading="lazy" height="100%" width="100%"> + <div class="play_button"> + </div> </div> - </div> - <!-- Description Collapsable --> - <details> - <summary> + <!-- Description Collapsable --> + <details> + <summary> Description - </summary> - <?php echo $video["desc"]; ?> + </summary> + <p class="description"></p> </details> </section> - <?php - - } - - ?> - <footer> <h3 id="about">About</h3> <details> diff --git a/lib/fetch_feed.php b/lib/fetch_feed.php @@ -0,0 +1,103 @@ +<?php + +/* Fetch XML Feed Channel */ +// $channel = $_GET["channel"]; +$channel_xml = file( + "https://www.youtube.com/feeds/videos.xml?channel_id=" . $channel +); + +/* Skip to next entry, if channel could not be found */ +if ($channel_xml === false) { + /* Skip item within loop */ + die(); +} + +// Replace un-parsable items +$channel_xml = str_replace( + array("yt:", "media:"), + array("yt_", "media_"), + $channel_xml +); + +// Cast Array to string +$channel_xml = implode(PHP_EOL, $channel_xml); + +// Parse XML +$channel_xml = simplexml_load_string($channel_xml); +$channel_xml = json_encode($channel_xml); +$channel_xml = json_decode($channel_xml, true); + +// Get Channel name +$author = str_replace( + array("&"), + "&amp;", + $channel_xml["entry"][0]["author"]["name"] +); + +/* Initilise Videos array */ +$videos = array(); + +// Process Entries +foreach ($channel_xml["entry"] as $entry) { + + // Robustness: Skip entry if it is not an array (broken return) + if (!is_array($entry)) { + continue; + } + + // Skip entry if it is a short + $uri = $entry["link"]["@attributes"]["href"]; + if (preg_match('/\/shorts\//', $uri) > 0) { + continue; + } + + /* Initilise videos template */ + $video = array( + "vid" => array(), + "title" => array(), + "desc" => array(), + "time" => array(), + "views" => array(), + "aid" => array(), + "author" => array() + ); + + // Get Video ID + $video["vid"] = str_replace(array("yt_video:"), "", $entry["id"]); + + // Get Video Title + $video["title"] = str_replace(array("&"), "&amp;", $entry["title"]); + + // Get Video Description + $video["desc"] = preg_replace( + "/\n+/", + "<br>", + $entry["media_group"]["media_description"] + ); + if (empty($video["desc"])) { + $video["desc"] = ""; + } + + // 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; + + // Get Channel Name + $video["author"] = $author; + + /* Add video to videos array */ + array_push($videos, $video); + +} + +// return content +echo json_encode($videos); + +?>