]> git.localhorst.tv Git - alttp.git/commitdiff
track twitch category where chats were sent in
authorDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Apr 2024 16:34:39 +0000 (18:34 +0200)
committerDaniel Karbach <daniel.karbach@localhorst.tv>
Tue, 9 Apr 2024 16:34:39 +0000 (18:34 +0200)
app/Console/Commands/TwitchChannelInfo.php
app/Models/ChatLog.php
database/migrations/2024_04_09_160015_channel_twitch_info.php [new file with mode: 0644]
database/migrations/2024_04_09_161510_chat_log_category.php [new file with mode: 0644]

index 09dc7180e55e5a84e97cd303df96a65cfa49ffce..6a3261ca8a9eb1b72bb6e4a3dbd7d9768b88541e 100644 (file)
@@ -4,6 +4,7 @@ namespace App\Console\Commands;
 
 use App\Models\Channel;
 use App\Models\TwitchToken;
+use GuzzleHttp\Psr7\Query;
 use Illuminate\Console\Command;
 use Illuminate\Http\Client\RequestException;
 use Illuminate\Support\Facades\Http;
@@ -39,7 +40,7 @@ class TwitchChannelInfo extends Command {
                $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
                foreach ($channels as $channel) {
                        try {
-                               $this->updateChannel($channel);
+                               $this->updateChannelId($channel);
                        } catch (RequestException $e) {
                                if ($e->response->status() == 401) {
                                        $this->token->refresh();
@@ -47,16 +48,23 @@ class TwitchChannelInfo extends Command {
                                }
                        }
                }
+               Channel::where('twitch_id', '!=', '')
+                       ->where(function($query) {
+                               $query->where('join', true);
+                               $query->orWhere('chat', true);
+                       })->chunk(100, function ($channels) {
+                               $this->updateChannelInfos($channels);
+                       });
                return Command::SUCCESS;
        }
 
-       private function updateChannel(Channel $channel) {
+       private function updateChannelId(Channel $channel) {
                $this->line($channel->twitch_chat);
                $login = substr($channel->twitch_chat, 1);
                $rsp = $this->token->request()
-                       ->get('/users', [
+                       ->get('/users', Query::build([
                                'login' => $login,
-                       ])
+                       ]))
                        ->throw();
                foreach ($rsp['data'] as $user) {
                        if ($user['login'] != $login) continue;
@@ -65,6 +73,33 @@ class TwitchChannelInfo extends Command {
                }
        }
 
+       private function updateChannelInfos($channels) {
+               $ids = $channels->pluck('twitch_id')->toArray();
+               $rsp = $this->token->request()
+                       ->get('/streams', Query::build([
+                               'user_id' => $ids,
+                       ]))
+                       ->throw();
+               foreach ($channels as $channel) {
+                       $data = null;
+                       foreach ($rsp['data'] as $info) {
+                               if ($info['user_id'] == $channel->twitch_id) {
+                                       $data = $info;
+                                       break;
+                               }
+                       }
+                       if (is_null($data)) {
+                               $channel->twitch_live = false;
+                               $channel->twitch_viewers = 0;
+                       } else {
+                               $channel->twitch_live = true;
+                               $channel->twitch_category = $data['game_id'];
+                               $channel->twitch_viewers = $data['viewer_count'];
+                       }
+                       $channel->save();
+               }
+       }
+
        private $token;
 
 }
index c28a0724685320254e122e3e727a5755a56134b6..41bdcc506653a1727e7e13af8f4cafab2df617be 100644 (file)
@@ -106,6 +106,9 @@ class ChatLog extends Model {
                $channel = Channel::firstWhere('twitch_chat', '=', $cname);
                if (!is_null($channel)) {
                        $this->channel()->associate($channel);
+                       if (now()->sub(5, 'minute')->isBefore($this->created_at)) {
+                               $this->twitch_category = $channel->twitch_category;
+                       }
                }
        }
 
diff --git a/database/migrations/2024_04_09_160015_channel_twitch_info.php b/database/migrations/2024_04_09_160015_channel_twitch_info.php
new file mode 100644 (file)
index 0000000..6e55a50
--- /dev/null
@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::table('channels', function (Blueprint $table) {
+                       $table->boolean('twitch_live')->default(false);
+                       $table->string('twitch_category')->default('');
+                       $table->integer('twitch_viewers')->default(0);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::table('channels', function (Blueprint $table) {
+                       $table->dropColumn('twitch_live');
+                       $table->dropColumn('twitch_category');
+                       $table->dropColumn('twitch_viewers');
+               });
+       }
+};
diff --git a/database/migrations/2024_04_09_161510_chat_log_category.php b/database/migrations/2024_04_09_161510_chat_log_category.php
new file mode 100644 (file)
index 0000000..1790f86
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+       /**
+        * Run the migrations.
+        */
+       public function up(): void
+       {
+               Schema::table('chat_logs', function (Blueprint $table) {
+                       $table->string('twitch_category')->default('');
+                       $table->index(['twitch_category']);
+               });
+       }
+
+       /**
+        * Reverse the migrations.
+        */
+       public function down(): void
+       {
+               Schema::table('chat_logs', function (Blueprint $table) {
+                       $table->dropColumn('twitch_category');
+               });
+       }
+};