/tmp/hongkong.xml /usr/bin/mythfilldatabase --file 1 1 /tmp/hongkong.xml make sure the filltv file is chmod +x Quick hack to generate xmltv listings from TVxb style .ini files. */ // seriously it's this bad.. error_reporting(E_ALL & ~E_STRICT &~E_NOTICE); if (!extension_loaded('mbstring')) { dl('mbstring.so'); } define ('QUOTE', '"'); class XML_Tv { var $config; // configuration array. var $date; // date we are fetching var $channels; // details on the channels. function start($ini) { // load the ini file. require_once 'JSON.php'; $j = new Services_JSON(); $conf = (array)($j->decode(file_get_contents($ini))); //$conf = (array)json_decode(file_get_contents($ini)); //print_r($conf); foreach($conf as $k=>$v) { $conf[$k] = (array)$v; } $this->channels = $conf; $this->config = $conf['global']; unset($this->channels['global']); //print_r($this->channels); $this->date = time(); foreach($this->channels as $k=>$v) { for ($i=0;$i< (isset($v['days']) ? $v['days'] : $conf['days']);$i++) { $this->grabChannel($k,$i); // break; } //break; } echo $this->toXML(); } function grabChannel($k,$dayoffset) { $cinfo = $this->channels[$k]; //print_r($cinfo); // exit; $date = $this->date + ($dayoffset * 24 * 60 * 60 ); $url = strftime( $cinfo['url'], $date); $this->debug($url ); //echo "GET $url\n"; $data = @file_get_contents($url); if (empty($data)) { $this->debug("NO DATA"); // something went wrong.. return; } $odata = $data; $map = array( array("0x22", "0x09", "0x0a"), array('"', "\t", "\n") ); $hs = $cinfo['htmlstart'] === false ? false : str_replace($map[0],$map[1],$cinfo['htmlstart']); //echo $hs; $he= str_replace($map[0],$map[1],$cinfo['htmlend']); //echo $he; if ($hs !== false) { list( , $data) = explode($hs,$data); } if (!empty($he)) { list($data,) = explode($he ,$data); } //$this->debug("DATA:".$data); $method = 'parse'.$cinfo['htmlparsetype']; $chid = isset($cinfo['id']) ? $cinfo['id'] : $k; // for multiday html layout of atv: if (!empty($cinfo['htmldaysep'])) { $days = explode($cinfo['htmldaysep'], $data); // kludge. = first monday of current week.. // loook for...
2007-12-31 Mon $start = preg_match('/
([0-9]{4}-[0-9]{2}-[0-9]{2}) Mon/i', $odata, $matches); $start = isset($matches[1]) ? $matches[1] : 0; // first monday.. $use_cols = 1; foreach($days as $i=>$ddata) { $cols = explode(',',$cinfo['htmlcols']); $dn = strtotime($start) + (($i * $use_cols) * 24 * 60 * 60 ); $res = $this->$method($ddata,$cols,date('Y-m-d',$dn)); if (is_string($res)) { $start = $res; $use_cols =0; continue; } if (!is_array($res)) { continue; } $day_id = strtotime($start) + (($i * $use_cols ) * 24 * 60 * 60 ); //print_r($res); if (empty($this->schedule[$chid][$day_id])) { $this->schedule[$chid][$day_id] = array(); } $this->schedule[$chid][$day_id] = array_merge($this->schedule[$chid][$day_id], $res); } return; } $cols = explode(',',$cinfo['htmlcols']); //print_r(array($data,$cols)); $this->schedule[$k][$this->date + ($dayoffset * 24 * 60 * 60 )] = $this->$method($data,$cols); //print_r($this->schedule); } //tvb +??? function parseTable($data,$colnames) { $rows = preg_split('/]*>/i', $data); // print_r($rows);exit; array_shift($rows); foreach($rows as $r) { //print_r($r); $cols = preg_split('/\]*\>/i', trim($r)); $rdata = array(); array_shift($cols); //var_dump(count(array_values($cols))); //print_r($r); // print_r($cols);exit; foreach($cols as $i=>$c) { if (count(array_values($cols)) != count(array_values($colnames))) { continue; } if (preg_match('/',' ', $c); $c = str_ireplace(' ',' ', $c); $c = str_replace("\n",' ', $c); $c = str_replace("\r",' ', $c); //var_dump($c); $rdata[$colnames[$i]] = trim(strip_tags($c)); } // print_r($rdata); if (count(array_values($rdata)) != count(array_values($colnames))) { continue; } // print_R($rdata); $ret[] = $rdata; } $this->debug(print_r($ret,true)); return $ret; } function parseJade($data, $colnames, $day=0) { $rows = preg_split('##i', $data); //print_r($rows); $ret = array(); foreach($rows as $r) { $r = str_ireplace(' ',' ', $r); $rdata = array(); @list($time,$r) = explode('', $r, 2); $rdata['hour'] = trim(strip_tags($time)); if (!strlen($rdata['hour'])) { continue; } //list($title,$r) = explode('', $r, 2); list($title,$r) = explode('

