pub / tw2html

Checks online status of streams on twitch.tv and lets you watch them
git clone https://src.jayvii.de/pub/tw2html.git
Home | Log | Files | Exports | Refs | README | RSS

load_streams.php (2067B)


      1 <!-- SPDX-License-Identifier: AGPL-3.0-or-later
      2      SPDX-FileCopyrightText: 2024 JayVii <jayvii[AT]posteo[DOT]de>
      3 -->
      4 
      5 <?php
      6 
      7 // Check Online Status of streams
      8 for ($i = 0; $i < sizeof($streams["stream"]); $i++) {
      9 
     10     // Fetch Stream Preview Image
     11     $img = file_get_contents(
     12         "https://static-cdn.jtvnw.net/previews-ttv/live_user_" .
     13         $streams["stream"][$i] .
     14         "-853x480.jpg"
     15     );
     16 
     17     // Check Online-Status via HTTP status code
     18     $http_code = preg_replace(
     19         '/^.*(\d{3}).*/',
     20         '\1',
     21         $http_response_header[0]
     22     );
     23 
     24     if (!($http_code == "200")) {
     25 
     26         // Stream is not online!
     27         array_push($streams["status"], 0);
     28         $desc = "";
     29         $game = "";
     30 
     31     } else {
     32 
     33         // Stream is online!
     34         array_push($streams["status"], 1);
     35 
     36         // Fetch HTML
     37         $html = file_get_contents(
     38             "https://m.twitch.tv/" . $streams["stream"][$i]
     39         );
     40         $html = str_replace(array("\r", "\n"), "", $html);
     41 
     42         // Parse Game Name
     43         $pattern = '/^.*<p.*?Playing <a.*?>(.*?)<\/a>.*$/';
     44         $replacement = '\1';
     45         $game = substr(
     46             strip_tags(
     47                 preg_replace(
     48                     $pattern,
     49                     $replacement,
     50                     $html
     51                 )
     52             ),
     53             0,
     54             50
     55         );
     56 
     57         // Parse Stream Description
     58         $html = preg_replace('/<script.*<\/script>/', '', $html);
     59         $html = explode(">", $html);
     60         $desc_raw = implode("", preg_grep('/name="description"/', $html));
     61         $pattern = '/^.*content="(.*?)".*$/';
     62         $replacement = '\1';
     63         $desc = substr(
     64             strip_tags(
     65                 preg_replace(
     66                     $pattern,
     67                     $replacement,
     68                     $desc_raw
     69                 )
     70             ),
     71             0,
     72             500
     73         );
     74 
     75     }
     76 
     77     // Fill Streams array
     78     array_push($streams["desc"], $desc);
     79     array_push($streams["game"], $game);
     80     array_push($streams["time"], (time() - $time0));
     81 
     82 }
     83 
     84 ?>