diff --git a/README.md b/README.md
index 6b1763e..1445130 100644
--- a/README.md
+++ b/README.md
@@ -1,17 +1,23 @@
# EPG2XML
-이 프로그램은 EPG(Electronic Program Guide)를 웹상의 여러 소스에서 가져와서 XML로 출력하는 프로그램으로 python2 및 php5 Cli에서 사용 가능하도록 제작되었다.
-기본적으로 외부의 소스를 분석하여 출력하므로 외부 소스 사이트가 변경되거나 삭제되면 문제가 발생할 수 있다.
+이 프로그램은 EPG(Electronic Program Guide)를 웹상의 여러 소스에서 가져와서 XML로 출력하는 프로그램으로 python2.7 및 php5.6.3 이상에서 사용 가능하도록 제작되었다.
+
+python 3과 php 5.6.3 이하에서는 정상적인 작동을 보장하지 못한다.
+
+또한 외부의 소스를 분석하여 EPG 정보를 가공하여 보여주는 것이므로 외부 소스 사이트가 변경되거나 삭제되면 문제가 발생할 수 있다.
## 필요 모듈
-### EPG2XML.PY
+### epg2xml.py
BeautifulSoup(bs4), lxml, requests 모듈이 추가로 필요하다.
설치 OS별로 모듈을 설치하기 위한 사전 설치 방법이 다를 수도 있으므로 검색해서 설치하도록 한다.
synology의 경우 파이썬 모듈을 설치하면 easy_install beautifulsoup, easy_install lxml, easy_install requests 로 추가할 수 있다.
-### EPG2XML.php
-json, dom, mbstring, openssl 모듈이 필요하다.
+### epg2xml.php
+json, dom, mbstring, openssl, curl 모듈이 필요하다. 일반적으로 PHP가 설치되어 있다면 대부분 설치되어 있는 모듈이나 설치되어 있지 않을 경우 추가로 설치해야 한다.
+
+### epg2xml-web.php
+epg2xml.php와 동일
## 설정방법
epg2xml.json 안의 항목이 설정 가능한 항목이다.
@@ -32,6 +38,7 @@ Channel.json 파일을 텍스트 편집기로 열어보면 각채널별 정보
삭제된 채널등으로 인해서 오류 발생시에도 Enabled:0으로 변경하면 오류 발생을 차단할 수 있다.
## 옵션 소개
+### epg2xml.py, epg2xml.php 옵션
실행시 사용가능한 인수는 --help 명령어로 확인이 가능하다
epg2xml.json의 설정을 옵션의 인수를 이용하여 변경할 수 있다.
@@ -48,10 +55,15 @@ epg2xml.json의 설정을 옵션의 인수를 이용하여 변경할 수 있다.
--verbose : EPG 정보 상세하게 표기 ex) --verbose y
+### epg2xml-web.php 옵션
+실행시 사용가능한 인수는 epg2xml.php?help 명령어로 확인이 가능하다
+epg2xml.json의 설정을 옵션의 인수를 이용하여 변경할 수 있다.
+ex : http://domain/epg2xml.php?i=ALL&l=2
+
## 사용방법
-### tv_grab_file 사용시 (https://github.com/neo365/tvheadend-for-Korea)
-epg2xml tv_grab_file 안의 cat xmltv.xml 또는 wget 부분을 아래와 같이 변경해준다.
+### tv_grab_file 사용시 (https://github.com/nurtext/tv_grab_file_synology)
+tv_grab_file 안의 cat xmltv.xml 또는 wget 부분을 아래와 같이 변경해준다.
#### PYTHON의 경우
@@ -59,7 +71,7 @@ epg2xml tv_grab_file 안의 cat xmltv.xml 또는 wget 부분을 아래와 같이
/home/hts/epg2xml.py
-#### PHP의 경우
+#### PHP CLI의 경우
/usr/bin/php /home/hts/epg2xml.php 또는
/home/hts/epg2xml.php
@@ -74,12 +86,16 @@ epg2xml tv_grab_file 안의 cat xmltv.xml 또는 wget 부분을 아래와 같이
/home/hts/epg2xml.py
-#### PHP의 경우
+#### PHP CLI의 경우
/usr/bin/php /home/hts/epg2xml.php 또는
/home/hts/epg2xml.php
-
+### PHP WEB의 경우
+
+http://domain/epg2xml.php 또는
+http://domain/epg2xml.php?i=ALL&l=2
+
## 라이센스
BSD
diff --git a/epg2xml-web.php b/epg2xml-web.php
new file mode 100644
index 0000000..45d477f
--- /dev/null
+++ b/epg2xml-web.php
@@ -0,0 +1,1312 @@
+getMessage());
+ exit;
+ }
+ endif;
+ }
+ catch(Exception $e) {
+ printError($e->getMessage());
+ exit;
+ }
+endif;
+
+if($output == "display") :
+ $fp = fopen('php://output', 'w+');
+ if ($fp === False) :
+ printError(DISPLAY_ERROR);
+ exit;
+ else :
+ try {
+ getEpg();
+ fclose($fp);
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+elseif($output == "file") :
+ if($default_xml_file) :
+ $fp = fopen($default_xml_file, 'w+');
+ if ($fp === False) :
+ printError(FIEL_ERROR);
+ exit;
+ else :
+ try {
+ getEpg();
+ fclose($fp);
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ else :
+ printError("epg2xml.json 파일의 default_xml_file항목이 없습니다.");
+ exit;
+ endif;
+elseif($output == "socket") :
+ if($default_xml_socket) :
+ $default_xml_socket = "unix://".$default_xml_socket;
+ $fp = @fsockopen($default_xml_socket, -1, $errno, $errstr, 30);
+ if ($fp === False) :
+ printError(SOCKET_ERROR);
+ exit;
+ else :
+ try {
+ getEpg();
+ fclose($fp);
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ else :
+ printError("epg2xml.json 파일의 default_xml_socket항목이 없습니다.");
+ exit;
+ endif;
+endif;
+
+function getEPG() {
+ $fp = $GLOBALS['fp'];
+ $MyISP = $GLOBALS['MyISP'];
+ $Channelfile = __DIR__."/Channel.json";
+ $IconUrl = "";
+ try {
+ $f = @file_get_contents($Channelfile);
+ if($f === False) :
+ printError("Channel.json.".JSON_FILE_ERROR);
+ exit;
+ else :
+ try {
+ $Channeldatas = json_decode($f, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception("Channel.".JSON_SYNTAX_ERROR);
+ }
+ catch(Exception $e) {
+ printError($e->getMessage());
+ exit;
+ }
+ endif;
+ }
+ catch(Exception $e) {
+ printError($e->getMessage());
+ exit;
+ }
+ header("Content-Type: application/xml; charset=utf-8");
+ fprintf($fp, "\n");
+ fprintf($fp, "\n\n");
+ fprintf($fp, "\n", VERSION);
+
+ foreach ($Channeldatas as $Channeldata) : #Get Channel & Print Channel info
+ if($Channeldata['Enabled'] == 1) :
+ $ChannelId = $Channeldata['Id'];
+ $ChannelName = htmlspecialchars($Channeldata['Name'], ENT_XML1);
+ $ChannelSource = $Channeldata['Source'];
+ $ChannelServiceId = $Channeldata['ServiceId'];
+ $ChannelIconUrl = htmlspecialchars($Channeldata['Icon_url'], ENT_XML1);
+ if($MyISP != "ALL" && $Channeldata[$MyISP.'Ch'] != Null):
+ $ChannelInfos[] = array($ChannelId, $ChannelName, $ChannelSource, $ChannelServiceId);
+ $Channelnumber = $Channeldata[$MyISP.'Ch'];
+ $ChannelISPName = htmlspecialchars($Channeldata[$MyISP." Name"], ENT_XML1);
+ fprintf($fp, " \n", $ChannelId);
+ fprintf($fp, " %s\n", $ChannelName);
+ fprintf($fp, " %s\n", $ChannelISPName);
+ fprintf($fp, " %s\n", $Channelnumber);
+ fprintf($fp, " %s\n", $Channelnumber." ".$ChannelISPName);
+ if($IconUrl) :
+ fprintf($fp, " \n", $IconUrl, $ChannelId);
+ else :
+ fprintf($fp, " \n", $ChannelIconUrl);
+ endif;
+ fprintf($fp, " \n");
+ elseif($MyISP == "ALL"):
+ $ChannelInfos[] = array($ChannelId, $ChannelName, $ChannelSource, $ChannelServiceId);
+ fprintf($fp, " \n", $ChannelId);
+ fprintf($fp, " %s\n", $ChannelName);
+ if($IconUrl) :
+ fprintf($fp, " \n", $IconUrl, $ChannelId);
+ else :
+ fprintf($fp, " \n", $ChannelIconUrl);
+ endif;
+ fprintf($fp, " \n");
+ endif;
+ endif;
+ endforeach;
+ # Print Program Information
+ foreach ($ChannelInfos as $ChannelInfo) :
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ChannelSource = $ChannelInfo[2];
+ $ChannelServiceId = $ChannelInfo[3];
+ if($GLOBALS['debug']) printLog($ChannelName.' 채널 EPG 데이터를 가져오고 있습니다');
+ if($ChannelSource == 'EPG') :
+ GetEPGFromEPG($ChannelInfo);
+ elseif($ChannelSource == 'KT') :
+ GetEPGFromKT($ChannelInfo);
+ elseif($ChannelSource == 'LG') :
+ GetEPGFromLG($ChannelInfo);
+ elseif($ChannelSource == 'SK') :
+ GetEPGFromSK($ChannelInfo);
+ elseif($ChannelSource == 'SKY') :
+ GetEPGFromSKY($ChannelInfo);
+ elseif($ChannelSource == 'NAVER') :
+ GetEPGFromNaver($ChannelInfo);
+ elseif($ChannelSource == 'TBROAD') :
+ GetEPGFromTbroad($ChannelInfo);
+ elseif($ChannelSource == 'ISCS') :
+ GetEPGFromIscs($ChannelInfo);
+ elseif($ChannelSource == 'MBC') :
+ GetEPGFromMbc($ChannelInfo);
+ elseif($ChannelSource == 'MIL'):
+ GetEPGFromMil($ChannelInfo);
+ elseif($ChannelSource == 'IFM'):
+ GetEPGFromIfm($ChannelInfo);
+ elseif($ChannelSource == 'KBS'):
+ GetEPGFromKbs($ChannelInfo);
+ endif;
+ endforeach;
+ fprintf($fp, "\n");
+}
+
+// Get EPG data from epg.co.kr
+function GetEPGFromEPG($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $epginfo = array();
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://www.epg.co.kr/epg-cgi/extern/cnm_guide_type_v070530.cgi";
+ $day = date("Ymd", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'beforegroup' => '100',
+ 'checkchannel' => $ServiceId,
+ 'select_group' => '100',
+ 'start_date' => $day
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ $response = str_replace("charset=euc-kr", "charset=utf-8", $response);
+ $dom = new DomDocument;
+ libxml_use_internal_errors(true);
+ $dom->loadHTML(mb_convert_encoding($response, "UTF-8", "EUC-KR"));
+ $xpath = new DomXPath($dom);
+ for($i = 2; $i < 5; $i++) :
+ $thisday = $day;
+ $query = "//table[contains(@style,'margin-bottom:30')][".$i."]//td[contains(@colspan,'2')]/following::td[1]/table[1]//td[2]";
+ $programs = $xpath->query($query);
+ foreach($programs as $program) :
+ $hour = $xpath->query("parent::*/parent::*/parent::*/parent::*/td[1]", $program)->item(0);
+ $hour = str_replace("시", "", trim($hour->nodeValue));
+ $minute = $xpath->query("preceding-sibling::td[1]", $program)->item(0);
+ $hour = $hour.":".str_replace(array("[", "]"), array("",""), trim($minute->nodeValue));
+ switch ($i) :
+ case 2 :
+ $hour = $hour." AM";
+ break;
+ case 3 :
+ $hour = $hour." PM";
+ break;
+ case 4 :
+ if($hour > 5 && $hour < 12) :
+ $hour = $hour." PM";
+ elseif($hour <5 || $hour == 12) :
+ $hour = $hour." AM";
+ $thisday = date("Ymd", strtotime($day." +1 days"));
+ endif;
+ break;
+ endswitch;
+ $startTime = date("YmdHis", strtotime($thisday." ".$hour));
+ $programName = "";
+ $subprogramName = "";
+ $rating = 0;
+ $episode = "";
+ $rebroadcast = False;
+ preg_match('/?(.*)?(.*?)\s*(<(.*)>)?\s*(\(재\))?\s*(\(([\d,]+)회\))?()?\s*<\/td>/', trim($dom->saveHTML($program)), $matches);
+ if ($matches != NULL) :
+ if(isset($matches[2])) $programName = trim($matches[2]) ?: "";
+ if(isset($matches[4])) $subprogramName = trim($matches[4]) ?: "";
+ if(isset($matches[5])) $rebroadcast = $matches[5] ? True : False;
+ if(isset($matches[7])) $episode = $matches[7] ?: "";
+ if(isset($matches[8])) :
+ $image = $matches[8] ? $matches[8] : "";
+ preg_match('/.*schedule_([\d,]+)?.*/', $image, $grade);
+ if($grade != NULL) $rating = $grade[1];
+ endif;
+ endif;
+ //programName, startTime, rating, subprogramName, rebroadcast, episode
+ $epginfo[] = array($programName, $startTime, $rating, $subprogramName, $rebroadcast, $episode );
+ endforeach;
+ endfor;
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+ $zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
+ foreach($zipped as $epg) :
+ $programName = $epg[0][0] ?: "";
+ $subprogramName = $epg[0][3] ?: "";
+ $startTime = $epg[0][1] ?: "";
+ $endTime = $epg[1][1] ?: "";
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = "";
+ $rebroadcast = $epg[0][4];
+ $episode = $epg[0][5] ?: "";
+ $rating = $epg[0][2] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+}
+
+// Get EPG data from KT
+function GetEPGFromKT($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $epginfo = array();
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://tv.olleh.com/renewal_sub/liveTv/pop_schedule_week.asp";
+ $day = date("Ymd", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'ch_name' => '',
+ 'ch_no' => $ServiceId,
+ 'nowdate'=> $day,
+ 'seldatie' => $day,
+ 'tab_no' => '1'
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ $response = str_replace("charset=euc-kr", "charset=utf-8", $response);
+ $dom = new DomDocument;
+ libxml_use_internal_errors(true);
+ $dom->loadHTML(mb_convert_encoding($response, "UTF-8", "EUC-KR"));
+ $xpath = new DomXPath($dom);
+ $query = "//table[@id='pop_day']/tbody/tr";
+ $rows = $xpath->query($query);
+ foreach($rows as $row) :
+ $cells = $row->getElementsByTagName('td');
+ #programName, startTime, rating, category
+ $startTime = date("YmdHis", strtotime($day." ".trim($cells->item(0)->nodeValue)));
+ $rating = str_replace("all", 0, str_replace("세 이상", "", trim($cells->item(2)->nodeValue)));
+ $epginfo[]= array(trim($cells->item(1)->nodeValue), $startTime, $rating, trim($cells->item(4)->nodeValue));
+ endforeach;
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+ $zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
+ foreach($zipped as $epg) :
+ $programName = "";
+ $subprogramName = "";
+ preg_match('/^(.*?)( <(.*)>)?$/', $epg[0][0], $matches);
+ if ($matches != NULL) :
+ if(isset($matches[1])) $programName = $matches[1] ?: "";
+ if(isset($matches[3])) $subprogramName = $matches[3] ?: "";
+ endif;
+ $startTime = $epg[0][1] ?: "";
+ $endTime = $epg[1][1] ?: "";
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = $epg[0][3] ?: "";
+ $rebroadcast = False;
+ $episode = "";
+ $rating = $epg[0][2] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+}
+
+// Get EPG data from LG
+function GetEPGFromLG($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $epginfo = array();
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://www.uplus.co.kr/css/chgi/chgi/RetrieveTvSchedule.hpi";
+ $day = date("Ymd", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'chnlCd' => $ServiceId,
+ 'evntCmpYmd' => $day
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ $response = ''.$response;
+ $dom = new DomDocument;
+ libxml_use_internal_errors(true);
+ $response = mb_convert_encoding($response, "UTF-8", "EUC-KR");
+ $response = str_replace(array('<재>', ' [..', ' (..'), array('<재>', '', ''), $response);
+ $dom->loadHTML($response);
+ $xpath = new DomXPath($dom);
+ $query = "//div[@class='tblType list']/table/tbody/tr";
+ $rows = $xpath->query($query);
+ foreach($rows as $row) :
+ $cells = $row->getElementsByTagName('td');
+ $programName = trim($cells->item(1)->childNodes->item(0)->nodeValue);
+ $startTime = date("YmdHis", strtotime($day." ".trim($cells->item(0)->nodeValue)));
+ $spans = $cells->item(1)->getElementsByTagName('span');
+ $rating = trim($spans->item(1)->nodeValue)=="All" ? 0 : trim($spans->item(1)->nodeValue);
+ //programName, startTime, rating, category
+ $epginfo[]= array($programName, $startTime, $rating, trim($cells->item(2)->nodeValue));
+ endforeach;
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+ $zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
+ foreach($zipped as $epg) :
+ preg_match('/(<재>?)?(.*?)(\[(.*)\])?\s?(\(([\d,]+)회\))?$/', $epg[0][0], $matches);
+ $programName = "";
+ $subprogramName = "";
+ $episode = "";
+ $rebroadcast = False;
+ if ($matches != NULL) :
+ if(isset($matches[2])) $programName = trim($matches[2]) ?: "";
+ if(isset($matches[4])) $subprogramName = trim($matches[4]) ?: "";
+ if(isset($matches[6])) $episode = trim($matches[6]) ?: "";
+ if(isset($matches[1])) $rebroadcast = trim($matches[1]) ? True: False;
+ endif;
+ $startTime = $epg[0][1] ?: "";
+ $endTime = $epg[1][1] ?: "";
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = $epg[0][3] ?: "";
+
+ $rating = $epg[0][2] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+}
+
+// Get EPG data from SK
+function GetEPGFromSK($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $today = date("Ymd");
+ $lastday = date("Ymd", strtotime("+".($GLOBALS['period'] - 1)." days"));
+ $url = "http://m.btvplus.co.kr/Common/Inc/IFGetData.asp";
+ $params = array(
+ 'variable' => 'IF_LIVECHART_DETAIL',
+ 'pcode' => '|^|start_time='.$today.'00|^|end_time='.$lastday.'24|^|svc_id='.$ServiceId
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if($data['channel'] == NULL) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $programs = $data['channel']['programs'];
+ foreach ($programs as $program) :
+ $programName = "";
+ $subprogramName = "";
+ $episode = "";
+ $rebroadcast = False;
+ preg_match('/^(.*?)(?:\s*[\(<]([\d,회]+)[\)>])?(?:\s*<([^<]*?)>)?(\((재)\))?$/', str_replace('...', '>', $program['programName']), $matches);
+ if ($matches != NULL) :
+ if(isset($matches[1])) $programName = trim($matches[1]) ?: "";
+ if(isset($matches[3])) $subprogramName = trim($matches[3]) ?: "";
+ if(isset($matches[2])) $episode = str_replace("회", "", $matches[2]) ?: "";
+ if(isset($matches[5])) $rebroadcast = $matches[5] ? True : False;
+ endif;
+ $startTime = date("YmdHis",$program['startTime']/1000);
+ $endTime = date("YmdHis",$program['endTime']/1000);
+ $desc = $program['synopsis'] ?: "";
+ $actors =trim(str_replace('...','',$program['actorName']), ', ') ?: "";
+ $producers = trim(str_replace('...','',$program['directorName']), ', ') ?: "";
+ if ($program['mainGenreName'] != NULL) :
+ $category = $program['mainGenreName'];
+ else:
+ $category = "";
+ endif;
+ $rating = $program['ratingCd'] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+}
+
+// Get EPG data from SKY
+function GetEPGFromSKY($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://www.skylife.co.kr/channel/epg/channelScheduleListJson.do";
+ $day = date("Y-m-d", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'area' => 'in',
+ 'inFd_channel_id' => $ServiceId,
+ 'inairdate' => $day,
+ 'indate_type' => 'now'
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if(count($data['scheduleListIn']) == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $programs = $data['scheduleListIn'];
+ foreach($programs as $program) :
+ $programName = htmlspecialchars_decode($program['program_name']) ?: "";
+ $subprogramName = str_replace(array('lt;', 'gt;', 'amp;'), array('<', '>', '&'),$program['program_subname']) ?: "";
+ $startTime = $program['starttime'];
+ $endTime = $program['endtime'];
+ $actors = trim(str_replace('...', '',$program['cast']), ', ') ?: "";
+ $producers = trim(str_replace('...', '',$program['dirt']), ', ') ?: "";
+ $description = str_replace(array('lt;', 'gt;', 'amp;'), array('<', '>', '&'),$program['description']) ?: "";
+ $summary = str_replace(array('lt;', 'gt;', 'amp;'), array('<', '>', '&'),$program['summary']) ?: "";
+ $desc = $description ?: "";
+ if($summary) :
+ $desc = $desc."\n".$summary;
+ endif;
+ $category = $program['program_category1'];
+ $episode = $program['episode_id'] ?: "";
+ $rebroadcast = $program['rebroad']== "Y" ? True : False;
+ $rating = $program['grade'] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+}
+
+// Get EPG data from Naver
+function GetEPGFromNaver($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $epginfo = array();
+ $totaldate = array();
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "https://search.naver.com/p/csearch/content/batchrender_ssl.nhn";
+ $day = date("Ymd", strtotime("+".($k - 1)." days"));
+ $totaldate[] = $day;
+ endforeach;
+ $params = array(
+ '_callback' => 'epg',
+ 'fileKey' => 'single_schedule_channel_day',
+ 'pkid' => '66',
+ 'u1' => 'single_schedule_channel_day',
+ 'u2' => join(",", $totaldate),
+ 'u3' => $day,
+ 'u4' => $GLOBALS['period'],
+ 'u5' => $ServiceId,
+ 'u6' => 1,
+ 'u7' => $ChannelName."편성표",
+ 'u8' => $ChannelName."편성표",
+ 'where' => 'nexearch'
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $response = str_replace('epg( ', '', $response );
+ $response = substr($response, 0, strlen($response)-2);
+ $response = preg_replace("/\/\*.*?\*\//","",$response);
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if($data['displayDates'][0]['count'] == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ for($i = 0; $i < count($data['displayDates']); $i++) :
+ for($j = 0; $j < 24; $j++) :
+ foreach($data['schedules'][$j][$i] as $program) :
+ //programName, startTime, episode, rebroadcast, rating
+ $startTime = date("YmdHis", strtotime($data['displayDates'][$i]['date']." ".$program['startTime']));
+ $epginfo[] = array($program['title'], $startTime, str_replace("회","", $program['episode']), $program['isRerun'], $program['grade']);
+ endforeach;
+ endfor;
+ endfor;
+ $zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
+ foreach($zipped as $epg) :
+ $programName = htmlspecialchars_decode($epg[0][0], ENT_XML1) ?: "";
+ $subprogramName = "";
+ $startTime = $epg[0][1] ?: "";
+ $endTime = $epg[1][1] ?: "";
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = "";
+ $rebroadcast = $epg[0][3] ? True: False;
+ $episode = $epg[0][2] ?: "";
+ $rating = $epg[0][4] ?: 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+}
+
+// Get EPG data from Tbroad
+function GetEPGFromTbroad($ChannelInfo) {
+ $url='https://www.tbroad.com/chplan/selectRealTimeListForNormal.tb';
+}
+
+// Get EPG data from Iscs
+function GetEPGFromIscs($ChannelInfo) {
+ $url='http://service.iscs.co.kr/sub/channel_view.asp';
+ $params = array(
+ 'chan_idx'=>'242',
+ 'source_id'=>'203',
+ 'Chan_Date'=>'2017-04-18'
+ );
+}
+
+// Get EPG data from MBC
+function GetEPGFromMbc($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $dayofweek = array('일', '월', '화', '수', '목', '금', '토');
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://miniunit.imbc.com/Schedule";
+ $day = date("Y-m-d", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'rtype' => 'json'
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if(count($data['Programs']) == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $programs = $data['Programs'];
+ foreach($programs as $program) :
+ if($program['Channel'] == "CHAM" && $program['LiveDays'] == $dayofweek[date("w", strtotime($day))]) :
+ $programName = "";
+ $rebroadcast = False;
+ preg_match('/^(.*?)(\(재\))?$/', htmlspecialchars_decode($program['ProgramTitle']), $matches);
+ if ($matches != NULL) :
+ $programName = $matches[1];
+ $rebroadcast = $matches[2] ? True : False;
+ endif;
+ $subprogramName = "";
+ $startTime = $day." ".$program['StartTime'];
+ $startTime = date("YmdHis", strtotime($startTime));
+ $endTime = date("YmdHis", strtotime("+".$program['RunningTime']." minutes", strtotime($startTime)));
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = "음악";
+ $episode = "";
+ $rating = 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endif;
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+}
+
+// Get EPG data from MIL
+function GetEPGFromMil($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://radio.dema.mil.kr/web/fm/quick/ajaxTimetableList.do";
+ $day = date("Y-m-d", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'program_date' => date("Ymd", strtotime($day))
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if(count($data['resultList']) == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $programs = $data['resultList'];
+ foreach($programs as $program) :
+ $programName = "";
+ $rebroadcast = False;
+ preg_match('/^(.*?)(\(재\))?$/', htmlspecialchars_decode($program['program_title']), $matches);
+ if ($matches != NULL) :
+ $programName = $matches[1];
+ $rebroadcast = $matches[2] ? True : False;
+ endif;
+ $subprogramName = htmlspecialchars_decode($program['program_subtitle']);
+ $startTime = $day." ".$program['program_time'];
+ $startTime = date("YmdHis", strtotime($startTime));
+ $endTime = $day." ".$program['program_end_time'];
+ $endTime = date("YmdHis", strtotime($endTime));
+ $desc = "";
+ $actors = htmlspecialchars_decode($program['movie_actor']);
+ $producers = htmlspecialchars_decode($program['movie_director']);
+ $category = "";
+ $episode = "";
+ $rating = 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+}
+
+// Get EPG data from IFM
+function GetEPGFromIfm($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $dayofweek = array('1', '2', '3', '4', '5', '6', '7');
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://mapp.itvfm.co.kr/hyb/front/selectHybPgmList.do";
+ $day = date("Y-m-d", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'outDay' => $dayofweek[(date("w", strtotime($day)+1))%7],
+ 'viewDt' => $day
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if(count($data['hybMusicInfoList']) == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $programs = $data['hybMusicInfoList'];
+ foreach($programs as $program) :
+ $programName = htmlspecialchars_decode($program['pgmTitle']) ?: "";
+ $subprogramName = "";
+ $startTime = $day." ".$program['pgmStime'];
+ $startTime = date("YmdHis", strtotime($startTime));
+ $endTime = $day." ".$program['pgmEtime'];
+ $endTime = date("YmdHis", strtotime($endTime));
+ $desc = "";
+ $actors = htmlspecialchars_decode($program['pgmDj']);
+ $producers = htmlspecialchars_decode($program['pgmPd']);
+ $category = "";
+ $episode = "";
+ $rebroadcast = False;
+ $rating = 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+}
+
+// Get EPG data from KBS
+function GetEPGFromKbs($ChannelInfo) {
+ $ChannelId = $ChannelInfo[0];
+ $ChannelName = $ChannelInfo[1];
+ $ServiceId = $ChannelInfo[3];
+ $epginfo = array();
+ foreach(range(1, $GLOBALS['period']) as $k) :
+ $url = "http://world.kbs.co.kr/include/wink/_ajax_schedule.php";
+ $day = date("Y-m-d", strtotime("+".($k - 1)." days"));
+ $params = array(
+ 'channel'=>'wink_11'
+ );
+ $params = http_build_query($params);
+ $url = $url."?".$params;
+ try {
+ $response = getWeb($url);
+ if ($response === False && $GLOBALS['debug']) :
+ printError($ChannelName.HTTP_ERROR);
+ else :
+ try {
+ $data = json_decode($response, TRUE);
+ if(json_last_error() != JSON_ERROR_NONE) throw new Exception(JSON_SYNTAX_ERROR);
+ if(count($data['schedule']) == 0) :
+ if($GLOBALS['debug']) :
+ printError($ChannelName.CHANNEL_ERROR);
+ endif;
+ else :
+ $dom = new DomDocument;
+ libxml_use_internal_errors(true);
+ $dom->loadHTML($data['schedule']);
+ $xpath = new DomXPath($dom);
+ $query = "//li";
+ $rows = $xpath->query($query);
+ foreach($rows as $row) :
+ $cells = $row->getElementsByTagName('span');
+ $programName = trim($cells->item(2)->childNodes->item(0)->nodeValue);
+ $programName = str_replace(array("[","]", " Broadcast"), array("", "", ""), $programName);
+ $startTime = $day." ".trim($cells->item(0)->childNodes->item(0)->nodeValue);
+ //programName, startTime, rating, category
+ $epginfo[]= array($programName, $startTime);
+ endforeach;
+ endif;
+ } catch(Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endif;
+ } catch (Exception $e) {
+ if($GLOBALS['debug']) printError($e->getMessage());
+ }
+ endforeach;
+ $zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
+ foreach($zipped as $epg) :
+ $programName = $epg[0][0] ?: "";
+ $subprogramName = "";
+ $startTime = $epg[0][1] ?: "";
+ $startTime = date("YmdHis", strtotime($startTime));
+ $endTime = $epg[1][1] ?: "";
+ $endTime = date("YmdHis", strtotime($endTime));
+ $desc = "";
+ $actors = "";
+ $producers = "";
+ $category = "";
+ $rebroadcast = False;
+ $episode = "";
+ $rating = 0;
+ $programdata = array(
+ 'channelId'=> $ChannelId,
+ 'startTime' => $startTime,
+ 'endTime' => $endTime,
+ 'programName' => $programName,
+ 'subprogramName'=> $subprogramName,
+ 'desc' => $desc,
+ 'actors' => $actors,
+ 'producers' => $producers,
+ 'category' => $category,
+ 'episode' => $episode,
+ 'rebroadcast' => $rebroadcast,
+ 'rating' => $rating
+ );
+ writeProgram($programdata);
+ endforeach;
+}
+
+function writeProgram($programdata) {
+ $fp = $GLOBALS['fp'];
+ $ChannelId = $programdata['channelId'];
+ $startTime = $programdata['startTime'];
+ $endTime = $programdata['endTime'];
+ $programName = htmlspecialchars($programdata['programName'], ENT_XML1);
+ $subprogramName = htmlspecialchars($programdata['subprogramName'], ENT_XML1);
+ $actors = htmlspecialchars($programdata['actors'], ENT_XML1);
+ $producers = htmlspecialchars($programdata['producers'], ENT_XML1);
+ $category = htmlspecialchars($programdata['category'], ENT_XML1);
+ $episode = $programdata['episode'];
+ $rebroadcast = $programdata['rebroadcast'];
+ if($episode && $GLOBALS['addepisode'] == 'y') $programName = $programName." (".$episode."회)";
+ if($rebroadcast == True && $GLOBALS['addrebroadcast'] == 'y') $programName = $programName." (재)";
+ if($programdata['rating'] == 0) :
+ $rating = "전체 관람가";
+ else :
+ $rating = sprintf("%s세 이상 관람가", $programdata['rating']);
+ endif;
+ if($GLOBALS['addverbose'] == 'y') :
+ $desc = htmlspecialchars($programdata['programName'], ENT_XML1);
+ if($subprogramName) $desc = $desc."\n부제 : ".$subprogramName;
+ if($episode) $desc = $desc."\n회차 : ".$episode."회";
+ if($category) $desc = $desc."\n장르 : ".$category;
+ if($actors) $desc = $desc."\n출연 : ".$actors;
+ if($producers) $desc = $desc."\n제작 : ".$producers;
+ $desc = $desc."\n등급 : ".$rating;
+ else:
+ $desc = "";
+ endif;
+ if($programdata['desc']) $desc = $desc."\n".htmlspecialchars($programdata['desc'], ENT_XML1);
+ $contentTypeDict = array(
+ '교양' => 'Arts / Culture (without music)',
+ '만화' => 'Cartoons / Puppets',
+ '교육' => 'Education / Science / Factual topics',
+ '취미' => 'Leisure hobbies',
+ '드라마' => 'Movie / Drama',
+ '영화' => 'Movie / Drama',
+ '음악' => 'Music / Ballet / Dance',
+ '뉴스' => 'News / Current affairs',
+ '다큐' => 'Documentary',
+ '라이프' => 'Documentary',
+ '시사/다큐' => 'Documentary',
+ '연예' => 'Show / Game show',
+ '스포츠' => 'Sports',
+ '홈쇼핑' => 'Advertisement / Shopping'
+ );
+ $contentType = "";
+ foreach($contentTypeDict as $key => $value) :
+ if(!(strpos($category, $key) === False)) :
+ $contentType = $value;
+ endif;
+ endforeach;
+ fprintf($fp, " \n", $startTime, $endTime, $ChannelId);
+ fprintf($fp, " %s\n", $programName);
+ if($subprogramName) :
+ fprintf($fp, " %s\n", $subprogramName);
+ endif;
+ if($GLOBALS['addverbose']=='y') :
+ fprintf($fp, " %s\n", $desc);
+ if($actors || $producers):
+ fprintf($fp, " \n");
+ if($actors) :
+ foreach(explode(',', $actors) as $actor):
+ if(trim($actor)) fprintf($fp, " %s\n", trim($actor));
+ endforeach;
+ endif;
+ if($producers) :
+ foreach(explode(',', $producers) as $producer):
+ if(trim($producer)) fprintf($fp, " %s\n", trim($producer));
+ endforeach;
+ endif;
+ fprintf($fp, " \n");
+ endif;
+ endif;
+ if($category) fprintf($fp, " %s\n", $category);
+ if($contentType) fprintf($fp, " %s\n", $contentType);
+ if($episode) fprintf($fp, " %s\n", $episode);
+ if($rebroadcast) fprintf($fp, " \n");
+ if($rating) :
+ fprintf($fp, " \n");
+ fprintf($fp, " %s\n", $rating);
+ fprintf($fp, " \n");
+ endif;
+ fprintf($fp, " \n");
+}
+function getWeb($url) {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['ua']);
+ $response = curl_exec($ch);
+ curl_close($ch);
+ return $response;
+}
+function printLog($string) {
+ print($string."\n");
+}
+function printError($string) {
+ header("Content-Type: text/plain; charset=utf-8");
+ print("Error : ".$string."\n");
+}
+?>
diff --git a/epg2xml.php b/epg2xml.php
index 6e58f95..a145719 100644
--- a/epg2xml.php
+++ b/epg2xml.php
@@ -1,9 +1,9 @@
#!/usr/bin/env php
getMessage());
exit;
}
- endif;
+endif;
if($output == "display") :
$fp = fopen('php://output', 'w+');
@@ -275,6 +301,7 @@ function getEPG() {
$fp = $GLOBALS['fp'];
$MyISP = $GLOBALS['MyISP'];
$Channelfile = __DIR__."/Channel.json";
+ $IconUrl = "";
try {
$f = @file_get_contents($Channelfile);
if($f === False) :
@@ -376,13 +403,6 @@ function GetEPGFromEPG($ChannelInfo) {
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
$epginfo = array();
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
- $epginfo = array();
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://www.epg.co.kr/epg-cgi/extern/cnm_guide_type_v070530.cgi";
$day = date("Ymd", strtotime("+".($k - 1)." days"));
@@ -395,7 +415,7 @@ function GetEPGFromEPG($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -430,18 +450,25 @@ function GetEPGFromEPG($ChannelInfo) {
break;
endswitch;
$startTime = date("YmdHis", strtotime($thisday." ".$hour));
+ $programName = "";
+ $subprogramName = "";
+ $rating = 0;
+ $episode = "";
+ $rebroadcast = False;
preg_match('/?(.*)?(.*?)\s*(<(.*)>)?\s*(\(재\))?\s*(\(([\d,]+)회\))?()?\s*<\/td>/', trim($dom->saveHTML($program)), $matches);
if ($matches != NULL) :
- $image = $matches[8] ? $matches[8] : "";
- preg_match('/.*schedule_([\d,]+)?.*/', $image, $grade);
- if($grade != NULL) :
- $rating = $grade[1];
- else :
- $rating = 0;
+ if(isset($matches[2])) $programName = trim($matches[2]) ?: "";
+ if(isset($matches[4])) $subprogramName = trim($matches[4]) ?: "";
+ if(isset($matches[5])) $rebroadcast = $matches[5] ? True : False;
+ if(isset($matches[7])) $episode = $matches[7] ?: "";
+ if(isset($matches[8])) :
+ $image = $matches[8] ? $matches[8] : "";
+ preg_match('/.*schedule_([\d,]+)?.*/', $image, $grade);
+ if($grade != NULL) $rating = $grade[1];
endif;
endif;
//programName, startTime, rating, subprogramName, rebroadcast, episode
- $epginfo[] = array(trim($matches[2]), $startTime, $rating, trim($matches[4]), $matches[5], $matches[7]);
+ $epginfo[] = array($programName, $startTime, $rating, $subprogramName, $rebroadcast, $episode );
endforeach;
endfor;
endif;
@@ -459,7 +486,7 @@ function GetEPGFromEPG($ChannelInfo) {
$actors = "";
$producers = "";
$category = "";
- $rebroadcast = $epg[0][4] ? True : False;
+ $rebroadcast = $epg[0][4];
$episode = $epg[0][5] ?: "";
$rating = $epg[0][2] ?: 0;
$programdata = array(
@@ -486,13 +513,6 @@ function GetEPGFromKT($ChannelInfo) {
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
$epginfo = array();
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
- $epginfo = array();
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://tv.olleh.com/renewal_sub/liveTv/pop_schedule_week.asp";
$day = date("Ymd", strtotime("+".($k - 1)." days"));
@@ -506,7 +526,7 @@ function GetEPGFromKT($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -534,9 +554,9 @@ function GetEPGFromKT($ChannelInfo) {
$programName = "";
$subprogramName = "";
preg_match('/^(.*?)( <(.*)>)?$/', $epg[0][0], $matches);
- if($matches) :
- $programName = $matches[1] ?: "";
- $subprogramName = $matches[3] ?: "";
+ if ($matches != NULL) :
+ if(isset($matches[1])) $programName = $matches[1] ?: "";
+ if(isset($matches[3])) $subprogramName = $matches[3] ?: "";
endif;
$startTime = $epg[0][1] ?: "";
$endTime = $epg[1][1] ?: "";
@@ -571,13 +591,6 @@ function GetEPGFromLG($ChannelInfo) {
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
$epginfo = array();
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
- $epginfo = array();
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://www.uplus.co.kr/css/chgi/chgi/RetrieveTvSchedule.hpi";
$day = date("Ymd", strtotime("+".($k - 1)." days"));
@@ -588,7 +601,7 @@ function GetEPGFromLG($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -618,16 +631,23 @@ function GetEPGFromLG($ChannelInfo) {
$zipped = array_slice(array_map(NULL, $epginfo, array_slice($epginfo,1)),0,-1);
foreach($zipped as $epg) :
preg_match('/(<재>?)?(.*?)(\[(.*)\])?\s?(\(([\d,]+)회\))?$/', $epg[0][0], $matches);
- $programName = trim($matches[2]) ?: "";
- $subprogramName = trim($matches[4]) ?: "";
+ $programName = "";
+ $subprogramName = "";
+ $episode = "";
+ $rebroadcast = False;
+ if ($matches != NULL) :
+ if(isset($matches[2])) $programName = trim($matches[2]) ?: "";
+ if(isset($matches[4])) $subprogramName = trim($matches[4]) ?: "";
+ if(isset($matches[6])) $episode = trim($matches[6]) ?: "";
+ if(isset($matches[1])) $rebroadcast = trim($matches[1]) ? True: False;
+ endif;
$startTime = $epg[0][1] ?: "";
$endTime = $epg[1][1] ?: "";
$desc = "";
$actors = "";
$producers = "";
$category = $epg[0][3] ?: "";
- $rebroadcast = trim($matches[1]) ? True: False;
- $episode = trim($matches[6]) ?: "";
+
$rating = $epg[0][2] ?: 0;
$programdata = array(
'channelId'=> $ChannelId,
@@ -654,12 +674,6 @@ function GetEPGFromSK($ChannelInfo) {
$ServiceId = $ChannelInfo[3];
$today = date("Ymd");
$lastday = date("Ymd", strtotime("+".($GLOBALS['period'] - 1)." days"));
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
$url = "http://m.btvplus.co.kr/Common/Inc/IFGetData.asp";
$params = array(
'variable' => 'IF_LIVECHART_DETAIL',
@@ -668,7 +682,7 @@ function GetEPGFromSK($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -688,10 +702,10 @@ function GetEPGFromSK($ChannelInfo) {
$rebroadcast = False;
preg_match('/^(.*?)(?:\s*[\(<]([\d,회]+)[\)>])?(?:\s*<([^<]*?)>)?(\((재)\))?$/', str_replace('...', '>', $program['programName']), $matches);
if ($matches != NULL) :
- $programName = trim($matches[1]) ?: "";
- $subprogramName = trim($matches[3]) ?: "";
- $episode = str_replace("회", "", $matches[2]) ?: "";
- $rebroadcast = $matches[5] ? True : False;
+ if(isset($matches[1])) $programName = trim($matches[1]) ?: "";
+ if(isset($matches[3])) $subprogramName = trim($matches[3]) ?: "";
+ if(isset($matches[2])) $episode = str_replace("회", "", $matches[2]) ?: "";
+ if(isset($matches[5])) $rebroadcast = $matches[5] ? True : False;
endif;
$startTime = date("YmdHis",$program['startTime']/1000);
$endTime = date("YmdHis",$program['endTime']/1000);
@@ -735,12 +749,6 @@ function GetEPGFromSKY($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'POST',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://www.skylife.co.kr/channel/epg/channelScheduleListJson.do";
$day = date("Y-m-d", strtotime("+".($k - 1)." days"));
@@ -753,7 +761,7 @@ function GetEPGFromSKY($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -815,12 +823,6 @@ function GetEPGFromNaver($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
$epginfo = array();
$totaldate = array();
foreach(range(1, $GLOBALS['period']) as $k) :
@@ -845,7 +847,7 @@ function GetEPGFromNaver($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -928,12 +930,6 @@ function GetEPGFromMbc($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
$dayofweek = array('일', '월', '화', '수', '목', '금', '토');
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://miniunit.imbc.com/Schedule";
@@ -944,7 +940,7 @@ function GetEPGFromMbc($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -1009,12 +1005,6 @@ function GetEPGFromMil($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://radio.dema.mil.kr/web/fm/quick/ajaxTimetableList.do";
$day = date("Y-m-d", strtotime("+".($k - 1)." days"));
@@ -1024,7 +1014,7 @@ function GetEPGFromMil($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -1088,12 +1078,6 @@ function GetEPGFromIfm($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
$dayofweek = array('1', '2', '3', '4', '5', '6', '7');
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://mapp.itvfm.co.kr/hyb/front/selectHybPgmList.do";
@@ -1105,7 +1089,7 @@ function GetEPGFromIfm($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -1164,12 +1148,6 @@ function GetEPGFromKbs($ChannelInfo) {
$ChannelId = $ChannelInfo[0];
$ChannelName = $ChannelInfo[1];
$ServiceId = $ChannelInfo[3];
- $options = array(
- 'http' => array(
- 'method' => 'GET',
- 'header'=> $GLOBALS['ua']
- ));
- $context = stream_context_create($options);
$epginfo = array();
foreach(range(1, $GLOBALS['period']) as $k) :
$url = "http://world.kbs.co.kr/include/wink/_ajax_schedule.php";
@@ -1180,7 +1158,7 @@ function GetEPGFromKbs($ChannelInfo) {
$params = http_build_query($params);
$url = $url."?".$params;
try {
- $response = @file_get_contents($url, False, $context);
+ $response = getWeb($url);
if ($response === False && $GLOBALS['debug']) :
printError($ChannelName.HTTP_ERROR);
else :
@@ -1334,10 +1312,21 @@ function writeProgram($programdata) {
endif;
fprintf($fp, " \n");
}
-function printLog($args) {
- fwrite(STDERR, $args."\n");
+function getWeb($url) {
+ $ch = curl_init();
+ curl_setopt($ch, CURLOPT_URL, $url);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
+ curl_setopt($ch, CURLOPT_HEADER, false);
+ curl_setopt($ch, CURLOPT_USERAGENT, $GLOBALS['ua']);
+ $response = curl_exec($ch);
+ curl_close($ch);
+ return $response;
}
-function printError($args) {
- fwrite(STDERR, "Error : ".$args."\n");
+function printLog($string) {
+ fwrite(STDERR, $string."\n");
+}
+function printError($string) {
+ fwrite(STDERR, "Error : ".$string."\n");
}
?>
+
diff --git a/epg2xml.py b/epg2xml.py
index c14161e..cafd122 100644
--- a/epg2xml.py
+++ b/epg2xml.py
@@ -2,24 +2,44 @@
# -*- coding: utf-8 -*-
from __future__ import print_function
+import imp
+
import os
import sys
-import requests
import json
import locale
import datetime
-from bs4 import BeautifulSoup, SoupStrainer
import codecs
import socket
import re
from xml.sax.saxutils import escape, unescape
import argparse
import pprint
-
+try:
+ imp.find_module('bs4')
+ from bs4 import BeautifulSoup, SoupStrainer
+except ImportError:
+ print("Error : ", "BeautifulSoup 모듈이 설치되지 않았습니다.", file=sys.stderr)
+ sys.exit()
+try:
+ imp.find_module('lxml')
+except ImportError:
+ print("Error : ", "lxml 모듈이 설치되지 않았습니다.", file=sys.stderr)
+ sys.exit()
+try:
+ imp.find_module('requests')
+ import requests
+except ImportError:
+ print("Error : ", "requests 모듈이 설치되지 않았습니다.", file=sys.stderr)
+ sys.exit()
reload(sys)
sys.setdefaultencoding('utf-8')
-__version__ = '1.1.8'
+__version__ = '1.1.9'
+
+if not sys.version_info[:2] == (2, 7):
+ print("Error : ", "python 2.7 버전이 필요합니다.", file=sys.stderr)
+ sys.exit()
# Set variable
debug = False
diff --git a/readme.txt b/readme.txt
index b18c958..b4e8e75 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,15 +1,98 @@
1. 버전
- 1.1.8
+ 1.1.9
2. 소개
- 이 프로그램은 EPG(Electronic Program Guide)를 웹상의 여러 소스에서 가져와서 XML로 출력하는 프로그램으로 python2 및 php5 Cli에서 사용 가능하도록 제작되었다.
+ 이 프로그램은 EPG(Electronic Program Guide)를 웹상의 여러 소스에서 가져와서 XML로 출력하는 프로그램으로 python2 및 php5이상 Cli에서 사용 가능하도록 제작되었다.
기본적으로 외부의 소스를 분석하여 출력하므로 외부 소스 사이트가 변경되거나 삭제되면 문제가 발생할 수 있다.
+4. 변경사항
+ Version 1.1.9
+ - 언어 버전 사항 체크
+ - 필요 모듈 사항 체크
+ - 버그 수정
+ - php 버전 웹 버전 추가
+ - php 버전 file_get_contents를 curl 사용으로 수정
+ Version 1.1.8
+ - KBS 함수 추가
+ - 채널 변경 사항 반영
+ - 스카이라이프 url 변경
+ - EPG 누락 데이터 수정
+ Version 1.1.7
+ - PHP 7.0 지원
+ - 채널 변경 사항 반영
+ - 라디오 채널 추가
+ Version 1.1.6
+ - iptv 선택 항목에 ALL 추가
+ - 에피소드 넘버 출력 수정
+ - 시작 시간 에러 출력 수정
+ - 타이틀 출력 수정
+ - 서브타이틀 추출 수정
+ - 데이터 중복 출력 문제 수정
+ - php 버전이 5.6.3 이전일 때 DOM access 관련 에러 수정
+ Version 1.1.5
+ - inline 변수 재추가
+ Version 1.1.4
+ - epg2xml.json 파일 도입
+ - inline 변수 삭제
+ - PHP 버전 추가
+ - 버그 수정
+ Version 1.1.3
+ - 제목에 회차정보, 재방송 정보 추가시 오류 수정
+ Version 1.1.2
+ - 재방송정보, 회차정보 옵션 추가
+ Version 1.1.1
+ - sk 카테고리 오류 수정
+ Version 1.1.0
+ - 채널 아이콘 추가
+ - 오류 메시지 통합
+ Version 1.0.9
+ - 소켓파일이 없을 때 오류 추가
+ - 채널 변경 사항 반영
+ Version 1.0.8
+ - 정지 시간 추가
+ - 오류 출력 구문 디버그시만 출력으로 변경
+ - 채널 소스 변경
+ Version 1.0.7
+ - urllib2를 requests로 변경
+ - User Agent 변경
+ - 오류 처리 추가
+ - 채널 변경 사항 반영
+ - 채널 소스 변경
+ - 지역 지상파 채널 추가
+ Version 1.0.6
+ - urllib를 urllib2로 변경
+ - User Agent 추가
+ - 채널 변경 사항 반영
+ Version 1.0.5
+ - epg.co.kr의 epg 정보 못가져오는 것 수정
+ Version 1.0.4
+ - KODI에서 사용가능하도록 수정
+ - 제목에서 서브타이틀 및 회차 분리
+ - 서브타이틀 추가
+ - 출연, 제작진 개인별로 분리
+ Version 1.0.3
+ - Channel.json 파일 오류 수정
+ - LG를 소스로 하는 EPG 정보 기간 오류 수정
+ Version 1.0.2
+ - ISP별 분리된 채널통합
+ - 개별 채널별 EPG 정보 수집가능하도록 Enabled 추가
+ - getMyChannel 함수 삭제
+ - 채널 변경 사항 반영
+ - KT TRU TV 채널 삭제
+ - ISP 선택 설정 추가
+ - EPG 정보 가져오는 기간 설정 추가
+ - 채널 아이콘 설정 URL 설정 추가
+ - tvheadend 전용 카테고리 추가
+ Version 1.0.1
+ - EPG 소스 변경
+ - 등록된 채널 정보만 EPG 정보 가져오도록 설정
+ - IPTV별 개인화
+ Version 1.0.0
+ - first release
-3. 변경사항
- KBS 함수 추가
- Channel.json 소스 변경
-스카이라이프 url 변경
- EPG 누락 데이터 수정
-4. 저작권
+5. 저작권
- BSD
| |