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;
$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();
}
}
}
+ 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;
}
}
+ 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;
}
--- /dev/null
+<?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');
+ });
+ }
+};
--- /dev/null
+<?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');
+ });
+ }
+};