\n";
$stationsRFID = array();
$stationDisplayName = array();
foreach ($stations as $station) {
$stationId = (string) $station['id'];
$stationId = $newStationId[$stationId];
$stationsRFID[$stationId] = 0;
foreach ($station->{'display-name'} as $displayName) {
if (ereg("^[0-9]+$", $displayName)) {
$stationsRFID[$stationId] = (int) $displayName;
break;
}
}
// Didn't find a rfid, try to get it from the ID, or fake it if it can't find any.
if ($stationsRFID[$stationId] == 0) {
if (ereg("^([0-9]+)$", (string) $station['id'], $regs)) {
$stationsRFID[$stationId] = (int) $regs[1];
}
else if (ereg("^([0-9]+)\..*", (string) $station['id'], $regs)) {
$stationsRFID[$stationId] = (int) $regs[1];
} else {
$stationsRFID[$stationId] = $stationId;
}
}
$stationDisplayName[$stationsRFID[$stationId]] = $station->{'display-name'}[0];
}
if (isset($config['channels_merge'])) {
foreach ($config['channels_merge'] as $channels_merge) {
$stationId = $newStationId[$channels_merge['target']['id']];
$stationsRFID[$stationId] = $channels_merge['target']['id'];
$stationDisplayName[$stationsRFID[$stationId]] = $channels_merge['target']['name'];
}
}
if (file_exists("$providerId.channels")) {
$fp = fopen("$providerId.channels", "r")
or die("Can't open $providerId.channels file for reading.");
while (!feof($fp)) {
$line = ereg_replace("[\r\n]*","",fgets($fp, 1024));
if (ereg("^([0-9]+)\t(.*)$", $line, $regs) !== false) {
$rfId = $regs[1];
$name = $regs[2];
$stationDisplayName[$rfId] = $name;
}
}
fclose($fp);
}
foreach ($newStationId as $stationId) {
$resultXML .= "
".$stationId."
".$stationDisplayName[$stationsRFID[$stationId]]."
".$stationsRFID[$stationId]."
";
}
$resultXML .= " \n";
return $resultXML;
}
function getProgramHash($program) {
$start = (string) $program['start'];
$end = (string) $program['stop'];
$hash = startstopToRuntime($start, $end) . $program->title;
if (isset($program->{'sub-title'})) {
$hash .= $program->{'sub-title'};
}
if (isset($program->{'episode-num'})) {
if (is_array($program->{'episode-num'})) {
$hash .= $program->{'episode-num'}[0];
} else {
$hash .= $program->{'episode-num'};
}
}
if (isset($program->desc)) {
$hash .= $program->desc;
}
if (isset($program->category)) {
if (is_array($program->category)) {
$hash .= $program->category[0];
} else {
$hash .= $program->category;
}
}
return md5($hash);
}
function startstopToRuntime($start, $stop) {
$timeS = strtotime($start);
$timeE = strtotime($stop);
return ($timeE - $timeS) / 60;
}
function isProgramSeries($program) {
if (isset($program->{'sub-title'})) {
return true;
}
if (!isset($program->category)) {
return false;
}
foreach ($program->category as $cat) {
if ($cat == 'Series') {
return true;
}
}
return false;
}
function getProgramsXMLTV($programs) {
global $newProgramId, $numPrograms;
$newProgramId = array();
$resultXML = "";
$resultXML .= " \n";
$i = 1;
foreach ($programs as $program) {
$hash = getProgramHash($program);
// Is this an existing program ?
if (isset($newProgramId[$hash])) {
continue;
}
$newProgramId[$hash] = $i;
$start = (string) $program['start'];
$end = (string) $program['stop'];
$resultXML .= "
".$i."
".startstopToRuntime($start, $end)."
".xmlize($program->title)."\n";
// Serie
if (isProgramSeries($program)) {
if (isset($program->{'sub-title'})) {
$resultXML .= " ".xmlize($program->{'sub-title'})."\n";
}
if (isset($program->{'episode-num'})) {
foreach ($program->{'episode-num'} as $num) {
$resultXML .= " ".$num."\n";
break;
}
}
$resultXML .= " Y\n";
} else {
$resultXML .= " N\n";
}
if (isset($program->desc)) {
$resultXML .= " ".xmlize($program->desc)."\n";
} else {
$resultXML .= " \n";
}
if (isset($program->category)) {
foreach ($program->category as $cat) {
if ($cat != 'Series') {
$resultXML .= " ".xmlize($cat)."\n";
break;
}
}
} else {
$resultXML .= " \n";
}
$resultXML .= " Other\n";
$resultXML .= " \n";
$i++;
}
$resultXML .= " \n";
$numPrograms = $i;
return $resultXML;
}
function getSchedulesXMLTV($programs) {
global $newProgramId, $newStationId, $config;
$resultXML = "";
$resultXML .= " \n";
foreach ($programs as $program) {
$hash = getProgramHash($program);
$channel = (string) $program['channel'];
$start = (string) $program['start'];
$startTime = convertTimeToBaseTime($start, $channel);
$end = (string) $program['stop'];
$stopTime = convertTimeToBaseTime($end, $channel);
$skip = FALSE;
if (isset($config['channels_merge'])) {
$merged = FALSE;
foreach ($config['channels_merge'] as $channels_merge) {
foreach ($channels_merge['source'] as $source) {
if (shouldMerge($channel, $start, $end, $source)) {
$resultXML .= getScheduleXML($hash, $channels_merge['target']['id'], convertTimeToBaseTime($start, $channels_merge['target']['id']), convertTimeToBaseTime($end, $channels_merge['target']['id']), $program);
$merged = TRUE;
break;
}
}
if (!$merged && $channel == $channels_merge['target']['id']) {
$skip = TRUE;
}
}
}
if (!$skip) {
$resultXML .= getScheduleXML($hash, $channel, $startTime, $stopTime, $program);
}
}
$resultXML .= " \n";
return $resultXML;
}
function shouldMerge($channel, $p_start, &$p_end, $source) {
if ($channel != $source['id']) {
return FALSE;
}
$start = strtotime($p_start);
$hour = sprintf('%04d', str_ireplace(array('h',':'), '', $source['from']));
$from = date('Ymd'.$hour.'00 O', $start);
$hour = sprintf('%04d', str_ireplace(array('h',':'), '', $source['to']));
if ($hour == '0000') {
$to = date('Ym'.(date('d', $start)+1).$hour.'00 O', $start);
} else {
$to = date('Ymd'.$hour.'00 O', $start);
}
if ($start >= strtotime($from) && $start < strtotime($to)) {
$end = strtotime($p_end);
if ($end > strtotime($to)) {
$p_end = $to;
}
return TRUE;
}
return FALSE;
}
function getScheduleXML($hash, $channel, $startTime, $stopTime, $program) {
global $newProgramId, $newStationId;
$resultXML = "
".$newProgramId[$hash]."
".$newStationId[$channel]."
".$startTime."
".$stopTime."\n";
if (isset($program->hd)) {
$resultXML .= " Y\n";
} else {
$resultXML .= " N\n";
}
if (isset($program->audio) && isset($program->audio->stereo)) {
$resultXML .= " Y\n";
} else {
$resultXML .= " N\n";
}
if (isset($program->subtitles) && (string) $program->subtitles['type'] == 'teletext') {
$resultXML .= " Y\n";
} else {
$resultXML .= " N\n";
}
$resultXML .= " \n";
return $resultXML;
}
// Returns: Response object that contains all EPG data.
function getEPGDataXMLTV($providerId) {
global $baseTime, $usingCache, $daysRequested, $config;
$response = simplexml_load_file("cache/xmltv-$providerId.xml");
$usingCache = false;
return $response;
}
// Generates the complete EPG data as a XML file (in TitanTV format)
function parseEPGDataXMLTV($providerId) {
global $stationsXML, $programsXML, $schedulesXML, $baseTime;
global $config, $numStations, $numPrograms, $numSchedules;
$response = getEPGDataXMLTV($providerId);
$stations = $response->channel;
$stationsXML = getStationsXMLTV($stations, $providerId);
$numStations = sizeof($stations);
$programs = $response->programme;
$programsXML = getProgramsXMLTV($programs);
$schedulesXML = getSchedulesXMLTV($programs);
$numSchedules = sizeof($programs);
}
?>