From 07a88747f8a252b41b739185fcb68bdee3a60f9a Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 9 Apr 2024 18:34:39 +0200 Subject: [PATCH] track twitch category where chats were sent in --- app/Console/Commands/TwitchChannelInfo.php | 43 +++++++++++++++++-- app/Models/ChatLog.php | 3 ++ .../2024_04_09_160015_channel_twitch_info.php | 32 ++++++++++++++ .../2024_04_09_161510_chat_log_category.php | 29 +++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2024_04_09_160015_channel_twitch_info.php create mode 100644 database/migrations/2024_04_09_161510_chat_log_category.php diff --git a/app/Console/Commands/TwitchChannelInfo.php b/app/Console/Commands/TwitchChannelInfo.php index 09dc718..6a3261c 100644 --- a/app/Console/Commands/TwitchChannelInfo.php +++ b/app/Console/Commands/TwitchChannelInfo.php @@ -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; } diff --git a/app/Models/ChatLog.php b/app/Models/ChatLog.php index c28a072..41bdcc5 100644 --- a/app/Models/ChatLog.php +++ b/app/Models/ChatLog.php @@ -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 index 0000000..6e55a50 --- /dev/null +++ b/database/migrations/2024_04_09_160015_channel_twitch_info.php @@ -0,0 +1,32 @@ +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 index 0000000..1790f86 --- /dev/null +++ b/database/migrations/2024_04_09_161510_chat_log_category.php @@ -0,0 +1,29 @@ +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'); + }); + } +}; -- 2.39.2