3 namespace App\Console\Commands;
5 use App\Models\Channel;
6 use App\Models\Episode;
7 use App\Models\EpisodePlayer;
9 use App\Models\Organization;
12 use Illuminate\Console\Command;
13 use Illuminate\Database\Eloquent\Builder;
14 use Illuminate\Support\Facades\Http;
15 use Illuminate\Support\Str;
17 class SyncZSR extends Command {
20 * The name and signature of the console command.
24 protected $signature = 'sync:zsr';
27 * The console command description.
31 protected $description = 'Synchronize Zelda Speedruns schedule';
34 * Execute the console command.
38 public function handle() {
39 $this->org = Organization::where('name', '=', 'zsr')->firstOrFail();
41 $events = Event::where('external_schedule', 'LIKE', 'zsr:%')
42 ->where(function (Builder $query) {
43 $query->whereNull('end');
44 $query->orWhere('end', '<', now());
48 $from = now()->sub(1, 'day');
49 $to = now()->add(14, 'day');
50 $schedule = Http::get('https://www.googleapis.com/calendar/v3/calendars/zsrstaff%40gmail.com/events', [
52 'key' => config('google.api_key'),
53 'timeMax' => $to->toIso8601String(),
54 'timeMin' => $from->toIso8601String(),
57 foreach ($events as $event) {
59 $this->line('syncing '.$event->name);
60 $this->syncEvent($event, $schedule['items']);
61 } catch (\Exception $e) {
62 $this->error('error syncing event '.$event->name.': '.$e->getMessage());
66 return Command::SUCCESS;
69 private function syncEvent(Event $event, $schedule) {
70 $tag = substr($event->external_schedule, 4);
71 foreach ($schedule as $entry) {
72 if (!Str::startsWith($entry['summary'], $tag)) continue;
73 $this->syncSchedule($event, $entry);
78 private function syncSchedule(Event $event, $entry) {
79 $ext_id = 'zsr:'.$entry['id'];
80 $episode = Episode::firstWhere('ext_id', '=', $ext_id);
82 $episode = new Episode();
83 $episode->ext_id = $ext_id;
85 $episode->event()->associate($event);
86 if (strlen($entry['summary']) > (strlen($event->external_schedule) - 4)) {
87 $episode->title = substr($entry['summary'], strlen($event->external_schedule) - 4);
89 $episode->title = $entry['summary'];
91 $start = Carbon::parse($entry['start']['dateTime'])->setTimezone('UTC');
92 if (!$episode->start || $start->ne($episode->start)) {
93 $episode->start = $start;
95 $end = Carbon::parse($entry['end']['dateTime'])->setTimezone('UTC');
96 $episode->estimate = $start->diffInSeconds($end);
97 $episode->confirmed = true;
98 if (preg_match('/^(.*) - (.*?) vs\.? (.*?)$/u', $episode->title, $matches)) {
99 $episode->title = $matches[1];
101 $this->syncPlayer($episode, $matches[2]);
102 $this->syncPlayer($episode, $matches[3]);
106 if (preg_match('/Restream: https?:\/\/(www\.)?twitch\.tv\/(\w+)/u', $entry['description'], $matches)) {
107 $channel = $this->syncChannel($episode, $matches[2]);
109 $episode->channels()->syncWithoutDetaching([$channel->id]);
114 private function syncChannel(Episode $episode, $zsrChannel) {
115 $ext_id = 'zsr:'.$zsrChannel;
116 $channel = $this->org->channels()->firstWhere('ext_id', '=', $ext_id);
118 $channel = new Channel();
119 $channel->ext_id = $ext_id;
120 $channel->organization()->associate($this->org);
121 $channel->languages = ['en'];
122 $channel->short_name = '';
124 $channel->title = $zsrChannel;
125 $channel->stream_link = 'https://twitch.tv/'.strtolower($zsrChannel);
130 private function syncPlayer(Episode $episode, $zsrPlayer) {
131 $ext_id = 'zsr:'.$zsrPlayer;
132 $player = $episode->players()->firstWhere('ext_id', '=', $ext_id);
134 $player = new EpisodePlayer();
135 $player->ext_id = $ext_id;
136 $player->episode()->associate($episode);
138 $user = $this->getUserByZSRPlayer($zsrPlayer);
140 $player->user()->associate($user);
142 $player->user()->disassociate();
144 $player->name_override = $zsrPlayer;
149 private function getUserByZSRPlayer($player) {
150 $user = User::firstWhere('discord_nickname', '=', $player);
154 $user = User::firstWhere('username', '=', strtolower($player));