<?php
header('Content-Type: text/html; charset=UTF-8');
$pluto_url = 'https://i.mjh.nz/PlutoTV/.channels.json.gz';
$samsung_url = 'https://i.mjh.nz/SamsungTVPlus/.channels.json.gz';
function extract_gzipped_json($url) {
$content = file_get_contents($url);
if ($content === false) {
return null;
}
$json_data = gzdecode($content);
if ($json_data === false) {
return null;
}
return json_decode($json_data, true);
}
function get_countries($url, $platform) {
$data = extract_gzipped_json($url);
$countries = [];
if ($data) {
if (isset($data['regions'])) {
foreach ($data['regions'] as $code => $info) {
if (isset($info['name'])) {
$countries[$code] = $info['name'];
}
}
}
}
return $countries;
}
function write_m3u_file($filename, $content) {
$filepath = __DIR__ . '/' . $filename;
file_put_contents($filepath, $content);
chmod($filepath, 0755);
return "M3U file saved: $filename";
}
function generate_m3u_by_country($data, $source_prefix, $country_code) {
if (!isset($data['regions'][$country_code])) {
return ["Country code '$country_code' not found for $source_prefix"];
}
$region = $data['regions'][$country_code];
if ($source_prefix === 'pluto') {
$tvg_url = 'https://i.mjh.nz/PlutoTV/' . strtolower($country_code) . '.xml.gz';
$link_prefix = 'https://jmp2.uk/plu-';
} elseif ($source_prefix === 'samsung') {
$tvg_url = 'https://i.mjh.nz/SamsungTVPlus/' . strtolower($country_code) . '.xml.gz';
$link_prefix = 'https://jmp2.uk/sam-';
} else {
return ["Invalid source prefix"];
}
$content = '#EXTM3U url-tvg="' . $tvg_url . '"' . "\n";
if (isset($region['channels'])) {
foreach ($region['channels'] as $channel_id => $channel_info) {
if (isset($channel_info['name'], $channel_info['logo'], $channel_info['group'])) {
$group_title = $channel_info['group'];
$channel_name = $channel_info['name'];
$channel_logo = $channel_info['logo'];
$content .= '#EXTINF:-1 tvg-id="' . $channel_id . '" tvg-logo="' . $channel_logo . '" group-title="' . $group_title . '",' . $channel_name . "\n";
$content .= $link_prefix . urlencode($channel_id) . '.m3u8' . "\n";
}
}
}
$filename = $source_prefix . 'tv_' . strtolower($country_code) . '.m3u';
return write_m3u_file($filename, $content);
}
if (isset($_GET['fetch_countries'])) {
$platform = $_GET['platform'];
$url = '';
if ($platform === 'pluto') {
$url = $pluto_url;
} elseif ($platform === 'samsung') {
$url = $samsung_url;
}
if ($url) {
$countries = get_countries($url, $platform);
echo json_encode($countries);
} else {
echo json_encode(['error' => 'Invalid platform']);
}
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>M3U File Generator</title>
<style>
body, html { font-family: Arial, sans-serif; background-color: #f0f2f5; }
.container { max-width: 600px; margin: 0 auto; padding: 20px; background-color: #fff; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1); }
h2 { text-align: center; color: #007BFF; margin-bottom: 20px; }
select, button { width: 100%; padding: 10px; margin-top: 10px; }
.output { margin-top: 20px; background-color: #e9ecef; padding: 15px; border-radius: 5px; }
</style>
</head>
<body>
<div class="container">
<h2>M3U File Generator</h2>
<form method="POST">
<label for="platform">Select Platform:</label>
<select name="platform" id="platform" required onchange="fetchCountries()">
<option value="">-- Select Platform --</option>
<option value="pluto">Pluto TV</option>
<option value="samsung">Samsung TV Plus</option>
</select>
<label for="country_code">Select Country:</label>
<select name="country_code" id="country_code" required>
<option value="">-- Select Country --</option>
</select>
<button type="submit">Generate M3U</button>
</form>
<div class="output" id="output">
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['platform'], $_POST['country_code'])) {
$platform = $_POST['platform'];
$country_code = $_POST['country_code'];
$url = $platform === 'pluto' ? $pluto_url : $samsung_url;
$data = extract_gzipped_json($url);
$message = generate_m3u_by_country($data, $platform, $country_code);
echo "<pre>" . implode("\n", (array)$message) . "</pre>";
}
?>
</div>
<?php if (!empty($message)): ?>
<script>
if (window.opener) {
window.opener.location.reload();
}
</script>
<?php endif; ?>
</div>
<script>
async function fetchCountries() {
const platform = document.getElementById('platform').value;
const countrySelect = document.getElementById('country_code');
countrySelect.innerHTML = '<option value="">Loading...</option>';
const response = await fetch(`?fetch_countries=1&platform=${platform}`);
const data = await response.json();
countrySelect.innerHTML = '';
if (data.error) {
countrySelect.innerHTML = `<option value="">${data.error}</option>`;
return;
}
for (const [code, name] of Object.entries(data)) {
const option = document.createElement('option');
option.value = code;
option.textContent = `${name} (${code.toUpperCase()})`;
countrySelect.appendChild(option);
}
}
</script>
</body>
</html>