From: Daniel Karbach Date: Mon, 13 Mar 2023 11:39:13 +0000 (+0100) Subject: SRA sync command X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c426e270c4da363fd5547cad6ff185d2e7c94e43;p=alttp.git SRA sync command --- diff --git a/.env.example b/.env.example index ffdc53b..ccc63e6 100644 --- a/.env.example +++ b/.env.example @@ -73,3 +73,5 @@ AOS_URL=https://aos.localhorst.tv TWITCH_CLIENT_ID= TWITCH_CLIENT_SECRET= TWITCH_REDIRECT_URI= + +GOOGLE_API_KEY= diff --git a/app/Console/Commands/SyncSRA.php b/app/Console/Commands/SyncSRA.php new file mode 100644 index 0000000..45c2002 --- /dev/null +++ b/app/Console/Commands/SyncSRA.php @@ -0,0 +1,99 @@ +channel = Channel::where('ext_id', '=', 'sra:SRA')->firstOrFail(); + $this->org = Organization::where('name', '=', 'sra')->firstOrFail(); + + $events = Event::where('external_schedule', 'LIKE', 'sra:%') + ->where(function (Builder $query) { + $query->whereNull('end'); + $query->orWhere('end', '<', now()); + }) + ->get(); + + $schedule = Http::get('https://sheets.googleapis.com/v4/spreadsheets/1J6iczoFRxG-NKCMqFugFqkvLSpPssegpIEMiNmFjwvI/values/Races', [ + 'alt' => 'json', + 'key' => config('google.api_key'), + ])->json(); + + foreach ($events as $event) { + try { + $this->line('syncing '.$event->name); + $this->syncEvent($event, $schedule['values']); + } catch (\Exception $e) { + $this->error('error syncing event '.$event->name.': '.$e->getMessage()); + } + } + + return Command::SUCCESS; + } + + private function syncEvent(Event $event, $schedule) { + $tag = substr($event->external_schedule, 4); + for ($i = 3; $i < count($schedule); ++$i) { + if (!Str::startsWith($schedule[$i][2], $tag)) continue; + $this->syncSchedule($event, $schedule[$i]); + } + + } + + private function syncSchedule(Event $event, $row) { + $ext_id = 'sra:'.$row[0].'-'.$row[1]; + $episode = Episode::firstWhere('ext_id', '=', $ext_id); + if (!$episode) { + $episode = new Episode(); + $episode->ext_id = $ext_id; + } + $episode->event()->associate($event); + if (strlen($row[2]) > (strlen($event->external_schedule) - 1)) { + $episode->title = substr($row[2], strlen($event->external_schedule) - 1); + } else { + $episode->title = $row[2]; + } + $start = Carbon::createFromFormat('m/d/Y H:i', $row[0].' '.$row[1], 'America/Chicago'); + if (!$episode->start || $start->ne($episode->start)) { + $episode->start = $start; + } + $episode->estimate = 90 * 60; + $episode->confirmed = true; + $episode->save(); + $episode->channels()->syncWithoutDetaching([$this->channel->id]); + } + + private $channel; + private $org; + +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 2fc689c..904bfd1 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -17,6 +17,7 @@ class Kernel extends ConsoleKernel { $schedule->command('sync:avatars')->everyFiveMinutes(); $schedule->command('sync:speedgaming')->everyFiveMinutes(); + $schedule->command('sync:sra')->everyFifteenMinutes(); } /** diff --git a/config/google.php b/config/google.php new file mode 100644 index 0000000..e6915ef --- /dev/null +++ b/config/google.php @@ -0,0 +1,5 @@ + env('GOOGLE_API_KEY', ''), +];