From e381c9484f33de99cad1e23e4a604978c1ca7719 Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Mon, 9 May 2022 15:58:44 +0200 Subject: [PATCH] creato script separato per vtuber live --- .../m_1652087333_liveColInVtubers.php | 21 +++++ www/migrationScript.php | 2 - www/models/Vtubers.php | 43 ---------- www/vtuberIsLive.php | 83 +++++++++++++++++++ 4 files changed, 104 insertions(+), 45 deletions(-) create mode 100644 www/Migrations/m_1652087333_liveColInVtubers.php create mode 100644 www/vtuberIsLive.php diff --git a/www/Migrations/m_1652087333_liveColInVtubers.php b/www/Migrations/m_1652087333_liveColInVtubers.php new file mode 100644 index 0000000..4145e8c --- /dev/null +++ b/www/Migrations/m_1652087333_liveColInVtubers.php @@ -0,0 +1,21 @@ +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); + } +} +?> diff --git a/www/migrationScript.php b/www/migrationScript.php index c9a0432..01e0603 100644 --- a/www/migrationScript.php +++ b/www/migrationScript.php @@ -1,6 +1,4 @@ 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 index 0000000..4afb72b --- /dev/null +++ b/www/vtuberIsLive.php @@ -0,0 +1,83 @@ +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); + } +} -- 2.52.0