option('daemon')) { $this->recordOnce($streamService); return Command::SUCCESS; } $interval = max(5, (int) $this->option('interval')); $this->info("Starting daemon mode (interval: {$interval}s)..."); while (true) { $this->recordOnce($streamService); sleep($interval); } } private function recordOnce(RadioStreamService $streamService): void { $enabled = Cache::remember('setting_radio_enabled', 60, function () { return WebsiteSetting::where('key', 'radio_enabled')->first()?->value; }); if ($enabled !== '1') { return; } $apiUrl = $this->getNowPlayingApiUrl(); if (! $apiUrl) { return; } $nowPlaying = $streamService->getNowPlaying($apiUrl); if (! $nowPlaying || empty($nowPlaying['song'])) { return; } $title = $nowPlaying['song']; $artist = $nowPlaying['artist'] ?? null; $recorded = RadioSongPlay::recordNowPlaying($title, $artist, [ 'artwork_url' => $nowPlaying['artwork'] ?? null, ]); if ($recorded) { $this->info("Recorded: {$title}" . ($artist ? " by {$artist}" : '')); } } private function getNowPlayingApiUrl(): ?string { $enabled = Cache::remember('setting_radio_now_playing_enabled', 60, function () { return WebsiteSetting::where('key', 'radio_now_playing_enabled')->first()?->value; }); if ($enabled !== '1') { return null; } $apiUrl = Cache::remember('setting_radio_now_playing_api_url', 60, function () { return WebsiteSetting::where('key', 'radio_now_playing_api_url')->first()?->value; }); if ($apiUrl) { return $apiUrl; } $azureCastUrl = Cache::remember('setting_radio_azurecast_base_url', 60, function () { return WebsiteSetting::where('key', 'radio_azurecast_base_url')->first()?->value; }); if ($azureCastUrl) { $stationId = Cache::remember('setting_radio_azurecast_station_id', 60, function () { return WebsiteSetting::where('key', 'radio_azurecast_station_id')->first()?->value ?? '1'; }); return rtrim($azureCastUrl, '/') . '/api/nowplaying/' . $stationId; } return null; } }