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 (11823B)


      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 async 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   /* Comparing function for sorting */
     27   function compare_numeric(array $a, array $b, string $col) {
     28     return $b[$col] - $a[$col];
     29   }
     30   function compare_string(array $a, array $b, string $col) {
     31     return strcmp(strtolower($a[$col]), strtolower($b[$col]));
     32   }
     33 
     34   /* Get Channel ID */
     35   function get_yt_channelid($channel_name) {
     36       // set fallback for channel id
     37       $channelid = $channel_name;
     38       // fetch HTML for given channel ID
     39       $html = explode(
     40           PHP_EOL,
     41           file_get_contents("https://www.youtube.com/" . $channel_name)
     42       );
     43       // If HTML could be fetched, extract lines with probable profile picture
     44       if (!empty($html)) {
     45           $channel_id_lines = preg_grep(
     46               '/videos.xml\?channel_id\=/',
     47               $html
     48           );
     49           $channelid = preg_replace(
     50               '/^.*\"https:\/\/www\.youtube\.com\/feeds\/videos\.xml\?channel_id\=([^\"]+).*$/',
     51               '${1}',
     52               $channel_id_lines
     53           );
     54       }
     55       // return the channel id
     56       return array_values($channelid)[0];
     57   }
     58 
     59   /* Initilise videos template */
     60   $video_template = array(
     61     "vid" => array(),
     62     "title" => array(),
     63     "desc" => array(),
     64     "time" => array(),
     65     "views" => array(),
     66     "aid" => array(),
     67     "author" => array()
     68   );
     69 
     70   /* Initilise channels arrays */
     71   $channels = array();
     72   $channels_list = array();
     73 
     74   /* Initilise videos array */
     75   $videos = array();
     76 
     77   // Fetch POST, GET arguments or the COOKIE
     78   // Cookie should have lowest priority (fallback), POST first (intend)
     79   // GET arguments are used for one-time usage (will not set cookie)
     80   if (!is_null($_COOKIE["channels"])) {
     81     $channels = explode(",", $_COOKIE["channels"]);
     82   }
     83   if (!is_null($_POST["channels"])) {
     84     $channels = urldecode($_POST["channels"]);
     85     $channels = preg_replace('/[^A-Za-z0-9\-\_\,\@]+/', '', $channels);
     86     $channels = explode(",", $channels);
     87   }
     88   if (!is_null($_GET["channels"])) {
     89     $channels = explode(",", $_GET["channels"]);
     90   }
     91 
     92   // replace usernames with channel-ids
     93   $channel_names = preg_grep('/^@/', $channels);
     94   foreach ($channel_names as $channel_name) {
     95     array_push($channels, get_yt_channelid($channel_name));
     96   }
     97   $channels = array_values(preg_grep('/^[^@]/', $channels));
     98 
     99   // Sort channels by alphabet and ensure each channel is unique
    100   $channels = array_unique($channels);
    101 
    102   /* refresh cookie */
    103   if (is_null($_GET["channels"])) {
    104     header(
    105       "Set-Cookie: " .
    106       "channels=" . implode(",", $channels) . ";" .
    107       "Max-Age=" . 31536000 . "; " . /* 60 x 60 x 24 x 365 = 1 year */
    108       "Domain=" . $_SERVER["SERVER_NAME"] . "; " .
    109       "SameSite=Strict;"
    110     );
    111   }
    112 
    113   for ($i = 0; $i < count($channels); $i++) {
    114     $channel = $channels[$i];
    115 
    116     // Fetch Youtube XML Feed
    117     $channel_xml = file(
    118     "https://www.youtube.com/feeds/videos.xml?channel_id=" . $channel
    119     );
    120 
    121     /* Skip to next entry, if channel could not be found */
    122     if ($channel_xml === false) {
    123       /* Remove entry from channels list */
    124       array_splice($channels, $i, 1);
    125       /* Skip item within loop */
    126       continue;
    127     }
    128 
    129     // Replace un-parsable items
    130     $channel_xml = str_replace(
    131       array("yt:", "media:"),
    132       array("yt_", "media_"),
    133       $channel_xml
    134     );
    135 
    136     // Cast Array to string
    137     $channel_xml = implode(PHP_EOL, $channel_xml);
    138 
    139     // Parse XML
    140     $channel_xml = simplexml_load_string($channel_xml);
    141     $channel_xml = json_encode($channel_xml);
    142     $channel_xml = json_decode($channel_xml, true);
    143 
    144     // Get Channel name
    145     $author = str_replace(
    146       array("&"),
    147       "&amp;",
    148       $channel_xml["entry"][0]["author"]["name"]
    149     );
    150 
    151     /* Fill channels list array */
    152     array_push($channels_list, array("aid" => $channel, "author" => $author));
    153 
    154     // Process Entries
    155     foreach ($channel_xml["entry"] as $entry) {
    156 
    157       /* Copy video template array */
    158       $video = $video_template;
    159 
    160       // Get Video ID
    161       $video["vid"] = str_replace(array("yt_video:"), "", $entry["id"]);
    162 
    163       // Get Video Title
    164       $video["title"] = str_replace(array("&"), "&amp;", $entry["title"]);
    165 
    166       // Get Video Description
    167       $video["desc"] = preg_replace(
    168         "/\n+/",
    169         "<br>",
    170         $entry["media_group"]["media_description"]
    171       );
    172       if (empty($video["desc"])) {
    173         $video["desc"] = "";
    174       }
    175 
    176       // Get Time
    177       $video["time"] = strtotime($entry["published"]);
    178 
    179       // Get Views
    180       $video["views"] = number_format(
    181         $entry["media_group"]["media_community"]["media_statistics"]["@attributes"]["views"]
    182       );
    183 
    184       // Get Channel ID
    185       $video["aid"] = $channel;
    186 
    187       // Get Channel Name
    188       $video["author"] = $author;
    189 
    190       /* check whether indications of a YouTube-Short exist, skip if it is
    191        * We assume video is a short if any of the following applies:
    192        * has #short/#shorts in the title
    193        * has #short #shorts in the description
    194        * description only contains #hashtags
    195        * has no description at all
    196        * Also filter out videos that come out in the future
    197        * Main indicator is exactly zero views
    198        */
    199       $vdesc = explode(" ", $video["desc"]);
    200       if (
    201         preg_match('/#short(s)*/', $video["title"]) > 0 ||
    202         preg_match('/#short(s)*/', $video["desc"]) > 0 ||
    203         preg_grep('/^(#|$)/', $vdesc) == $vdesc ||
    204         strlen($video["desc"]) == 0 ||
    205         $video["views"] == "0"
    206       ) {
    207         continue;
    208       }
    209 
    210       /* Add video to videos array */
    211       array_push($videos, $video);
    212 
    213     }
    214 
    215   }
    216 
    217   /* Sort videos and channels arrays */
    218   function cmp_time(array $a, array $b) {
    219     return compare_numeric($a, $b, "time");
    220   }
    221   function cmp_name(array $a, array $b) {
    222     return compare_string($a, $b, "author");
    223   }
    224   usort($videos, "cmp_time");
    225   usort($channels_list, "cmp_name");
    226 
    227 ?>
    228 
    229   <body>
    230 
    231     <header>
    232       <nav>
    233         <a href="/">All Videos</a>
    234         <?php
    235           /* For GET-requests, draw simple reload button,
    236            * otherwise re-submit channels form
    237            */
    238           if (is_null($_GET["channels"])) {
    239             $jsfun = "document.getElementById('channel_form').submit();";
    240           } else {
    241             $jsfun = "window.location.reload();";
    242           }
    243         ?>
    244         <a href="#" onclick="<?php echo $jsfun; ?>">
    245           Refresh
    246         </a>
    247         <a href="https://src.jayvii.de/pub/yt2html" target="_blank">
    248           Development
    249         </a>
    250       </nav>
    251       <h1>Videos</h1>
    252     </header>
    253 
    254     <form action="https://www.youtube.com/results" method="GET">
    255       <input
    256         id="searchbar"
    257         type="text"
    258         id="searchInput"
    259         name="search_query"
    260         placeholder="Search on YouTube..."
    261       >
    262     </form>
    263 
    264     <!-- Channels List Form -->
    265     <details id="channels">
    266       <summary>List of Channels</summary>
    267       <form action="/" method="POST" id="channel_form">
    268         <details>
    269           <summary>Import / Export</summary>
    270           <p>
    271             Please enter the YouTube channel-IDs you want to check here, each
    272             separated with a <code>,</code>. You may also apply Usernames,
    273             starting with a <code>@</code>:
    274           </p>
    275           <input
    276             name="channels"
    277             type="text"
    278             value="<?php echo implode("," . PHP_EOL, $channels); ?>"
    279           >
    280         <input type="submit" value="Save &amp; Reload">
    281         </details>
    282         <div id="channels_list">
    283           <?php
    284             /* Draw Input fields for each channel ID */
    285             for ($i = 0; $i < count($channels_list); $i++) {
    286           ?>
    287           <label for="channel_<?php echo $i; ?>">
    288             <?php
    289               if ($channels_list[$i]["author"] != "") {
    290                 echo "<a href=\"/?channels=" . $channels_list[$i]["aid"] .
    291                   "\" target=\"_blank\">" . $channels_list[$i]["author"] .
    292                   "</a>";
    293               } else {
    294                 echo "<mark class=\"error\">Error</mark>";
    295               }
    296             ?>
    297           </label>
    298           <input
    299             name="channel_<?php echo $i; ?>"
    300             class="channels_input"
    301             type="text"
    302             value="<?php echo $channels_list[$i]["aid"]; ?>"
    303             oninput="yt2html_update_channels_list();"
    304           >
    305           <?php
    306             }
    307           ?>
    308           <!-- Draw one empty input field for new entry by the user -->
    309           <label for="channel_<?php echo ($i); ?>">
    310             <mark>Add a new channel here</mark>
    311           </label>
    312           <input
    313             name="channel<?php echo ($i); ?>"
    314             class="channels_input"
    315             type="text"
    316             placeholder="@username"
    317             oninput="yt2html_update_channels_list();"
    318           >
    319         </div>
    320         <!-- Submit Button -->
    321         <input type="submit" value="Save &amp; Reload">
    322       </form>
    323     </details>
    324 
    325     <!-- Video List -->
    326     <?php
    327       $index = 0;
    328       foreach ($videos as $video) {
    329 
    330         /* Stop if 500 videos are listed */
    331         if ($index >= 500) {
    332           break;
    333         }
    334     ?>
    335     <section id="video_<?php echo $index; ?>">
    336 
    337       <!-- Headline: streamer name -->
    338       <h3 style="margin-bottom:5px;"><?php echo $video["title"]; ?></h3>
    339 
    340       <!-- Video Information -->
    341       <div style="width:100%;margin-bottom:1em;color:var(--border);">
    342         <span style="margin-right:0.25em;"><?php echo $video["author"]; ?></span>
    343         <span style="margin-left:0.25em;margin-right:0.25em;"><?php echo date("Y-m-d, H:i:s", $video["time"]); ?></span>
    344         <span style="margin-left:0.25em;"><?php echo "(" . $video["views"] . " Views)"; ?></span>
    345       </div>
    346 
    347       <!-- player Container -->
    348       <div class="player_container hidden"></div>
    349 
    350       <!-- Preview Image -->
    351       <img
    352         id="preview_<?php echo $index; ?>"
    353         class="preview"
    354         src="https://i4.ytimg.com/vi/<?php echo $video["vid"]; ?>/hqdefault.jpg"
    355         loading="lazy"
    356       >
    357 
    358       <!-- Video Buttons -->
    359       <?php $js_args = $index . ",'" . $video["vid"] . "'"; ?> 
    360       <button onclick="yt2html_toggle_player(<?php echo $js_args; ?>)">
    361         Toggle Player
    362       </button>
    363       <a
    364         class="button"
    365         href="/?channels=<?php echo $video["aid"]; ?>"
    366       >
    367         Open Channel
    368       </a>
    369       <a
    370         class="button"
    371         href="https://www.youtube.com/watch?v=<?php echo $video["vid"]; ?>"
    372         target="_blank"
    373       >
    374         Open on YouTube
    375       </a>
    376 
    377     <!-- Chat Collapsable -->
    378     <details>
    379         <summary>
    380             Description
    381         </summary>
    382         <?php echo $video["desc"]; ?>
    383     </details>
    384 
    385   </section>
    386 
    387   <?php
    388 
    389     $index++;
    390 
    391     }
    392 
    393   ?>
    394 
    395   </body>
    396 </html>