Skip to content

Commit

Permalink
add guides_to_generate setting + fix Telecablesat
Browse files Browse the repository at this point in the history
  • Loading branch information
racacax committed Dec 29, 2021
1 parent fe72fd9 commit dbc17c8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 70 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ Le fichier config.json est au format JSON.
"delete_raw_xml": false, // Supprimer le XML brut après génération (true|false)
"enable_gz": false, // Activer la compression gz (true|false)
"enable_zip": true // Activer la compression zip (true|false),
"enable_dummy" => false, // Afficher un EPG mire en cas d'absence de guide pour une chaine
"custom_priority_orders" => {"Telerama": 0.2, "UltraNature": 0.5} // Modifier l'ordre de priorité pour certains services globalement
"enable_dummy" : false, // Afficher un EPG mire en cas d'absence de guide pour une chaine
"custom_priority_orders" : {"Telerama": 0.2, "UltraNature": 0.5}, // Modifier l'ordre de priorité pour certains services globalement
"guides_to_generate" : [{"channels": "./channels.json", "filename": "xmltv.xml"}] // liste des XML à générer. Pour chaque élément, `channels` indique le fichier des chaines et `filename` le nom du fichier de sortie
}
```

Expand Down
18 changes: 9 additions & 9 deletions channels_per_provider/channels_teleloisirs.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,21 +266,21 @@
"TF1plus1.fr": "programme-tf1plus1-492.html",
"TMCplus1.fr": "programme-tmcplus1-491.html",
"LuckyJack.fr": "programme-lucky-jack-473.html",
"TELEFOOTSTADIUM1.fr": "programme-telefoot-stadium-1-495.html",
"TelefootStadium1.fr": "programme-telefoot-stadium-1-495.html",
"Museum.fr": "programme-museum-tv-474.html",
"TELEFOOTSTADIUM2.fr": "programme-telefoot-stadium-2-497.html",
"TelefootStadium2.fr": "programme-telefoot-stadium-2-497.html",
"BBCWorldNews.uk": "programme-bbc-world-news-485.html",
"TELEFOOTSTADIUM3.fr": "programme-telefoot-stadium-3-499.html",
"TelefootStadium3.fr": "programme-telefoot-stadium-3-499.html",
"SRF1.ch": "programme-srf-1-478.html",
"TELEFOOTSTADIUM4.fr": "programme-telefoot-stadium-4-494.html",
"TelefootStadium4.fr": "programme-telefoot-stadium-4-494.html",
"SRF2.ch": "programme-srf-2-477.html",
"TELEFOOTSTADIUM5.fr": "programme-telefoot-stadium-5-498.html",
"TelefootStadium5.fr": "programme-telefoot-stadium-5-498.html",
"RSI1.ch": "programme-rsi-la-1-482.html",
"TELEFOOTSTADIUM6.fr": "programme-telefoot-stadium-6-493.html",
"TelefootStadium6.fr": "programme-telefoot-stadium-6-493.html",
"RSI2.ch": "programme-rsi-la-2-481.html",
"TELEFOOTSTADIUM7.fr": "programme-telefoot-stadium-7-496.html",
"TelefootStadium7.fr": "programme-telefoot-stadium-7-496.html",
"Viceland.fr": "programme-vice-tv-486.html",
"TELEFOOTSTADIUM8.fr": "programme-telefoot-stadium-8-500.html",
"TelefootStadium8.fr": "programme-telefoot-stadium-8-500.html",
"DiscoveryInvestigation.fr": "programme-discovery-investigation-488.html",
"UltraNature.fr": "programme-ultra-nature-501.html",
"ES1.fr": "programme-es1-483.html",
Expand All @@ -290,7 +290,7 @@
"SportEnFrance.fr": "programme-sport-en-france-476.html",
"RTFrance.fr": "programme-rt-france-480.html",
"OlympiaTV.fr": "programme-olympia-tv-479.html",
"TELEFOOT.fr": "programme-telefoot-490.html",
"Telefoot.fr": "programme-telefoot-490.html",
"SAT1.de": "programme-sat-1-3512.html",
"WDR.de": "programme-wdr-3511.html",
"TeleTOONplus1.fr": "programme-teletoonplus1-3519.html",
Expand Down
10 changes: 10 additions & 0 deletions classes/Telecablesat.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Telecablesat extends AbstractProvider implements Provider

private static $cache = []; // multiple channels are on the same page
private static $BASE_URL = "https://tv-programme.telecablesat.fr";
private $loopCounter = 0;
public function __construct()
{
parent::__construct("channels_per_provider/channels_telecablesat.json", 0.55);
Expand All @@ -24,8 +25,17 @@ function constructEPG($channel, $date)
$channel_url = "https://tv-programme.telecablesat.fr/programmes-tele/?date=$date&page=$page";
if(!isset(self::$cache[md5($channel_url)])) {
$res1 = $this->getContentFromURL($channel_url);
if(empty($res1)) {
$this->loopCounter++;
if($this->loopCounter > 3)
return false;
displayTextOnCurrentLine(" \e[31mRate limited, waiting 30s ($this->loopCounter)\e[39m");
sleep(30);
return $this->constructEPG($channel, $date);
}
self::$cache[md5($channel_url)] = $res1;
}
$this->loopCounter = 0;
$content = self::$cache[md5($channel_url)];
preg_match_all('/logos_chaines\/(.*?).png" title="(.*?)"/', $content, $channels);
$channel_index = array_search($channel_id, $channels[1]);
Expand Down
96 changes: 54 additions & 42 deletions classes/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class MainProgram {
public static $customTextDisplayed = false;
public static $currentOutput = null;
public static $dummyEPG = "";
public static $filesQueue = [];
}
function echoSilent($string) {
if(MainProgram::$customTextDisplayed) {
Expand All @@ -23,59 +24,61 @@ function generateFilePath($channel,$date)
return XML_PATH.$channel."_".$date.".xml";
}

function reformatXML() {
echoSilent("\e[34m[EXPORT] \e[39mReformatage du XML...\n");
function reformatXML($file) {
echoSilent("\e[34m[EXPORT] \e[39mReformatage du XML... ($file)\n");
$domxml = new DOMDocument('1.0');
$domxml->preserveWhiteSpace = false;
$domxml->formatOutput = true;
/* @var $xml SimpleXMLElement */
$domxml->loadXML(file_get_contents(CONFIG['output_path']."/xmltv.xml"));
$domxml->save(CONFIG['output_path']."/xmltv.xml");
$domxml->loadXML(file_get_contents(CONFIG['output_path']."/$file"));
$domxml->save(CONFIG['output_path']."/$file");
}
function validateXML() {
function validateXML($file) {
echoSilent("\e[34m[EXPORT] \e[39mValidation du fichier XML...\n");
libxml_use_internal_errors(true);
$xml = @simplexml_load_file(CONFIG['output_path'] . '/xmltv.xml');
$xml = @simplexml_load_file(CONFIG['output_path'] . '/'.$file);
if($xml === false) {
echoSilent("\e[34m[EXPORT] \e[31mXML non valide\e[39m\n");
echoSilent("\e[34m[EXPORT] \e[31mXML non valide\e[39m ($file)\n");
foreach (libxml_get_errors() as $error) {
echo "\t", $error->message;
}
libxml_clear_errors();
return false;
} else {
echoSilent("\e[34m[EXPORT] \e[32mXML valide\e[39m\n");
echoSilent("\e[34m[EXPORT] \e[32mXML valide\e[39m ($file)\n");
return true;
}
}

function gzCompressXML() {
echoSilent("\e[34m[EXPORT] \e[39mCompression du XMLTV en GZ...\n");
$got = file_get_contents(CONFIG['output_path'].'/xmltv.xml');
function gzCompressXML($file) {
echoSilent("\e[34m[EXPORT] \e[39mCompression du XMLTV en GZ... ($file=\n");
$got = file_get_contents(CONFIG['output_path']."/$file");
$got1 = gzencode($got,true);
file_put_contents(CONFIG['output_path'].'/xmltv.xml.gz',$got1);
echoSilent("\e[34m[EXPORT] \e[39mGZ : \e[32mOK\e[39m\n");
file_put_contents(CONFIG['output_path']."/$file.gz",$got1);
echoSilent("\e[34m[EXPORT] \e[39mGZ : \e[32mOK\e[39m ($file)\n");
}

function zipCompressXML() {
echoSilent("\e[34m[EXPORT] \e[39mCompression du XMLTV en ZIP...\n");
function zipCompressXML($file) {
echoSilent("\e[34m[EXPORT] \e[39mCompression du XMLTV en ZIP... ($file)\n");
$zip = new ZipArchive();
$filename = CONFIG['output_path']."/xmltv.zip";
$filenameSplited = explode('.', $file)[0];
$filename = CONFIG['output_path']."/$filenameSplited.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
echoSilent("\e[34m[EXPORT] \e[39mZIP : \e[31mHS\e[39m\n");
echoSilent("\e[34m[EXPORT] \e[39mZIP : \e[31mHS\e[39m ($file)\n");
} else {
echoSilent("\e[34m[EXPORT] \e[39mZIP : \e[32mOK\e[39m\n");
echoSilent("\e[34m[EXPORT] \e[39mZIP : \e[32mOK\e[39m ($file)\n");
}
$zip->addFile(CONFIG['output_path']."/xmltv.xml", "xmltv.xml");
$zip->addFile(CONFIG['output_path']."/$file", "xmltv.xml");
$zip->close();

}

function getChannelsEPG($classes_priotity) {
echoSilent("\e[95m[EPG GRAB] \e[39mRécupération du guide des programmes\n");
function getChannelsEPG($classes_priotity, $file) {
echoSilent("\e[95m[EPG GRAB] \e[39mRécupération du guide des programmes ($file)\n");
$logs = array('channels'=>array(), 'xml'=>array(),'failed_providers'=>array());
$channels = json_decode(file_get_contents('channels.json'),true);
$channels = json_decode(file_get_contents($file),true);
MainProgram::$filesQueue = [];
$channelsKeys = array_keys($channels);
foreach($channelsKeys as $channel)
{
Expand Down Expand Up @@ -105,6 +108,7 @@ function getChannelsEPG($classes_priotity) {
$logs["channels"][$date][$channel]['provider'] = $classe;
$logs["channels"][$date][$channel]['cache'] = false;
date_default_timezone_set($old_zone);
MainProgram::$filesQueue[] = $file;
break;
} else {
date_default_timezone_set($old_zone);
Expand All @@ -123,6 +127,7 @@ function getChannelsEPG($classes_priotity) {
echoSilent(" | \e[33mOK \e[39m- $provider (Cache)".chr(10));
$logs["channels"][$date][$channel]['success'] = true;
$logs["channels"][$date][$channel]['cache'] = true;
MainProgram::$filesQueue[] = $file;

}
}
Expand All @@ -147,12 +152,13 @@ function getProviderFromComment($file) {
return @trim(explode('-->', explode('<!--', file_get_contents($file))[1])[0]);
}

function moveOldXML() {
function moveOldXML($xmlFile) {
$splitedFile = explode('.', $xmlFile)[0];
foreach(["xml","zip","xml.gz"] as $ext) {

if(file_exists(CONFIG['output_path']."/xmltv.$ext"))
if(file_exists(CONFIG['output_path']."/$splitedFile.$ext"))
{
rename(CONFIG['output_path']."/xmltv.$ext",CONFIG['output_path']."/xmltv_".date('Y-m-d_H-i-s',filemtime(CONFIG['output_path']."/xmltv.$ext")).".$ext");
rename(CONFIG['output_path']."/$splitedFile.$ext",CONFIG['output_path']."/{$splitedFile}_".date('Y-m-d_H-i-s',filemtime(CONFIG['output_path']."/$splitedFile.$ext")).".$ext");
}
}
}
Expand All @@ -170,12 +176,12 @@ function getDefaultChannelsInfos() {

}

function generateXML() {
function generateXML($channelsFile, $xmlFile) {

echoSilent("\e[34m[EXPORT] \e[39mGénération du XML...\n");
$channels = json_decode(file_get_contents('channels.json'),true);
echoSilent("\e[34m[EXPORT] \e[39mGénération du XML... ($xmlFile)\n");
$channels = json_decode(file_get_contents($channelsFile),true);
$defaultChannelsInfos = getDefaultChannelsInfos();
$filepath = CONFIG['output_path']."/xmltv.xml";
$filepath = CONFIG['output_path']."/$xmlFile";
$out = fopen($filepath, "w");
fwrite($out,'<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
Expand All @@ -197,7 +203,7 @@ function generateXML() {
'.(!(empty($icon)) ? '<icon src="'.stringAsXML($icon).'" />' : '').'
</channel>'.chr(10));
}
$files = glob(XML_PATH.'*');
$files = MainProgram::$filesQueue;
foreach($files as $file){
$in = fopen($file, "r");
while ($line = fgets($in)){
Expand All @@ -209,7 +215,7 @@ function generateXML() {
fwrite($out,'</tv>');
fclose($out);

echoSilent("\e[34m[EXPORT] \e[39mGénération du XML terminée\n");
echoSilent("\e[34m[EXPORT] \e[39mGénération du XML terminée ($xmlFile)\n");
}

function loadConfig() {
Expand All @@ -224,7 +230,8 @@ function loadConfig() {
"enable_zip" => true, # enable zip compression for the XMLTV,
"xml_cache_days" => 5, # How many days old XML are stored
"enable_dummy" => false, # Add a dummy EPG if channel not found
"custom_priority_orders" => [] # Add a custom priority order for a provider globally
"custom_priority_orders" => [], # Add a custom priority order for a provider globally,
"guides_to_generate" => [array("channels"=>"./channels.json", "filename"=>"xmltv.xml")] # list of xmltv to generate
);


Expand All @@ -238,6 +245,9 @@ function loadConfig() {
$json = json_decode(file_get_contents('config.json'),true);
foreach ($json as $key => $value) {
$CONFIG[$key] = $value;
if(is_array($value)) {
$value = json_encode($value);
}
echoSilent("\e[95m($key) \e[39m=> \e[33m$value\e[39m, ");
}
define('CONFIG', $CONFIG);
Expand All @@ -248,18 +258,20 @@ function loadConfig() {
echoSilent("\n");
}

function getClasses() {
function compare_classe($a,$b)

function compare_classe($a,$b)
{
if(class_exists($a) && class_exists($b))
{
if(class_exists($a) && class_exists($b))
{
if(call_user_func($a. "::getPriority") > call_user_func($b. "::getPriority"))
return -1;
return 1;
} else {
return 0;
}
if(call_user_func($a. "::getPriority") > call_user_func($b. "::getPriority"))
return -1;
return 1;
} else {
return 0;
}
}

function getClasses() {
$classes = glob('classes/*.php');
$classes_priotity = array();
echoSilent("\e[36m[CHARGEMENT] \e[39mOrganisation des classes de Provider \n");
Expand Down
3 changes: 2 additions & 1 deletion config_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
"enable_zip": true,
"xml_cache_days": 5,
"enable_dummy": false,
"custom_priority_orders": {}
"custom_priority_orders": {},
"guides_to_generate": [{"channels": "./channels.json", "filename": "xmltv.xml"}]
}
36 changes: 20 additions & 16 deletions script_all.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,35 @@
copy('channels_example.json', 'channels.json');
}
}
foreach(CONFIG['guides_to_generate'] as $guide) {
$xmlFile = $guide["filename"];
$channelsFile = $guide['channels'];
getChannelsEPG(getClasses(), $channelsFile);

getChannelsEPG(getClasses());
clearOldXML();

clearOldXML();
moveOldXML($xmlFile);

moveOldXML();
clearXMLCache();

clearXMLCache();
generateXML($channelsFile, $xmlFile);

generateXML();
if (validateXML($xmlFile)) {
reformatXML($xmlFile);

if(validateXML()) {
reformatXML();
if (CONFIG["enable_gz"]) {
gzCompressXML($xmlFile);
}

if (CONFIG["enable_gz"]) {
gzCompressXML();
}
if (CONFIG["enable_zip"]) {
zipCompressXML($xmlFile);
}

if (CONFIG["enable_zip"]) {
zipCompressXML();
}
if (CONFIG["delete_raw_xml"]) {
echo "\e[34m[EXPORT] \e[39mSuppression du fichier XML brut ($xmlFile)\n";
unlink(CONFIG["output_path"] . "/$xmlFile");
}

if (CONFIG["delete_raw_xml"]) {
echo "\e[34m[EXPORT] \e[39mSuppression du fichier XML brut\n";
unlink(CONFIG["output_path"] . "/xmltv.xml");
}
}

0 comments on commit dbc17c8

Please sign in to comment.