/** * Парсер программы * * @param string $path * @param string $path_announce * @return boolean */ function parse_channel($path, $path_announce) { global $ChannelGmtDiff, $message, $verbose; $EpgText = ""; //"C S13.0E-318-9400-8208\r\n"; $prevEpgEvent = ""; $prevTimeStamp = 0; $prevShortDesc = ""; $prevDesc = ""; $day = 0; $month = 0; $date = getdate(); $year = $date['year']; $hour = 0; $min = 0; $eventText = ""; $result = ''; if (!($fchannel = file_get_contents($path))) { $message .= "Can't open input epg file {$path}" . PHP_EOL; return 0; } else { if (!file_exists($path_announce)) { $message .= "Can't open input announce file {$path_announce}" . PHP_EOL; $announce = false; } else { $message .= "Parse announces" . PHP_EOL; $announce = parse_announce($path_announce); $message .= "Found " . count($announce) . " announces" . PHP_EOL; //var_dump($announce); } } $channel_days = preg_split('%</pre>%', $fchannel); unset($channel_days[0]); unset($channel_days[count($channel_days)]); foreach ($channel_days as $channel_day) { $channel_events = preg_split('/(\\d{1,2}:\\d{1,2}\\s+.+)\\n/u', $channel_day, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); foreach ($channel_events as $channel_event) { if (preg_match("/<P><FONT\\sSIZE=\\+2><B>(Понедельник|Вторник|Среда|Четверг|Пятница|Суббота|Воскресенье)\\. ([0-3]\\d) (\\S+)/u", $channel_event, $result)) { //found day $month = substr($result[3], 0, -1); $day = $result[2]; $hour = 0; $min = 0; if ($month == "Января") { $month = 1; } if ($month == "Февраля") { $month = 2; } if ($month == "Марта") { $month = 3; } if ($month == "Апреля") { $month = 4; } if ($month == "Мая") { $month = 5; } if ($month == "Июня") { $month = 6; } if ($month == "Июля") { $month = 7; } if ($month == "Августа") { $month = 8; } if ($month == "Сентября") { $month = 9; } if ($month == "Октября") { $month = 10; } if ($month == "Ноября") { $month = 11; } if ($month == "Декабря") { $month = 12; } //print("$day-$month-$year $hour:$min - $eventText<br/>"); } elseif (preg_match("/(\\d{1,2}):(\\d{1,2})\\s+(.+)/u", $channel_event, $result)) { // found event if ($result[1] < $hour) { // found overlaped time $day++; #try next day if (!checkdate($month, $day, $year)) { //after day increase date is not valid $day = 1; $month++; //try next month if (!checkdate($month, $day, $year)) { //after month increase date is not valid $day = 1; $month = 1; //happy new year !!! $year++; } } } $hour = $result[1]; $min = $result[2]; $unixtime = mktime($hour, $min, 00, $month, $day, $year) + $ChannelGmtDiff; $Desc = $announce[$unixtime]['desc']; $ShortDesc = $announce[$unixtime]['shortdesc']; $event_times = preg_split('/,\\s(\\d{1,2})\\.(\\d{1,2})/', trim($result[3]), -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); if (count($event_times) == 1) { $result[3] = $event_times[0]; $eventText = trim($result[3]); $channels[$unixtime]['T'] = $eventText; $channels[$unixtime]['S'] = $ShortDesc; $channels[$unixtime]['D'] = $Desc; //print (date ( 'd-m-Y G:i', $unixtime )."- $eventText<br/>"); } else { //еще время передач в той же строке $eventText = preg_replace('%(?:\\.)?(?:</pre>)?(\\r|\\z|\\n)*%', '', $event_times[count($event_times) - 1]); $channels[$unixtime]['T'] = trim($eventText); $channels[$unixtime]['S'] = $ShortDesc; $channels[$unixtime]['D'] = $Desc; //print("$day-$month-$year $hour:$min - $eventText<br/>"); for ($i = 0; $i < count($event_times) - 1; $i += 2) { $day2 = $day; $month2 = $month; $year2 = $year; if ($event_times[$i] < $hour) { // found overlaped time $day2++; #try next day if (!checkdate($month, $day2, $year)) { //after day increase date is not valid $day2 = 1; $month2++; //try next month if (!checkdate($month2, $day2, $year)) { //after month increase date is not valid $day2 = 1; $month2 = 1; //happy new year !!! $year2++; } } $hour = $event_times[$i]; $min = $event_times[$i + 1]; //print("$day2-$month2-$year2 $hour:$min - $eventText<br/>"); } $unixtime = mktime($hour, $min, 00, $month2, $day2, $year2) + $ChannelGmtDiff; $channels[$unixtime]['T'] = trim($eventText); $channels[$unixtime]['S'] = $ShortDesc; $channels[$unixtime]['D'] = $Desc; //print (date ( 'd-m-Y G:i', $unixtime )."- $eventText<br/>"); } } } } } //end foreach //create $EpgText ksort($channels); foreach ($channels as $timestamp => $event) { if ($prevTimeStamp != 0) { $duration = $timestamp - $prevTimeStamp; //$EpgText .= "E 1 " . date ( 'd-m-Y G:i', $prevTimeStamp ) . " $duration 0\r\n"; //////// $EpgText .= "E 1 " . $prevTimeStamp . " {$duration} 0\r\n"; $EpgText .= $prevEpgEvent; if ($prevShortDesc != "") { $EpgText .= "S {$prevShortDesc}\r\n"; } if ($prevDesc != "") { $EpgText .= "D {$prevDesc}\r\n"; } $EpgText .= "e\r\n"; } $prevTimeStamp = $timestamp; $prevEpgEvent = "T {$event['T']}\r\n"; $prevDesc = $event['D']; $prevShortDesc = $event['S']; } if ($prevTimeStamp != 0) { #last event, duration is not known, use default 60 min :-( $duration = 3600; //$EpgText .= "E 1 " . date ( 'd-m-Y G:i', $prevTimeStamp ) . " $duration 0\r\n"; //////// $EpgText .= "E 1 " . $prevTimeStamp . " {$duration} 0\r\n"; $EpgText .= $prevEpgEvent; if ($prevShortDesc != "") { $EpgText .= "S {$prevShortDesc}\r\n"; } if ($prevDesc != "") { $EpgText .= "D {$prevDesc}\r\n"; } $EpgText .= "e\r\n"; } if ($verbose > 0) { return 1; } print $EpgText; }
function parse_channel($path, $path_announce) { $EpgText = ""; //"C S13.0E-318-9400-8208\r\n"; $prevEpgEvent = ""; $prevTimeStamp = 0; $prevShortDesc = ""; $prevDesc = ""; $day = 0; $month = 0; $date = getdate(); $year = $date['year']; $hour = 0; $min = 0; $eventText; if (!($fchannel = fopen($path, 'r'))) { echo "Can't open input epg file {$path}" . PHP_EOL; return 0; } else { if (!file_exists($path_announce)) { echo "Can't open input announce file {$path_announce}" . PHP_EOL; $announce = false; } else { $announce = parse_announce($path_announce); } } //parse channel while ($line = fgets($fchannel)) { if (preg_match("/(\\d{1,2}):(\\d{1,2})\\s+(.+)/u", $line, $result) == 1) { //found program + time if ($result[1] < $hour) { // found overlaped time $day++; #try next day if (!checkdate($month, $day, $year)) { //after day increase date is not valid $day = 1; $month++; //try next month if (!checkdate($month, $day, $year)) { //after month increase date is not valid $day = 1; $month = 1; //happy new year !!! $year++; } } } $hour = $result[1]; $min = $result[2]; $eventText = preg_replace('/\\.\\z/', '', $result[3]); $eventText = preg_replace('/\\A"/', '', $eventText); $eventText = preg_replace('/"\\z/', '', $eventText); //print "#" . $eventText . " # $hour:$min - $day,$month,$year<br>"; $unixtime = mktime($hour, $min, 00, $month - 1, $day, $year) + $siteChannelDiff + $gmtDiff; if ($prevTimeStamp != 0) { #try to calculate correct duration $duration = $unixtime - $prevTimeStamp; $EpgText .= "E 1 {$prevTimeStamp} {$duration} 0\r\n"; $EpgText .= $prevEpgEvent; if ($prevShortDesc != "") { $EpgText .= "S {$prevShortDesc}\r\n"; } if ($prevDesc != "") { $EpgText .= "D {$prevDesc}\r\n"; } $EpgText .= "e\r\n"; } $prevTimeStamp = $unixtime; $prevEpgEvent = "T {$eventText}\r\n"; $prevShortDesc = $eventExtraType; $prevDesc = ""; } elseif (preg_match("/<\\/pre>.*(Понедельник|Вторник|Среда|Четверг|Пятница|Суббота|Воскресенье)\\. ([0-3]\\d) (\\S+)/u", $line, $result) == 1) { //echo 'found day.month'.$line. PHP_EOL; //echo mb_strtolower($result[3]); $month = substr($result[3], 0, -1); $day = $result[2]; $hour = 0; $min = 0; if ($month == "Января") { $month = 1; } if ($month == "Февраля") { $month = 2; } if ($month == "Марта") { $month = 3; } if ($month == "Апреля") { $month = 4; } if ($month == "Мая") { $month = 5; } if ($month == "Июня") { $month = 6; } if ($month == "Июля") { $month = 7; } if ($month == "Августа") { $month = 8; } if ($month == "Сентября") { $month = 9; } if ($month == "Октября") { $month = 10; } if ($month == "Ноября") { $month = 11; } if ($month == "Декабря") { $month = 12; } } else { //echo 'can not parse line'. PHP_EOL; } } fclose($fchannel); if ($prevTimeStamp != 0) { #last event, duration is not known, use default 60 min :-( $duration = 3600; $EpgText .= "E 1 {$prevTimeStamp} {$duration} 0\r\n"; $EpgText .= $prevEpgEvent; if ($prevShortDesc != "") { $EpgText .= "S {$prevShortDesc}\r\n"; } if ($prevDesc != "") { $EpgText .= "D {$prevDesc}\r\n"; } $EpgText .= "e\r\n"; } print $EpgText; return 1; }