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 pathapi.php
More file actions
162 lines (136 loc) · 4.82 KB
/
Copy pathapi.php
File metadata and controls
162 lines (136 loc) · 4.82 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
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
$allowedUrls = [
'https://stream.zeno.fm/yn65fsaurfhvv',
'https://sv2.globalhostlive.com/proxy/bendistereo/stream2',
'https://azuracast.invictamix.pt:8093/emissao.mp3',
'https://stream.radiorostova.ru/radio-rostova-high.mp3',
// Adicione outras URLs permitidas aqui
];
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' => 30 // Definindo um timeout para a conexão
]
]);
$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;
}
$offset = 0;
$maxReads = 10; // Definindo um número máximo de leituras para evitar loops infinitos
while (!feof($stream) && $maxReads > 0) {
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, "' ");
}
$offset += $metaDataInterval + $interval;
$maxReads--;
}
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 getAlbumArt($artist, $song) {
$url = 'https://itunes.apple.com/search?term=' . urlencode("$artist $song") . '&media=music&limit=1';
$response = @file_get_contents($url);
if ($response === false) {
return null;
}
$data = json_decode($response, true);
if ($data['resultCount'] > 0) {
return str_replace('100x100bb', '512x512bb', $data['results'][0]['artworkUrl100']);
}
return null;
}
function getHistoryFileName($url, $ignoreFirst = true) {
$suffix = $ignoreFirst ? '_ignore_first.json' : '.json';
return 'history_' . md5($url) . $suffix;
}
function updateHistory($url, $artist, $song) {
$historyFile = getHistoryFileName($url, false); // Alterado para não ignorar a primeira entrada
$historyLimit = 5;
if (!file_exists($historyFile)) {
$history = [];
} else {
$history = json_decode(file_get_contents($historyFile), true);
if ($history === null) {
$history = [];
}
}
// Verifica se a música já está no histórico
$currentSong = ["title" => $song, "artist" => $artist];
$existingIndex = array_search($currentSong, array_column($history, 'song'));
if ($existingIndex !== false) {
// Remove a entrada existente para evitar duplicações
array_splice($history, $existingIndex, 1);
}
// Adiciona a nova música no início do histórico
array_unshift($history, ["song" => $currentSong]);
$history = array_slice($history, 0, $historyLimit);
file_put_contents($historyFile, json_encode($history));
return $history;
}
header('Content-Type: application/json');
$url = $_GET['url'];
$interval = isset($_GET['interval']) ? (int)$_GET['interval'] : 19200;
if (!filter_var($url, FILTER_VALIDATE_URL)) {
echo json_encode(["error" => "Invalid URL"]);
exit;
}
if (!in_array($url, $allowedUrls)) {
echo json_encode(["error" => "URL not allowed, to use, send an email contato@jailson.es"]);
exit;
}
$title = getMp3StreamTitle($url, $interval);
if ($title) {
[$artist, $song] = extractArtistAndSong($title);
$artUrl = getAlbumArt($artist, $song);
$history = updateHistory($url, $artist, $song);
// Ignorar o primeiro elemento do histórico se existir
$filteredHistory = array_slice($history, 1);
$response = [
"artist" => $artist,
"song" => $song,
"history" => array_map(function($entry) {
return [
"artist" => $entry['song']['artist'],
"song" => $entry['song']['title']
];
}, $filteredHistory)
];
echo json_encode($response);
} else {
echo json_encode(["error" => "Failed to retrieve stream title"]);
}