]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
Fixed invalid channel link
authorLeonardoBizzoni <leo2002714@gmail.com>
Sun, 3 Jul 2022 06:52:02 +0000 (08:52 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sun, 3 Jul 2022 06:52:02 +0000 (08:52 +0200)
www/models/Vtubers.php
www/views/live.php

index 8beb6d3ab4db0cb071148c48610abe833269f738..471a760c4a6cc30e943b0f0e59dae212c819c9fb 100644 (file)
@@ -2,6 +2,7 @@
 
 namespace app\models;
 
+use \DOMDocument;
 use app\core\Application;
 use app\core\DbModel;
 use Google_Client;
@@ -80,8 +81,33 @@ class Vtubers extends DbModel
             $client->setDeveloperKey(Application::$app->config["yt"]["key"]);
             $service = new Google_Service_YouTube($client);
 
-            $id = str_replace("https://www.youtube.com/channel/", "", $this->link);
-            $response = get_object_vars($service->channels->listChannels('snippet', ["id" => $id]));
+            if (str_contains($this->link, "https://www.youtube.com/channel/")) {
+                $id = str_replace("https://www.youtube.com/channel/", "", $this->link);
+            } else if (str_contains($this->link, "https://www.youtube.com/c/")) {
+                    $ch = curl_init($this->link);
+                    curl_setopt($ch, CURLOPT_URL, $this->link);
+                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+
+                    $result = curl_exec($ch);
+                    curl_close($ch);
+
+                    $doc = new DOMDocument();
+                    libxml_use_internal_errors(true);
+                    $doc->loadHTML($result);
+
+                    $result = $doc->getElementsByTagName("link");
+                    $length = $result->length;
+
+                    for ($i = 0; $i < $length; $i++) {
+                        $tag = $result->item($i)->getAttribute("href");
+
+                        if (str_contains($tag, "https://www.youtube.com/channel/")) {
+                            $id = str_replace("https://www.youtube.com/channel/", "", $tag);
+                            break;
+                        }
+                    }
+            }
+            $response = get_object_vars($service->channels->listChannels('snippet', [ 'id' => $id ]));
 
             $this->username = $response["items"][0]["snippet"]["title"];
             $this->login = $id;
index 561f309d1acf4343dd841a9e844897f91dc77b1d..d90475be0dc15df4fa7c2c42e59de3716f6133b3 100644 (file)
@@ -8,10 +8,15 @@ if (isset($_GET["aggiungi"])) {
 }
 
 if (count($model->errors) > 0) {
-    echo "<pre>";
-    var_dump($model->errors);
-    echo "</pre>";
-} else if (!isset($_GET["id"])) {
+    echo "
+<div class=\"alert alert-danger\" role=\"alert\">
+  <h4 class=\"alert-heading\">Invalid channel link!</h4>
+  <p>The link you provided it's either not correct or already in the list.<br>Try again.</p>
+</div>
+";
+}
+
+else if (!isset($_GET["id"])) {
     echo "<h1>Live page</h1>";
 ?>
     <!-- Modal -->