From: LeonardoBizzoni Date: Sun, 3 Jul 2022 06:52:02 +0000 (+0200) Subject: Fixed invalid channel link X-Git-Url: http://git.leonardobizzoni.com/?a=commitdiff_plain;h=ea3cb95140699f0734eb5d6022de495754a21817;p=highschool-graduation-project Fixed invalid channel link --- diff --git a/www/models/Vtubers.php b/www/models/Vtubers.php index 8beb6d3..471a760 100644 --- a/www/models/Vtubers.php +++ b/www/models/Vtubers.php @@ -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; diff --git a/www/views/live.php b/www/views/live.php index 561f309..d90475b 100644 --- a/www/views/live.php +++ b/www/views/live.php @@ -8,10 +8,15 @@ if (isset($_GET["aggiungi"])) { } if (count($model->errors) > 0) { - echo "
";
-    var_dump($model->errors);
-    echo "
"; -} else if (!isset($_GET["id"])) { + echo " +
+

Invalid channel link!

+

The link you provided it's either not correct or already in the list.
Try again.

+
+"; +} + +else if (!isset($_GET["id"])) { echo "

Live page

"; ?>