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

fetch_feed.php (2115B)


      1 <?php
      2 
      3 /* Fetch XML Feed Channel */
      4 $channel = $_GET["channel"];
      5 $channel_xml = file(
      6     "https://www.youtube.com/feeds/videos.xml?channel_id=" . $channel
      7 );
      8 
      9 /* Skip to next entry, if channel could not be found */
     10 if ($channel_xml === false) {
     11   /* Skip item within loop */
     12   die();
     13 }
     14 
     15 // Replace un-parsable items
     16 $channel_xml = str_replace(
     17   array("yt:", "media:"),
     18   array("yt_", "media_"),
     19   $channel_xml
     20 );
     21 
     22 // Cast Array to string
     23 $channel_xml = implode(PHP_EOL, $channel_xml);
     24 
     25 // Parse XML
     26 $channel_xml = simplexml_load_string($channel_xml);
     27 $channel_xml = json_encode($channel_xml);
     28 $channel_xml = json_decode($channel_xml, true);
     29 
     30 // Get Channel name
     31 $author = str_replace(
     32   array("&"),
     33   "&amp;",
     34   $channel_xml["entry"][0]["author"]["name"]
     35 );
     36 
     37 /* Initilise Videos array */
     38 $videos = array();
     39 
     40 // Process Entries
     41 foreach ($channel_xml["entry"] as $entry) {
     42 
     43   // Robustness: Skip entry if it is not an array (broken return)
     44   if (!is_array($entry)) {
     45     continue;
     46   }
     47 
     48   // Skip entry if it is a short
     49   $uri = $entry["link"]["@attributes"]["href"];
     50   if (preg_match('/\/shorts\//', $uri) > 0) {
     51     continue;
     52   }
     53 
     54   /* Initilise videos template */
     55   $video = array(
     56     "vid" => array(),
     57     "title" => array(),
     58     "desc" => array(),
     59     "time" => array(),
     60     "views" => array(),
     61     "aid" => array(),
     62     "author" => array()
     63   );
     64 
     65   // Get Video ID
     66   $video["vid"] = str_replace(array("yt_video:"), "", $entry["id"]);
     67 
     68   // Get Video Title
     69   $video["title"] = str_replace(array("&"), "&amp;", $entry["title"]);
     70 
     71   // Get Video Description
     72   $video["desc"] = $entry["media_group"]["media_description"];
     73   if (empty($video["desc"])) {
     74     $video["desc"] = "";
     75   }
     76 
     77   // Get Time
     78   $video["time"] = strtotime($entry["published"]);
     79 
     80   // Get Views
     81   $video["views"] = number_format(
     82     $entry["media_group"]["media_community"]["media_statistics"]["@attributes"]["views"]
     83   );
     84 
     85   // Get Channel ID
     86   $video["aid"] = $channel;
     87 
     88   // Get Channel Name
     89   $video["author"] = $author;
     90 
     91   /* Add video to videos array */
     92   array_push($videos, $video);
     93 
     94 }
     95 
     96 // return content
     97 echo json_encode($videos);
     98 
     99 ?>