From 4caeac216c5e5a044d81b63a8aa5b66451162271 Mon Sep 17 00:00:00 2001 From: Daniel Karbach Date: Tue, 12 Mar 2024 14:50:56 +0100 Subject: [PATCH] endpoint for twitch leaderboard --- app/Console/Commands/TwitchChannelInfo.php | 70 +++++++++++++++++++ app/Console/Kernel.php | 1 + app/Http/Controllers/ChannelController.php | 6 ++ app/Models/TwitchToken.php | 8 +++ .../2024_03_12_133614_channel_twitch_sync.php | 32 +++++++++ routes/api.php | 1 + 6 files changed, 118 insertions(+) create mode 100644 app/Console/Commands/TwitchChannelInfo.php create mode 100644 database/migrations/2024_03_12_133614_channel_twitch_sync.php diff --git a/app/Console/Commands/TwitchChannelInfo.php b/app/Console/Commands/TwitchChannelInfo.php new file mode 100644 index 0000000..09dc718 --- /dev/null +++ b/app/Console/Commands/TwitchChannelInfo.php @@ -0,0 +1,70 @@ +token = TwitchToken::firstWhere('nick', 'localhorsttv'); + if (!$this->token) { + $this->line('please acquire a token for localhorsttv first'); + return 1; + } + $channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get(); + foreach ($channels as $channel) { + try { + $this->updateChannel($channel); + } catch (RequestException $e) { + if ($e->response->status() == 401) { + $this->token->refresh(); + $this->updateChannel($channel); + } + } + } + return Command::SUCCESS; + } + + private function updateChannel(Channel $channel) { + $this->line($channel->twitch_chat); + $login = substr($channel->twitch_chat, 1); + $rsp = $this->token->request() + ->get('/users', [ + 'login' => $login, + ]) + ->throw(); + foreach ($rsp['data'] as $user) { + if ($user['login'] != $login) continue; + $channel->twitch_id = $user['id']; + $channel->save(); + } + } + + private $token; + +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ea3807d..bbb747d 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -15,6 +15,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + $schedule->command('twitch:channel-info')->everyFiveMinutes(); $schedule->command('sync:ladder')->daily(); $schedule->command('sync:speedgaming')->everyFiveMinutes(); $schedule->command('sync:sra')->everyFifteenMinutes(); diff --git a/app/Http/Controllers/ChannelController.php b/app/Http/Controllers/ChannelController.php index 78648af..f7d57c0 100644 --- a/app/Http/Controllers/ChannelController.php +++ b/app/Http/Controllers/ChannelController.php @@ -216,6 +216,12 @@ class ChannelController extends Controller { ]; } + public function getGuessingGameLeaderboard(Channel $channel, $type) { + return [ + 'all' => $channel->getGuessingLeaderboard(), + ]; + } + public function getGuessingGameMonitor($key) { $channel = Channel::where('access_key', '=', $key)->firstOrFail(); diff --git a/app/Models/TwitchToken.php b/app/Models/TwitchToken.php index 9b15977..a4c561e 100644 --- a/app/Models/TwitchToken.php +++ b/app/Models/TwitchToken.php @@ -35,6 +35,14 @@ class TwitchToken extends Model $this->save(); } + public function request() { + return Http::baseUrl('https://api.twitch.tv/helix') + ->withToken($this->access) + ->withHeaders([ + 'Client-Id' => config('twitch.client_id'), + ]); + } + protected $casts = [ 'scope' => 'array', ]; diff --git a/database/migrations/2024_03_12_133614_channel_twitch_sync.php b/database/migrations/2024_03_12_133614_channel_twitch_sync.php new file mode 100644 index 0000000..5f4422c --- /dev/null +++ b/database/migrations/2024_03_12_133614_channel_twitch_sync.php @@ -0,0 +1,32 @@ +string('twitch_id')->default(''); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('channels', function (Blueprint $table) { + $table->dropColumn('twitch_id'); + }); + } +}; diff --git a/routes/api.php b/routes/api.php index 0c897af..363a112 100644 --- a/routes/api.php +++ b/routes/api.php @@ -37,6 +37,7 @@ Route::post('channels/{channel}/guessing-game/{name}', 'App\Http\Controllers\Cha Route::put('channels/{channel}/guessing-game/{name}', 'App\Http\Controllers\ChannelController@saveGuessingGame'); Route::get('guessing-game-monitor/{key}', 'App\Http\Controllers\ChannelController@getGuessingGameMonitor'); +Route::get('guessing-game-leaderboard/twitch/{channel:twitch_id}/{type}', 'App\Http\Controllers\ChannelController@getGuessingGameLeaderboard'); Route::get('content', 'App\Http\Controllers\TechniqueController@search'); Route::get('content/{tech:name}', 'App\Http\Controllers\TechniqueController@single'); -- 2.39.2