This repository was archived by the owner on Jul 6, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi5.php
More file actions
246 lines (205 loc) · 9.58 KB
/
Copy pathapi5.php
File metadata and controls
246 lines (205 loc) · 9.58 KB
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
<?php
// Configuração de exibição de erros
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
// URLs permitidas para acesso (se a verificação de URL estiver habilitada)
$allowedUrls = [
'https://stream.zeno.fm/yn65fsaurfhvv',
'https://sv2.globalhostlive.com/proxy/bendistereo/stream2'
// Adicione outras URLs permitidas aqui
];
// Desabilita a verificação de URLs permitidas
$disableUrlCheck = true;
// Limite de requisições por segundo por URL
$requestsPerSecondLimit = 5;
// Classe para gerenciar informações do streaming
class StreamManager {
private $allowedUrls; // URLs permitidas
private $historyManager; // Gerenciador de histórico de músicas
private $disableUrlCheck; // Flag para desabilitar a verificação de URL
public function __construct(array $allowedUrls, bool $disableUrlCheck) {
$this->allowedUrls = $allowedUrls;
$this->disableUrlCheck = $disableUrlCheck;
}
// Obtém o título da música do streaming
public function getStreamTitle($streamingUrl, $interval = 19200): ?string {
$needle = 'StreamTitle='; // String que indica o início do título da música nos metadados
$headers = [
'Icy-MetaData: 1', // Solicita metadados do streaming
'User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36' // Define um user agent
];
// Cria um contexto de stream com os cabeçalhos e timeout
$context = stream_context_create([
'http' => [
'header' => implode("\r\n", $headers),
'timeout' => 30 // Tempo limite para a conexão
]
]);
// Abre o streaming
$stream = @fopen($streamingUrl, 'r', false, $context);
if ($stream === false) {
return null; // Erro ao abrir o streaming
}
// Lê os cabeçalhos para encontrar o intervalo de metadados
$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; // Intervalo de metadados não encontrado
}
$offset = 0;
$maxReads = 10; // Limita o número de leituras para evitar loops infinitos
$readAttempts = 0; // Contador de tentativas de leitura
// Lê o stream em blocos até encontrar o título da música
while (!feof($stream) && $maxReads > 0 && $readAttempts < 5) {
fread($stream, $metaDataInterval); // Pula o intervalo de metadados
$buffer = fread($stream, $interval); // Lê um bloco de dados
$titleIndex = strpos($buffer, $needle); // Procura pelo início do título
if ($titleIndex !== false) {
$title = substr($buffer, $titleIndex + strlen($needle));
$title = substr($title, 0, strpos($title, ';')); // Extrai o título
fclose($stream);
return trim($title, "' "); // Remove aspas e espaços extras
}
$offset += $metaDataInterval + $interval;
$maxReads--;
$readAttempts++;
}
fclose($stream);
return null; // Título não encontrado
}
// Extrai o artista e a música do título
public function extractArtistAndSong($title): array {
$title = trim($title, "'");
if (strpos($title, '-') !== false) {
[$artist, $song] = explode('-', $title, 2);
return [trim($artist), trim($song)];
}
return ['', trim($title)];
}
// Obtém a URL da capa do álbum a partir do iTunes
public function getAlbumArt($artist, $song): ?string {
$url = 'https://itunes.apple.com/search?term=' . urlencode("$artist $song") . '&media=music&limit=1';
$response = @file_get_contents($url);
if ($response === false) {
return null; // Erro ao obter dados do iTunes
}
$data = json_decode($response, true);
if (!empty($data) && isset($data['resultCount']) && $data['resultCount'] > 0) {
return str_replace('100x100bb', '512x512bb', $data['results'][0]['artworkUrl100']);
}
return null; // Capa não encontrada
}
// Atualiza o histórico de músicas
public function updateHistory($url, $artist, $song): array {
$this->historyManager = new HistoryManager($url);
$this->historyManager->addSong($artist, $song);
return $this->historyManager->getHistory(true);
}
}
class HistoryManager {
private $url; // URL do streaming
private $historyFile; // Arquivo JSON para armazenar o histórico
private $historyLimit = 5; // Limite de músicas no histórico
public function __construct($url) {
$this->url = $url;
$this->historyFile = $this->getHistoryFileName(); // Obtém o nome do arquivo de histórico
}
// Obtém o nome do arquivo de histórico com base na URL
private function getHistoryFileName(): string {
$historyDir = 'history'; // Diretório para armazenar os históricos
if (!file_exists($historyDir)) {
mkdir($historyDir, 0755, true); // Cria o diretório se não existir
}
return $historyDir . '/' . hash('sha256', $this->url) . '.json';
}
// Carrega o histórico do arquivo JSON
public function loadHistory(): array {
if (file_exists($this->historyFile)) {
$history = json_decode(file_get_contents($this->historyFile), true);
return $history !== null ? $history : []; // Retorna o histórico ou um array vazio se não for válido
}
return []; // Retorna um array vazio se o arquivo não existir
}
// Salva o histórico no arquivo JSON
public function saveHistory(array $history): void {
file_put_contents($this->historyFile, json_encode($history));
}
// Adiciona uma música ao histórico
public function addSong($artist, $song): void {
$history = $this->loadHistory(); // Carrega o histórico existente
$currentSong = ["title" => $song, "artist" => $artist]; // Cria um array para a música atual
// Verifica se a música já está no histórico
$existingIndex = array_search($currentSong, array_column($history, 'song'));
if ($existingIndex !== false) {
unset($history[$existingIndex]); // Remove a entrada existente para evitar duplicações
}
// Adiciona a nova música no início do histórico
array_unshift($history, ["song" => $currentSong]);
// Limita o histórico ao número máximo de entradas
$history = array_slice($history, 0, $this->historyLimit);
$this->saveHistory($history); // Salva o histórico atualizado
}
// Obtém o histórico de músicas, opcionalmente ignorando a primeira entrada
public function getHistory(bool $ignoreFirst = true): array {
$history = $this->loadHistory();
return $ignoreFirst ? array_slice($history, 1) : $history; // Ignora a primeira entrada se $ignoreFirst for true
}
}
// Define o cabeçalho para indicar que a resposta é JSON
header('Content-Type: application/json');
// Habilita CORS para todas as origens
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
// Trata requisições de preflight OPTIONS
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
exit; // Não precisa processar mais nada para OPTIONS
}
// Obtém a URL e o intervalo da requisição GET
$url = filter_input(INPUT_GET, 'url', FILTER_VALIDATE_URL);
$interval = filter_input(INPUT_GET, 'interval', FILTER_VALIDATE_INT, ["options" => ["default" => 19200]]);
// Define o formato de saída desejado (true para o novo formato, false para o original)
$outputFormat = true; // Altere para false se quiser o formato original
// Verifica se a URL é válida
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode(["error" => "URL inválida"]);
exit; // Encerra o script se a URL for inválida
}
// Cria uma instância do StreamManager
$streamManager = new StreamManager($allowedUrls, $disableUrlCheck);
// Obtém o título da música a partir da URL do streaming
$title = $streamManager->getStreamTitle($url, $interval);
// Processa a resposta com base no título obtido
if ($title) {
[$artist, $song] = $streamManager->extractArtistAndSong($title);
$artUrl = $streamManager->getAlbumArt($artist, $song);
$history = $streamManager->updateHistory($url, $artist, $song);
// Monta a resposta JSON com base no formato definido
if ($outputFormat) {
$response = [
"songtitle" => "$song - $artist",
"artist" => $artist,
"song" => $song,
"source" => $url,
"artwork" => $artUrl,
"song_history" => $history
];
} else {
$response = [
"currentSong" => $song,
"currentArtist" => $artist,
"songHistory" => $history,
"albumArt" => $artUrl
];
}
} else {
$response = ["error" => "Falha ao obter o título da transmissão"];
}
echo json_encode($response);