From: Daniel Karbach Date: Tue, 9 Apr 2024 16:34:39 +0000 (+0200) Subject: track twitch category where chats were sent in X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=07a88747f8a252b41b739185fcb68bdee3a60f9a;p=alttp.git track twitch category where chats were sent in --- 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'); + }); + } +};