query("SELECT artist, song, requester, youtube_url, duration, UNIX_TIMESTAMP(created_at) as started_at FROM song_requests WHERE status = 'played' ORDER BY created_at DESC LIMIT 1"); $override = $stmt->fetch(); if ($override) { $duration = (int)($override['duration'] ?? 240); $startedAt = $override['started_at']; $endAt = $startedAt + $duration; $remaining = $endAt - time(); if ($remaining > 0) { echo json_encode([ 'success' => true, 'source' => 'local_request', 'artist' => $override['artist'], 'title' => $override['song'], 'requester' => $override['requester'], 'youtube_url' => $override['youtube_url'], 'duration' => $duration, 'started_at' => date('c', $startedAt), 'end_at' => date('c', $endAt), 'remaining' => $remaining, 'cover' => './assets/pasted-20260215-163754-def41f49.png' ]); exit; } } // Fallback: Proxy RadioKing metadata $radioKingUrl = 'https://www.radioking.com/widgets/api/v1/radio/828046/track/current'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $radioKingUrl); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); $resp = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode === 200 && $resp) { $data = json_decode($resp, true); $duration = $data['duration'] ?? 0; $endAt = $data['end_at'] ?? null; $startedAt = null; if ($endAt && $duration) { $startedAt = date('c', strtotime($endAt) - $duration); } echo json_encode([ 'success' => true, 'source' => 'radioking', 'artist' => $data['artist'] ?? 'Lili Records', 'title' => $data['title'] ?? 'La mejor música', 'cover' => $data['cover'] ?? './assets/pasted-20260215-163754-def41f49.png', 'duration' => $duration, 'started_at' => $startedAt, 'end_at' => $endAt, 'next_track' => $data['next_track'] ?? null ]); } } catch (Exception $e) { echo json_encode(['success' => false, 'error' => $e->getMessage()]); }