]> git.leonardobizzoni.com Git - highschool-graduation-project/commitdiff
creato script separato per vtuber live
authorLeonardoBizzoni <leo2002714@gmail.com>
Mon, 9 May 2022 13:58:44 +0000 (15:58 +0200)
committerLeonardoBizzoni <leo2002714@gmail.com>
Mon, 9 May 2022 13:58:44 +0000 (15:58 +0200)
www/Migrations/m_1652087333_liveColInVtubers.php [new file with mode: 0644]
www/migrationScript.php
www/models/Vtubers.php
www/vtuberIsLive.php [new file with mode: 0644]

diff --git a/www/Migrations/m_1652087333_liveColInVtubers.php b/www/Migrations/m_1652087333_liveColInVtubers.php
new file mode 100644 (file)
index 0000000..4145e8c
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+
+use app\core\Application;
+
+class m_1652087333_liveColInVtubers
+{
+    public function up()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE vtubers ADD COLUMN live VARCHAR(255) default NULL;";
+        $db->pdo->exec($sql);
+    }
+
+    public function down()
+    {
+        $db = Application::$app->db;
+        $sql = "ALTER TABLE vtubers DROP COLUMN live;";
+        $db->pdo->exec($sql);
+    }
+}
+?>
index c9a0432b5166122ac6607f9fff25ce78e156310e..01e06037502de2a6796df1495bed1edb1d4dbd39 100644 (file)
@@ -1,6 +1,4 @@
 <?php
-use app\controllers\AuthController;
-use app\controllers\SiteController;
 use app\core\Application;
 
 require_once __DIR__."/vendor/autoload.php";
index 95f2d86447a1ada394b29a4a498d69ade388d5cd..203a461de86f5af600a0ac3ebdb68bd74e41c1f8 100644 (file)
@@ -93,49 +93,6 @@ class Vtubers extends DbModel
 
     public function isLive(string $login, string $link)
     {
-        // if (str_contains($link, "twitch.tv")) {
-        //     $clientID = Application::$app->config["twitch"]["clientid"] ?? "";
-        //     $token = Application::$app->config["twitch"]["token"] ?? "";
-
-        //     $url = "https://api.twitch.tv/helix/streams?user_login=$login";
-
-        //     $ch = curl_init($url);
-        //     curl_setopt($ch, CURLOPT_URL, $url);
-        //     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
-        //     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Client-ID: $clientID", "Authorization: Bearer $token"));
-
-        //     $result = get_object_vars(json_decode(curl_exec($ch)));
-        //     curl_close($ch);
-
-        //     return count($result["data"]) ? $result["data"] : [];
-        // }
-
-        // if (str_contains($link, "youtube.com")) {
-        //     $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 = 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/watch?v=")) {
-        //             return [str_replace("https://www.youtube.com/watch?v=", "",  $tag)];
-        //         }
-        //     }
-
-        //     unset($doc);
-        // }
 
         return [];
     }
diff --git a/www/vtuberIsLive.php b/www/vtuberIsLive.php
new file mode 100644 (file)
index 0000000..4afb72b
--- /dev/null
@@ -0,0 +1,83 @@
+<?php
+// se il canale รจ live aggiunge il link della live al database di vtuber
+// aggiungi colonna alla table vtubers con null o il link alla live
+
+use app\core\Application;
+
+require_once __DIR__ . "/vendor/autoload.php";
+$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
+$dotenv->load();
+
+$config = [
+    "db" => [
+        "dsn" => $_ENV["DB_DSN"],
+        "user" => $_ENV["DB_USER"],
+        "password" => $_ENV["DB_PASSWORD"]
+    ],
+    "twitch" => [
+        "clientid" => $_ENV["TWITCH_CLIENTID"],
+        "token" => $_ENV["TWITCH_TOKEN"]
+    ],
+    "yt" => [
+        "key" => $_ENV["GOOGLE_API_KEY"]
+    ],
+    "userClass" => null
+];
+
+$app = new Application(__DIR__, $config);
+
+$stmt = $app->db->pdo->prepare("select link, login from vtubers");
+$stmt->execute();
+$data = $stmt->fetchAll();
+
+foreach ($data as $vtuber) {
+    $link = $vtuber["link"];
+    $login = $vtuber["login"];
+
+    if (str_contains($link, "twitch.tv")) {
+        $clientID = $app->config["twitch"]["clientid"] ?? "";
+        $token = $app->config["twitch"]["token"] ?? "";
+
+        $url = "https://api.twitch.tv/helix/streams?user_login=$login";
+
+        $ch = curl_init($url);
+        curl_setopt($ch, CURLOPT_URL, $url);
+        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+        curl_setopt($ch, CURLOPT_HTTPHEADER, array("Client-ID: $clientID", "Authorization: Bearer $token"));
+
+        $result = get_object_vars(json_decode(curl_exec($ch)));
+        curl_close($ch);
+
+        // echo count($result["data"]) ? "$login live\n" : "$login non live\n";
+        if (count($result["data"])) {
+            echo "$login live\n";
+        }
+    }
+
+    if (str_contains($link, "youtube.com")) {
+        $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 = 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/watch?v=")) {
+                echo "$login live\n";
+            }
+        }
+
+        unset($doc);
+    }
+}