0
0
mirror of https://github.com/anthonyaxenov/iptv.git synced 2024-11-22 13:34:45 +00:00

Optimizations and fixes of getinfo

- return empty response immidiately when playlist not found or offline
- timeout 3 sec
- fixed regex to parse channel names
- fixed 'Content-Type' header
This commit is contained in:
Anthony Axenov 2022-06-03 12:15:16 +08:00
parent 57919923a2
commit 516b2a56a4
Signed by: anthony
GPG Key ID: EA9EC32FF7CCD4EC

View File

@ -8,30 +8,47 @@ $updated_at = date('d.m.Y h:i', filemtime('playlists.ini'));
$my_url = $_SERVER['SERVER_NAME'] . '?'; $my_url = $_SERVER['SERVER_NAME'] . '?';
$ini = parse_ini_file('playlists.ini', true); $ini = parse_ini_file('playlists.ini', true);
// получение инфы о плейлисте // get playlist info (ajax)
if (!empty($_GET['getinfo'])) { if (!empty($_GET['getinfo'])) {
$ch = curl_init(); $pls_cfg = $ini[$_GET['getinfo']];
curl_setopt($ch, CURLOPT_URL, $ini[$_GET['getinfo']]['pls']);
unset($ini); unset($ini);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); if (empty($pls_cfg)) {
curl_setopt($ch, CURLOPT_HEADER, 1); die(json_encode([
$response = curl_exec($ch); 'is_online' => false,
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE); 'count' => 0,
$headers = explode("\r\n", substr($response, 0, $header_size)); 'channels' => [],
$content = substr($response, $header_size); ]));
unset($response); }
unset($header_size);
curl_close($ch); $curl = curl_init();
unset($ch); curl_setopt($curl, CURLOPT_URL, $pls_cfg['pls']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_TIMEOUT, 3);
curl_setopt($curl, CURLOPT_HEADER, 1);
$response = curl_exec($curl);
$headers_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
curl_close($curl);
if ($response === false) {
die(json_encode([
'is_online' => false,
'count' => '-',
'channels' => [],
]));
}
$headers = array_unique(explode("\r\n", substr($response, 0, $headers_size)));
$pls_content = substr($response, $headers_size);
unset($response, $headers_size, $curl);
$matches = []; $matches = [];
preg_match_all("/^#EXTINF:-?[\d](?:(\s?url-tvg=\".*\")?(\stvg-logo=\".*\")?(\stvg-name=\".*\")?(\stvg-id=\".*\")?(\sgroup-title=\".*\")?)\s?,\s?(.*)/m", $content, $matches); // preg_match_all("/^#EXTINF:-?[\d](?:(\s?url-tvg=\".*\")?(\stvg-logo=\".*\")?(\stvg-name=\".*\")?(\stvg-id=\".*\")?(\sgroup-title=\".*\")?)\s?,\s?(.*)/m", $content, $matches);
unset($content); preg_match_all("/^#EXTINF:-?\d.*,\s*(.*)/m", $pls_content, $matches);
$channels = $matches[5]; $channels = $matches[1];
unset($matches); unset($pls_content, $matches);
$is_online = is_array($headers) && !empty($headers) && strpos($headers[0], ' 200') !== false; $is_online = !empty($headers) && strpos($headers[0], ' 200') !== false;
unset($headers); unset($headers);
array_walk($channels, function (&$str) { $str = trim($str); }); $channels = array_map('trim', $channels);
header("Content-Type: text/plain; charset=utf-8"); header("Content-Type: application/json; charset=utf-8");
die(json_encode([ die(json_encode([
'is_online' => $is_online, 'is_online' => $is_online,
'count' => $is_online ? count($channels) : '-', 'count' => $is_online ? count($channels) : '-',
@ -39,6 +56,7 @@ if (!empty($_GET['getinfo'])) {
])); ]));
} }
// redirect to playlist
if (array_intersect(array_keys($_GET), array_keys($ini))) { if (array_intersect(array_keys($_GET), array_keys($ini))) {
$id = array_keys($_GET)[0]; $id = array_keys($_GET)[0];
if (!empty($ini[$id]['redirect'])) { if (!empty($ini[$id]['redirect'])) {