]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Can check if vtuber is live without YT api
authorLeonardoBizzoni <leo2002714@gmail.com>
Sat, 23 Apr 2022 15:54:46 +0000 (17:54 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sat, 23 Apr 2022 15:54:46 +0000 (17:54 +0200)
www/models/Vtubers.php
www/views/live.php

index 99761e27981cde2a6a68ec3fda6cda2269a87d76..3ac8372c823cdaa31c3ef85858a79f1f6dc9ed40 100644 (file)
@@ -4,6 +4,7 @@ namespace app\models;
 
 use app\core\Application;
 use app\core\DbModel;
+use DOMDocument;
 use Google_Client;
 use Google_Service_YouTube;
 
@@ -101,19 +102,28 @@ class Vtubers extends DbModel
         }
 
         if (str_contains($link, "youtube.com")) {
-            $clientID = Application::$app->config["yt"]["key"] ?? "";
-
-            $url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=$login&eventType=live&type=video&key=$clientID";
+            $url = "https://www.youtube.com/channel/$login/live";
 
             $ch = curl_init($url);
             curl_setopt($ch, CURLOPT_URL, $url);
             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 
-            $result = get_object_vars(json_decode(curl_exec($ch)));
+            $result = curl_exec($ch);
             curl_close($ch);
 
-            return count($result["items"]) ? $result["items"] : [];
-        }
+            $doc = new DOMDocument();
+            libxml_use_internal_errors(true);
+            $doc->loadHTML($result);
+            $length = $doc->getElementsByTagName('link')->length;
 
+            for ($i = 0; $i < $length; $i++) {
+                $result = $doc->getElementsByTagName("link")->item($i)->getAttribute("href");
+                if (str_contains($result, "https://www.youtube.com/watch?v=")) {
+                    return [str_replace( "https://www.youtube.com/watch?v=", "",  $result)];
+                }
+            }
+
+            return [];
+        }
     }
 }
index 14014073670fe41793d9480e1d8427c9724d1d40..74508138d50a531020f53793c0459da7625642d6 100644 (file)
@@ -41,7 +41,7 @@ if (!isset($_GET["id"])) {
             if (str_contains($vtuber["vtuber"][0]["link"], "twitch.tv")) {
                 echo "<iframe src=\"https://player.twitch.tv/?channel=".$vtuber["vtuber"][0]["login"]."&parent=localhost\" frameborder=\"0\" allowfullscreen=\"true\" scrolling=\"no\"></iframe>";
             } else {
-                echo "<iframe src=\"https://www.youtube.com/embed/".$vtuber["vtuber"][1][0]->id->videoId."\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
+                echo "<iframe src=\"https://www.youtube.com/embed/".$vtuber["vtuber"][1][0]."\" title=\"YouTube video player\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>";
             }
 
             echo "</div></div>";