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

index.php (10931B)


      1 <!-- SPDX-License-Identifier: AGPL-3.0-or-later
      2      SPDX-FileCopyrightText: 2021-2024 JayVii <jayvii[AT]posteo[DOT]de>
      3 -->
      4 
      5 <!DOCTYPE html>
      6 <html lang="en">
      7   <head>
      8     <meta charset="utf-8">
      9     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
     10     <title>YouTube Videos</title>
     11     <link rel="icon" type="image/png" href="/assets/favicon.png">
     12     <link rel="icon" type="image/png" sizes="16x16" href="/assets/favicon_16.png">
     13     <link rel="icon" type="image/png" sizes="32x32" href="/assets/favicon_32.png">
     14     <link rel="icon" type="image/png" sizes="64x64" href="/assets/favicon_64.png">
     15     <link rel="icon" type="image/png" sizes="128x128" href="/assets/favicon_128.png">
     16     <link rel="apple-touch-icon" href="/assets/favicon.png">
     17     <link rel="stylesheet" type="text/css" href="/assets/css/simple.min.css"/>
     18     <link rel="stylesheet" type="text/css" href="/assets/css/custom.css"/>
     19     <script src="assets/js/yt.js"></script>
     20     <link crossorigin="use-credentials" rel="manifest" href="/manifest.json">
     21     <meta name="viewport" content="width=device-width, initial-scale=1.0">
     22   </head>
     23 
     24 <?php
     25 
     26   /* Get Channel ID */
     27   function get_yt_channelid($channel_name) {
     28       // set fallback for channel id
     29       $channelid = $channel_name;
     30       // fetch HTML for given channel ID
     31       $html = explode(
     32           PHP_EOL,
     33           file_get_contents("https://www.youtube.com/" . $channel_name)
     34       );
     35       // If HTML could be fetched, extract lines with probable profile picture
     36       if (!empty($html)) {
     37           $channel_id_lines = preg_grep(
     38               '/videos.xml\?channel_id\=/',
     39               $html
     40           );
     41           $channelid = preg_replace(
     42               '/^.*\"https:\/\/www\.youtube\.com\/feeds\/videos\.xml\?channel_id\=([^\"]+).*$/',
     43               '${1}',
     44               $channel_id_lines
     45           );
     46       }
     47       // return the channel id
     48       return array_values($channelid)[0];
     49   }
     50 
     51   /* Initilise videos template */
     52   $video_template = array(
     53     "vid" => array(),
     54     "title" => array(),
     55     "desc" => array(),
     56     "time" => array(),
     57     "views" => array(),
     58     "aid" => array(),
     59     "author" => array()
     60   );
     61 
     62   /* Initilise channels arrays */
     63   $channels = array();
     64   $channels_list = array();
     65 
     66   /* Initilise videos array */
     67   $videos = array();
     68 
     69   // Fetch POST, GET arguments or the COOKIE
     70   // Cookie should have lowest priority (fallback), POST first (intend)
     71   // GET arguments are used for one-time usage (will not set cookie)
     72   if (!is_null($_COOKIE["channels"])) {
     73     $channels = explode(",", $_COOKIE["channels"]);
     74   }
     75   if (!is_null($_POST["channels"])) {
     76     $channels = urldecode($_POST["channels"]);
     77     $channels = preg_replace('/[^A-Za-z0-9\-\_\,\@]+/', '', $channels);
     78     $channels = explode(",", $channels);
     79   }
     80   if (!is_null($_GET["channels"])) {
     81     $channels = explode(",", $_GET["channels"]);
     82   }
     83 
     84   // replace usernames with channel-ids
     85   $channel_names = preg_grep('/^@/', $channels);
     86   foreach ($channel_names as $channel_name) {
     87     array_push($channels, get_yt_channelid($channel_name));
     88   }
     89   $channels = array_values(preg_grep('/^[^@]/', $channels));
     90 
     91   // Sort channels by alphabet and ensure each channel is unique
     92   $channels = array_unique($channels);
     93 
     94   /* refresh cookie */
     95   if (is_null($_GET["channels"])) {
     96     header(
     97       "Set-Cookie: " .
     98       "channels=" . implode(",", $channels) . ";" .
     99       "Max-Age=" . 31536000 . "; " . /* 60 x 60 x 24 x 365 = 1 year */
    100       "Domain=" . $_SERVER["SERVER_NAME"] . "; " .
    101       "SameSite=Strict;"
    102     );
    103   }
    104 
    105 
    106   /* Comparing function for sorting */
    107   function compare_numeric(array $a, array $b, string $col) {
    108     return $b[$col] - $a[$col];
    109   }
    110   function compare_string(array $a, array $b, string $col) {
    111     return strcmp(strtolower($a[$col]), strtolower($b[$col]));
    112   }
    113   function cmp_time(array $a, array $b) {
    114     return compare_numeric($a, $b, "time");
    115   }
    116   function cmp_name(array $a, array $b) {
    117     return compare_string($a, $b, "author");
    118   }
    119 
    120   /* Sort videos and channels arrays */
    121   usort($videos, "cmp_time");
    122   usort($channels_list, "cmp_name");
    123 
    124 ?>
    125 
    126   <body>
    127 
    128     <header>
    129       <nav>
    130         <a href="/">All Videos</a>
    131         <?php
    132           /* For GET-requests, draw simple reload button,
    133            * otherwise re-submit channels form
    134            */
    135           if (is_null($_GET["channels"])) {
    136             $jsfun = "document.getElementById('channel_form').submit();";
    137           } else {
    138             $jsfun = "window.location.reload();";
    139           }
    140         ?>
    141         <a href="#" onclick="<?php echo $jsfun; ?>">
    142           Refresh
    143         </a>
    144         <a href="#about">
    145           About
    146         </a>
    147       </nav>
    148       <h1>Videos</h1>
    149       <script>let loaded_channels = [];</script>
    150       <stretch
    151         id="loading"
    152         data="<?php echo implode(",", $channels); ?>"
    153       >
    154       <i id="loading_dot"></i>
    155       <span id="loading_text"></span>
    156       </stretch>
    157     </header>
    158 
    159     <form action="https://www.youtube.com/results" method="GET">
    160       <input
    161         id="searchbar"
    162         type="text"
    163         id="searchInput"
    164         name="search_query"
    165         placeholder="Search on YouTube..."
    166       >
    167     </form>
    168 
    169     <!-- Channels List Form -->
    170     <details id="channels">
    171       <summary>List of Channels</summary>
    172       <form action="/" method="POST" id="channel_form">
    173         <details>
    174           <summary>Import / Export</summary>
    175           <p>
    176             Please enter the YouTube channel-IDs you want to check here, each
    177             separated with a <code>,</code>. You may also apply Usernames,
    178             starting with a <code>@</code>:
    179           </p>
    180           <input
    181             name="channels"
    182             type="text"
    183             value="<?php echo implode("," . PHP_EOL, $channels); ?>"
    184           >
    185         <input type="submit" value="Save &amp; Reload">
    186         </details>
    187         <div id="channels_list">
    188           <?php
    189             /* Draw Input fields for each channel ID */
    190             $i = 0;
    191             foreach ($channels as $channel) {
    192           ?>
    193           <label for="channel_<?php echo $i; ?>">
    194           <a
    195             id="channellist_<?php echo $channel; ?>"
    196             href="/?channels=<?php echo $channel; ?>"
    197             target="_blank"
    198           >
    199           </a>
    200           </label>
    201           <input
    202             name="channel_<?php echo $i; ?>"
    203             class="channels_input"
    204             type="text"
    205             value="<?php echo $channel; ?>"
    206             oninput="yt2html_update_channels_list();"
    207           >
    208           <?php
    209               $i++;
    210             } // foreach channel
    211           ?>
    212           <!-- Draw one empty input field for new entry by the user -->
    213           <label for="channel_<?php echo ($i); ?>">
    214             <mark>Add a new channel here</mark>
    215           </label>
    216           <input
    217             name="channel<?php echo ($i); ?>"
    218             class="channels_input"
    219             type="text"
    220             placeholder="@username"
    221             oninput="yt2html_update_channels_list();"
    222           >
    223         </div>
    224         <!-- Submit Button -->
    225         <input type="submit" value="Save &amp; Reload">
    226       </form>
    227     </details>
    228 
    229     <!-- Fetch all videos via javascript and insert them into the feed -->
    230     <script>
    231     <?php
    232       $i = 0;
    233       foreach ($channels as $channel) {
    234         echo "setTimeout(function() { fetch_and_insert_yt('" .
    235           $channel . "') }, " . $i * 50 . ");";
    236         $i++;
    237       }
    238     ?>
    239     </script>
    240 
    241     <!-- Video Section template -->
    242     <section class="video" id="video_template" data="0">
    243 
    244       <!-- Headline: video name -->
    245       <h3 style="margin-bottom:5px;">
    246         <a class="title" target="_blank" href="#"></a>
    247       </h3>
    248 
    249       <!-- Video Information -->
    250       <div style="width:100%;margin-bottom:1em;color:var(--border);">
    251         <!-- Channel Name -->
    252         <span style="margin-right:0.25em;">
    253           <a class="channel" href="#"></a>
    254         </span>
    255         <!-- Timestamp -->
    256         <span class="timestamp" style="margin-left:0.25em;margin-right:0.25em;">
    257         </span>
    258         <!-- Views -->
    259         <span class="views" style="margin-left:0.25em;margin-right:0.25em;">
    260         </span>
    261       </div>
    262 
    263       <!-- player Container -->
    264       <div class="player_container hidden">
    265         <button class="toggle_video">Toggle Player</button>
    266       </div>
    267 
    268       <!-- Preview Image -->
    269       <div class="preview">
    270         <img class="preview_image" src="#" loading="lazy" height="100%" width="100%">
    271         <div class="play_button">
    272         </div>
    273       </div>
    274 
    275       <!-- Description Collapsable -->
    276       <details>
    277           <summary>
    278             Description
    279           </summary>
    280           <p class="description"></p>
    281     </details>
    282 
    283   </section>
    284 
    285   <footer>
    286     <h3 id="about">About</h3>
    287     <details>
    288       <summary>What is this?</summary>
    289       <p>
    290         This web-application is a way to <em>follow</em> YouTube-Channels,
    291         without using either the YouTube.com website directly or requiring a
    292         Youtube/Google account.
    293       </p>
    294       <p>
    295         Instead, this utility uses YouTube's
    296         <a href="https://en.wikipedia.org/wiki/RSS" target="_blank">
    297           RSS-Feeds
    298         </a> in order to fetch the latest videos uploaded to specified Channels.
    299         Simply insert the Channel-ID or <em>Username</em> (starting with an
    300         <code>@</code>) in the form at the top of the page and click "save".
    301       </p>
    302       <p>
    303         For each channel, the newest 15 videos will be gathered, sorted
    304         chronoligically and served you as a simple list. You can click the
    305         thumbnail of a video-preview to start playback. This will embed the
    306         YouTube-Video directly, i.e. your browser will connect to youtube.com
    307       </p>
    308     </details>
    309     <details>
    310       <summary>How can I contribute?</summary>
    311       <p>
    312         All development happens at
    313         <a href="https://src.jayvii.de/pub/yt2html/" target="_blank">
    314           src.jayvii.de/pub/yt2html
    315         </a>. If you want to contribute or would like to self-host yt2html,
    316         please head over there.
    317       </p>
    318     </details>
    319     <details>
    320       <summary>Privacy Statement</summary>
    321       <p>
    322         Besides Access Information, no information of site visitors are
    323         collected.
    324       </p>
    325       <p>
    326         When submitting a list of youtube-channels in the form, a cookie storing
    327         that list is set, so the service knows which channels to poll.
    328       </p>
    329       <p>
    330         Starting to watch a youtube video will connect your browser directly to
    331         <code>www.youtube-nocookie.com</code>, thereby transfering your IP
    332         address as well as potentially other data, leaked by your webbrowser.
    333         You can find <a href="https://www.youtube.com/privacy" target="_blank">
    334           YouTube’s privacy policy here
    335         </a>.
    336       </p>
    337       <p>
    338         No other data is shared with anyone.
    339       </p>
    340   </footer>
    341 
    342   </body>
    343 </html>