$this->line('please acquire a token for localhorsttv first');
return 1;
}
+ if ($this->token->hasExpired()) {
+ $this->line('access token has expired, refreshing');
+ $this->token->refresh();
+ }
$channels = Channel::where('twitch_chat', '!=', '')->where('twitch_id', '=', '')->get();
foreach ($channels as $channel) {
try {
->where(function($query) {
$query->where('join', true);
$query->orWhere('chat', true);
- })->chunk(100, function ($channels) {
+ })
+ ->chunk(100, function ($channels) {
$this->updateChannelInfos($channels);
});
return Command::SUCCESS;
.'&scope='.implode('+', array_map('rawurlencode', $this->scope));
}
+ public function hasExpired() {
+ return now()->isAfter($this->expires_at);
+ }
+
public function refresh() {
$rsp = Http::post('https://id.twitch.tv/oauth2/token', [
'client_id' => config('twitch.client_id'),
$this->scope = $rsp['scope'];
$this->access = $rsp['access_token'];
$this->refresh = $rsp['refresh_token'];
+ $this->expires_at = now()->add($rsp['expires_in'], 'second');
$this->save();
}
if (!$this->token) {
throw new \Exception('unable to find access token');
}
+ if ($this->token->hasExpired()) {
+ $this->token->refresh();
+ }
$this->connector = new Connector();
$this->connect();
--- /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('twitch_tokens', function (Blueprint $table) {
+ $table->timestamp('expires_at')->nullable()->default(null);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ */
+ public function down(): void
+ {
+ Schema::table('twitch_tokens', function (Blueprint $table) {
+ $table->dropColumn('expires_at');
+ });
+ }
+};