Last active
March 4, 2020 17:52
-
-
Save Mr-Groch/64ba1696462a4537d7c3e38ee9d280ec to your computer and use it in GitHub Desktop.
PHP online converters for adding catchup-source tag for PVR IPTV Archive Client on the fly (requires https://github.com/ttodua/useful-php-scripts/blob/master/get-remote-url-content-data.php)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$catchup_channels = array(); | |
function init_xc_catchup_channels($api_result) | |
{ | |
global $catchup_channels; | |
if (empty($api_result)) { | |
return; | |
} | |
else { | |
$result = json_decode($api_result, true); | |
if (json_last_error() === 0) { | |
foreach($result as $key => $value) { | |
if ($value['tv_archive'] == 1) { | |
array_push($catchup_channels, $value); | |
} | |
} | |
} | |
} | |
} | |
if (empty($_GET["url"])) { | |
echo '<form method="get" action="'.htmlspecialchars($_SERVER["PHP_SELF"]).'">'; | |
echo 'URL: <input type="text" name="url" size="100">'; | |
echo '<input type="submit" value="Convert"><br>'; | |
echo 'Output type: <select name="type"><option value="">Unchanged</option><option value="hls">HLS</option><option value="ts">MPEG-TS</option></select><br>'; | |
echo '</form>'; | |
} else { | |
require_once 'get-remote-url-content-data.php'; | |
$type = $_GET['type']; | |
$content = get_remote_data(urldecode($_GET['url'])); | |
if (empty($content)) { | |
echo 'BAD URL'; | |
return; | |
} | |
$init = false; | |
foreach (preg_split('~[\r\n]+~', $content) as $line) { | |
if (preg_match('~^#EXTINF~i', $line) === 1) { | |
$prev_line = $line; | |
continue; | |
} | |
if (preg_match('~^(http[s]?://[^/]+)/(?:live/)?([^/]+)/([^/]+)/([^/\.]+)(\.m3u[8]?)?$~i', $line, $matches) === 1 && isset($prev_line)) { | |
if (!$init) { | |
$init = true; | |
init_xc_catchup_channels(get_remote_data($matches[1].'/player_api.php?username='.$matches[2].'&password='.$matches[3].'&action=get_live_streams')); | |
} | |
if ($type == 'hls') { | |
$matches[5] = '.m3u8'; | |
$line = $matches[1].'/live/'.$matches[2].'/'.$matches[3].'/'.$matches[4].$matches[5]; | |
} elseif ($type == 'ts') { | |
$matches[5] = '.ts'; | |
$line = $matches[1].'/'.$matches[2].'/'.$matches[3].'/'.$matches[4]; | |
} | |
$found = false; | |
foreach($catchup_channels as $key => $value) { | |
if ($value['stream_id'] == $matches[4]) { | |
$extension = ($matches[5] ? $matches[5] : '.ts'); | |
$catchup_str = ' catchup-days="'.$value['tv_archive_duration'].'" catchup="default" catchup-source="'.$matches[1].'/timeshift/'.$matches[2].'/'.$matches[3].'/240/{Y}-{m}-{d}:{H}-{M}/'.$matches[4].$extension.'"'; | |
$new_prev_line = substr_replace($prev_line, $catchup_str, strpos($prev_line, ' '), 0); | |
$new_content .= $new_prev_line.PHP_EOL; | |
$found = true; | |
break; | |
} | |
} | |
if (!$found) { | |
$new_content .= $prev_line.PHP_EOL; | |
} | |
unset($prev_line); | |
} elseif (isset($prev_line)) { | |
$new_content .= $prev_line.PHP_EOL; | |
unset($prev_line); | |
} | |
$new_content .= $line.PHP_EOL; | |
} | |
echo $new_content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment