From 916d39079f94cab062305c33f7d090eab1233327 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Sat, 23 Apr 2022 17:54:46 +0200 Subject: [PATCH] Can check if vtuber is live without YT api --- www/models/Vtubers.php | 22 ++++++++++++++++------ www/views/live.php | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/www/models/Vtubers.php b/www/models/Vtubers.php index 99761e2..3ac8372 100644 --- a/www/models/Vtubers.php +++ b/www/models/Vtubers.php @@ -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 []; + } } } diff --git a/www/views/live.php b/www/views/live.php index 1401407..7450813 100644 --- a/www/views/live.php +++ b/www/views/live.php @@ -41,7 +41,7 @@ if (!isset($_GET["id"])) { if (str_contains($vtuber["vtuber"][0]["link"], "twitch.tv")) { echo ""; } else { - echo ""; + echo ""; } echo ""; -- 2.52.0