<?php
function download_and_extract($urls, $tmp_directory) {
if (!file_exists($tmp_directory)) {
mkdir($tmp_directory, 0755, true);
chown($tmp_directory, 'www-data');
chgrp($tmp_directory, 'www-data');
}
foreach ($urls as $url) {
$filename = basename($url);
$xz_path = $tmp_directory . '/' . $filename;
$target_filename = "Germany_" . str_replace('.xz', '', $filename);
$target_path = $tmp_directory . '/' . $target_filename;
echo "Downloading $url...\n";
$response = file_get_contents($url);
file_put_contents($xz_path, $response);
chown($xz_path, 'www-data');
chgrp($xz_path, 'www-data');
chmod($xz_path, 0755);
$command = "7z e " . escapeshellarg($xz_path) . " -o" . escapeshellarg($tmp_directory);
shell_exec($command);
rename(str_replace('.xz', '', $xz_path), $target_path);
chown($target_path, 'www-data');
chgrp($target_path, 'www-data');
chmod($target_path, 0755);
unlink($xz_path);
}
}
function is_file_recent($filepath, $age_limit = 86400) {
if (!file_exists($filepath)) {
return false;
}
$file_age = time() - filemtime($filepath);
return $file_age < $age_limit;
}
function check_files_recent($files, $age_limit = 86400) {
foreach ($files as $file) {
if (!is_file_recent($file, $age_limit)) {
return false;
}
}
return true;
}
function build_epg($country, $tmp_directory, $target_directory, $matching_files) {
$temp_file_path = $tmp_directory . '/epgtemp.xml';
$output_file = fopen($temp_file_path, 'a');
fwrite($output_file, '<?xml version="1.0" encoding="UTF-8"?>' . "\n");
$matching_files = array_values($matching_files);
if (isset($matching_files[0])) {
$first_file_path = $matching_files[0];
echo "Processing first file: $first_file_path\n";
$first_file_content = file_get_contents($first_file_path);
preg_match_all('/<tv[^>]*>.*?<channel id="[^"]+">.*?<\/channel>/s', $first_file_content, $header_and_channels);
foreach ($header_and_channels[0] as $part) {
fwrite($output_file, $part . "\n");
}
} else {
echo "No files found to process.\n";
return;
}
foreach ($matching_files as $file) {
$content = file_get_contents($file);
preg_match_all('/<channel id="[^"]+">.*?<\/channel>/s', $content, $channels);
foreach ($channels[0] as $channel_content) {
fwrite($output_file, $channel_content . "\n");
}
}
foreach ($matching_files as $file) {
$content = file_get_contents($file);
preg_match_all('/<programme start="[^"]+".*?<\/programme>/s', $content, $programmes);
foreach ($programmes[0] as $programme_content) {
fwrite($output_file, $programme_content . "\n");
}
}
fwrite($output_file, '</tv>' . "\n");
fclose($output_file);
chown($temp_file_path, 'www-data');
chgrp($temp_file_path, 'www-data');
chmod($temp_file_path, 0755);
$final_file_path = $target_directory . '/' . $country . '.xml';
rename($temp_file_path, $final_file_path);
chown($final_file_path, 'www-data');
chgrp($final_file_path, 'www-data');
chmod($final_file_path, 0755);
echo "EPG file saved to $final_file_path\n";
}
$expected_files = [
"Germany_rytecAT_Basic",
"Germany_rytecCH_Basic",
"Germany_rytecDE_Basic",
"Germany_rytecDE_Common",
"Germany_rytecDE_SportMovies",
"Germany_rytecERO",
"Germany_rytecNWS",
];
$urls = [
"http://www.xmltvepg.nl/rytecAT_Basic.xz",
"http://www.xmltvepg.nl/rytecCH_Basic.xz",
"http://www.xmltvepg.nl/rytecDE_Basic.xz",
"http://www.xmltvepg.nl/rytecDE_Common.xz",
"http://www.xmltvepg.nl/rytecDE_SportMovies.xz",
"http://www.xmltvepg.nl/rytecERO.xz",
"http://www.xmltvepg.nl/rytecNWS.xz",
];
$script_dir = __DIR__;
$tmp_folder = $script_dir . '/tmp';
$target_folder = __DIR__;
if (!file_exists($tmp_folder)) {
mkdir($tmp_folder, 0755, true);
chown($tmp_folder, 'www-data');
chgrp($tmp_folder, 'www-data');
}
if (!check_files_recent(array_map(function($filename) use ($tmp_folder) { return $tmp_folder . '/' . $filename; }, $expected_files))) {
download_and_extract($urls, $tmp_folder);
}
$matching_files = array_filter(scandir($tmp_folder), function($file) use ($tmp_folder) {
return is_file($tmp_folder . '/' . $file) && strpos($file, 'Germany_') === 0;
});
$matching_files = array_map(function($file) use ($tmp_folder) {
return $tmp_folder . '/' . $file;
}, $matching_files);
echo "Matching files:\n";
print_r($matching_files);
if (!empty($matching_files)) {
build_epg('Germany', $tmp_folder, $target_folder, $matching_files);
} else {
echo "No matching files found in the tmp folder.\n";
}
exit(0);