-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapi.php
262 lines (216 loc) · 8.32 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// Configura tus credenciales de Spotify
define('SPOTIFY_CLIENT_ID', '363efb40421f40c1af9f4ef11c697168'); // Reemplaza con tu client_id
define('SPOTIFY_CLIENT_SECRET', 'c4ff252eba7744e3822893133f272ccb'); // Reemplaza con tu client_secret
function getSpotifyToken() {
$url = 'https://accounts.spotify.com/api/token';
$headers = [
'Authorization: Basic ' . base64_encode(SPOTIFY_CLIENT_ID . ':' . SPOTIFY_CLIENT_SECRET)
];
$data = [
'grant_type' => 'client_credentials'
];
$options = [
'http' => [
'header' => implode("\r\n", $headers),
'method' => 'POST',
'content' => http_build_query($data)
]
];
$context = stream_context_create($options);
$response = @file_get_contents($url, false, $context);
if ($response === false) {
return null;
}
$tokenData = json_decode($response, true);
return $tokenData['access_token'] ?? null;
}
function getMp3StreamTitle($streamingUrl, $interval) {
$needle = 'StreamTitle=';
$headers = [
'Icy-MetaData: 1',
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36'
];
$context = stream_context_create([
'http' => [
'header' => implode("\r\n", $headers),
'timeout' => 60 // Incrementar timeout a 60 segundos
]
]);
$stream = @fopen($streamingUrl, 'r', false, $context);
if ($stream === false) {
return null;
}
$metaDataInterval = null;
foreach ($http_response_header as $header) {
if (stripos($header, 'icy-metaint') !== false) {
$metaDataInterval = (int)trim(explode(':', $header)[1]);
break;
}
}
if ($metaDataInterval === null) {
fclose($stream);
return null;
}
while (!feof($stream)) {
fread($stream, $metaDataInterval);
$buffer = fread($stream, $interval);
$titleIndex = strpos($buffer, $needle);
if ($titleIndex !== false) {
$title = substr($buffer, $titleIndex + strlen($needle));
$title = substr($title, 0, strpos($title, ';'));
fclose($stream);
return trim($title, "' ");
}
}
fclose($stream);
return null;
}
function extractArtistAndSong($title) {
$title = trim($title, "'");
if (strpos($title, '-') !== false) {
[$artist, $song] = explode('-', $title, 2);
return [trim($artist), trim($song)];
}
return ['', trim($title)];
}
function getAlbumInfo($artist, $song) {
$token = getSpotifyToken();
if (!$token) {
return [null, 'No disponible', 'No disponible', 'No disponible', 0];
}
$url = 'https://api.spotify.com/v1/search?q=' . urlencode("track:$song artist:$artist") . '&type=track&limit=1';
$headers = [
'Authorization: Bearer ' . $token
];
$options = [
'http' => [
'header' => implode("\r\n", $headers),
'method' => 'GET'
]
];
$context = stream_context_create($options);
$response = @file_get_contents($url, false, $context);
if ($response === false) {
return [null, 'No disponible', 'No disponible', 'No disponible', 0];
}
$data = json_decode($response, true);
if (isset($data['tracks']['items'][0])) {
$track = $data['tracks']['items'][0];
$album = $track['album']['name'] ?? 'No disponible';
$artworkUrl = $track['album']['images'][0]['url'] ?? null;
$year = isset($track['album']['release_date']) ? substr($track['album']['release_date'], 0, 4) : 'No disponible';
// Duración en milisegundos
$durationMs = $track['duration_ms'] ?? 0;
// Obtener el género del artista
$artistId = $track['artists'][0]['id'];
$artistUrl = "https://api.spotify.com/v1/artists/$artistId";
$artistResponse = @file_get_contents($artistUrl, false, $context);
$artistData = json_decode($artistResponse, true);
$genres = $artistData['genres'] ?? [];
$genre = !empty($genres) ? implode(', ', $genres) : 'No disponible';
return [$artworkUrl, $album, $year, $genre, $durationMs];
}
return [null, 'No disponible', 'No disponible', 'No disponible', 0];
}
function updateHistory($url, $artist, $song) {
$historyFile = 'history_' . md5($url) . '.json';
$historyLimit = 10;
if (!file_exists($historyFile)) {
$history = [];
} else {
$history = json_decode(file_get_contents($historyFile), true);
if ($history === null) {
$history = [];
}
}
$currentSong = ["title" => $song, "artist" => $artist];
$existingIndex = array_search($currentSong, array_column($history, 'song'));
if ($existingIndex !== false) {
array_splice($history, $existingIndex, 1);
}
array_unshift($history, ["song" => $currentSong]);
$history = array_slice($history, 0, $historyLimit);
file_put_contents($historyFile, json_encode($history));
return $history;
}
// Funcion Para Leer Las Canciones
header('Content-Type: application/json');
// URL de streaming
$url = isset($_GET['url']) ? $_GET['url'] : null;
$interval = isset($_GET['interval']) ? (int)$_GET['interval'] : 19200;
if ($url === null) {
echo json_encode(["error" => "URL parameter is missing"]); // User-friendly error message
exit;
}
// Intentar obtener el start_time desde el archivo
$start_time_file = 'start_time_' . md5($url) . '.txt';
$previous_song_file = 'previous_song_' . md5($url) . '.txt';
if (file_exists($previous_song_file)) {
// Leer la canción anterior desde el archivo
$previous_song = file_get_contents($previous_song_file);
} else {
$previous_song = null;
}
if (file_exists($start_time_file)) {
// Si el archivo existe, leer el start_time desde él
$start_time = (int)file_get_contents($start_time_file);
} else {
// Si no existe, asignar un start_time basado en la hora actual
$start_time = time();
// Guardar el start_time en el archivo
file_put_contents($start_time_file, $start_time);
}
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode(["error" => "Invalid URL format"]); // More specific error message
exit;
}
$title = getMp3StreamTitle($url, $interval);
if ($title) {
[$artist, $song] = extractArtistAndSong($title);
// Si la canción ha cambiado, reiniciar el start_time
if ($song !== $previous_song) {
// Reiniciar el start_time
$start_time = time();
file_put_contents($start_time_file, $start_time);
file_put_contents($previous_song_file, $song); // Guardar la canción actual
}
[$artUrl, $album, $year, $genre, $durationMs] = getAlbumInfo($artist, $song);
// Convertimos la duración de la canción de milisegundos a segundos
$duration = $durationMs / 1000; // Duración de la canción en segundos
// Calcular el tiempo transcurrido desde que se inició la canción
$elapsed = time() - $start_time; // Tiempo transcurrido en segundos
$elapsed = min($elapsed, $duration); // Limitar el tiempo transcurrido al tiempo total de la canción
// Calcular el tiempo restante
$remaining = max(0, $duration - $elapsed); // Tiempo restante, no puede ser negativo
// Convertir todo a enteros antes de enviar la respuesta
$elapsed = (int) $elapsed; // Elapsed como entero
$remaining = (int) $remaining; // Remaining como entero
$duration = (int) $duration; // Duration como entero
// Actualizar historial de canciones
$history = updateHistory($url, $artist, $song);
$filteredHistory = array_slice($history, 1);
$response = [
"songtitle" => "$artist - $song",
"artist" => $artist,
"song" => $song,
"source" => $url,
"artwork" => $artUrl,
"album" => $album,
"year" => $year,
"genre" => $genre,
"song_history" => $filteredHistory,
"now_playing" => [
"elapsed" => $elapsed, // Elapsed como entero
"remaining" => $remaining, // Remaining como entero
"duration" => $duration // Duration como entero
]
];
// Responder con la información en formato JSON
echo json_encode($response);
} else {
echo json_encode(["error" => "The stream title could not be retrieved."]);
}