', $r, 2); $rdata['description'] = trim(strip_tags($title)); $rdata['description2'] = trim(strip_tags($r)); $rdata['day'] = $day; $ret[] = $rdata; } //print_R($ret); return $ret; } function parseatv($data,$colnames, $day) { // if it's a day row.. $lines = explode("\n", trim($data)); //var_dump($lines[1]); if (isset($lines[1]) && preg_match('/
([0-9]{4}-[0-9]{2}-[0-9]{2})/i', $lines[1], $matches); //var_dump($matches[1]); return $matches[1]; } $rows = preg_split('/]*>/i', $data); if ($day == '2011-10-29') { //$this->debug(print_r($rows,true)); } if ($day < date('Y-m-d')) { // $this->debug("OLD DATA $day"); return array(); } //$this->debug($day); //$this->debug(print_r($rows,true)); //return; array_shift($rows); $ret = array(); //$day = false; foreach($rows as $r) { //print_r($r); $cols = preg_split('/]*>/i', $r); $rdata = array(); if (!isset($cols[2])) { continue; } //PRINT_r($cols); $c= $cols[2]; //var_dump($c); // look for time.. if (!preg_match('/^[0-9]+:[0-9]+/', $c)) { continue; } //$this->debug("GOT HOUR: $c"); $rdata['hour'] = trim(array_shift(explode('<', $c))); $c = $cols[3]; $kv = preg_split('/
/',$c); $c = $kv[0]; $c = str_ireplace('
',' ', $c); $c = str_ireplace(' ',' ', $c); $c = str_replace("\n",' ', $c); $c = str_replace("\r",' ', $c); $c = preg_replace('/\<[^>]+\>/', ' ', $c); $rdata['description2'] = trim($c); $c = isset($kv[1]) ? $kv[1] : ''; $c = str_ireplace('
',' ', $c); $c = str_ireplace(' ',' ', $c); $c = str_replace("\n",' ', $c); $c = str_replace("\r",' ', $c); $c = preg_replace('/\<[^>]+\>/', ' ', $c); $rdata['description'] = trim($c); $rdata['day'] = $day; //print_R($rdata); $ret[] = $rdata; } //print_r($ret); exit; return $ret; } /* function parseTableCells($data,$colnames, $day) { $rows = preg_split('/]*>/i', $data); //$this->debug(print_r($rows,true)); //exit; //exit; array_shift($rows); $day = false; foreach($rows as $r) { //print_r($r); $cols = preg_split('/]*>/i', $r); $rdata = array(); $c= $cols[1]; //var_dump($c); // look for time.. if (!preg_match('/^[0-9]+:[0-9]+\s/', $c)) { continue; } $c = str_ireplace('
',' ', $c); $c = str_ireplace(' ',' ', $c); $c = str_replace("\n",' ', $c); $c = str_replace("\r",' ', $c); $c = preg_replace('/\<[^>]+\>/', ' ', $c); $c = trim($c); $kv = preg_split("/\s+/", $c, 2); $rdata[$colnames[0]] = trim($kv[0]); $rdata[$colnames[1]] = trim($kv[1]); //print_r($kv); if (count(array_values($rdata)) != count(array_values($colnames))) { continue; } $rdata['day'] = $day; $ret[] = $rdata; } //print_r($ret); return $ret; } */ /* BBC2 Channel 4 The Phil Silvers Show Bilko claims he's had a close encounter with an alien in order to be given some compassionate leave so he can visit an old flame in New York. King of the Hill Meet the Propaniacs Bobby tours with a comedy troupe who specialize in propane-related mirth. Mike Judge Lane Smith animation */ function toXml() { //print_r($this->schedule); $doc = new DomDocument('1.0', 'UTF-8'); $tv = $doc->createElement('tv'); $tv->setAttribute( 'generator-info-name','akpear_xml_tv'); $doc->appendChild($tv); //$out = '<'.'?xml version="1.0" encoding="UTF-8"?.'>'."\n" . // ''."\n" ."\n" . /// ''."\n"; $donec = array(); foreach($this->channels as $k => $v) { // dont dupe?! $chid = isset($v['id']) ? $v['id'] : $k; if (isset($donec[$chid])) { continue; } $donec[$chid] = true; $ch = $doc->createElement('channel'); $ch->setAttribute('id', $chid); $disp = $doc->createElement('display-name'); $disp->setAttribute('lang', 'en'); $disp->appendChild($doc->createTextNode($v['name'])); $ch->appendChild($disp); $tv->appendChild($ch); //$out .= // ' // '. $v['name'] .' // '."\n"; } //print_r($this->schedule); foreach($this->schedule as $chan => $scheds) { //print_r($sched); foreach($scheds as $day => $sched) { $hoffset = 0; $last = -1; if (empty($sched)) { continue; } foreach($sched as $item) { $item['day'] = $day; $bits = explode(':', $item['hour']); if ($bits[0] < $last) { $hoffset +=12; } $last = $bits[0]; //var_dump($bits[0] + $hoffset); $start = mktime(/*hmsmdy */ $bits[0] + $hoffset, $bits[1], 0, date('m', $day), date('d', $day), date('Y', $day) ); if ($start < strtotime(date("Y-m-d 00:00:00", strtotime('NOW - 1 DAY')))) { continue; } $item['hoffset'] = $hoffset; $item['hoffset_ar'] = $bits; //$this->debug(print_r($item, true)); $start_str = date('YmdHis',$start) . ' ' . $this->config['gmtoffset']; //var_dump($start_str); //var_dump($this->channels); $description = iconv($this->channels[$chan]['encoding'], 'UTF-8',$item['description'] . (isset($item['description2']) ? (' ' . $item['description2']) : '')); $this->debug(date("Y-m-d H:i - ", $start). $description); $pg = $doc->createElement('programme'); $pg->setAttribute('channel', $chan); $pg->setAttribute('start', $start_str); $title = $doc->createElement('title'); $title->setAttribute('lang', 'zh'); $title->appendChild($doc->createTextNode($this->toTitle($description,$chan))); $pg->appendChild($title); $title = $doc->createElement('desc'); $title->setAttribute('lang', 'zh'); $title->appendChild($doc->createTextNode($description)); $pg->appendChild($title); $tv->appendChild($pg); //$out.= ' // '. $this->toTitle($description) .' // '. $description .' // '."\n"; } } } $doc->formatOutput = true; //$out .= "\n"; return $doc->saveXML(); } function toTitle($description, $chan) { // remove sponsor message. $title_pre = ''; @list($title, $fuldesc) = explode(">>",$description); if (preg_match('/Followed\s*By/i', $title)) { $bits = preg_split('/Followed\s*By/i', $title); $title_pre = $bits[0] . ' Followed By '; $title = $bits[1]; } $title = preg_replace('#countdown to[a-z0-9 ]+#i', '' , $title); # NICAM Language $title = preg_replace('#^(solar x|Samsung Digital)\s*#i', '' , $title); # known sponsors.. $title = preg_replace('#[a-z0-9 ]+(presents|special|blockbuster|movie of the month|showtime)\s*:\s*#i', '', $title); $title = preg_replace('#\([a-z]+/[a-z]+\s*(|bilingual)\)#i', '' , $title); # NICAM Language $title = preg_replace('#\(live\)#i', '' , $title); # live $title = preg_replace('#\((s|c|l|e|cs|es|ecs|can|ce)[*]*\)#i', '' , $title); # Subtitle $title = preg_replace('#\((pg\d*\w*)\)#i', '' , $title); # Adult $title = preg_replace('#\(r\)#i', '' , $title); # Repeated $title = trim($title, '/'); $title = trim($title); $ret = $title_pre . $title; // if (!strlen($ret)) { // die("got $description : nothing to return"); // } if (in_array($chan, array('tvbpearl.hk', 'english.atvworld.hk'))) { $enonly = preg_replace('#^[^a-z0-9]+#i', '', $title); if (strlen($enonly) > 10) { $title = $enonly; } } return $title_pre . $title; } function debug($str) { if (empty($this->config['debug'])) { return; } echo $str."\n"; } } $x = new XML_Tv; //print_r($_SERVER); $x->start($_SERVER['argv'][1]);