5 make a weekly cronjob (/etc/cron.weekly/filltv)
7 /usr/bin/php /path/to/application/Tv.php /path/to/application/hongkong.ini > /tmp/hongkong.xml
8 /usr/bin/mythfilldatabase --file 1 1 /tmp/hongkong.xml
10 make sure the filltv file is chmod +x
14 Quick hack to generate xmltv listings from TVxb style .ini files.
19 if (!extension_loaded('mbstring')) {
23 define ('QUOTE', '"');
26 var $config; // configuration array.
27 var $date; // date we are fetching
28 var $channels; // details on the channels.
34 require_once 'JSON.php';
35 $j = new Services_JSON();
36 $conf = (array)($j->decode(file_get_contents($ini)));
37 //$conf = (array)json_decode(file_get_contents($ini));
39 foreach($conf as $k=>$v) {
40 $conf[$k] = (array)$v;
42 $this->channels = $conf;
43 $this->config = $conf['global'];
44 unset($this->channels['global']);
45 //print_r($this->channels);
50 foreach($this->channels as $k=>$v) {
52 for ($i=0;$i< (isset($v['days']) ? $v['days'] : $conf['days']);$i++) {
53 $this->grabChannel($k,$i);
66 function grabChannel($k,$dayoffset)
68 $cinfo = $this->channels[$k];
72 $date = $this->date + ($dayoffset * 24 * 60 * 60 );
74 $url = strftime( $cinfo['url'], $date);
77 $data = @file_get_contents($url);
79 $this->debug("NO DATA");
81 // something went wrong..
87 array("0x22", "0x09", "0x0a"),
88 array('"', "\t", "\n")
92 $hs = $cinfo['htmlstart'] === false ? false : str_replace($map[0],$map[1],$cinfo['htmlstart']);
94 $he= str_replace($map[0],$map[1],$cinfo['htmlend']);
97 list( , $data) = explode($hs,$data);
101 list($data,) = explode($he ,$data);
105 //$this->debug("DATA:".$data);
107 $method = 'parse'.$cinfo['htmlparsetype'];
109 $chid = isset($cinfo['id']) ? $cinfo['id'] : $k;
110 // for multiday html layout of atv:
111 if (!empty($cinfo['htmldaysep'])) {
112 $days = explode($cinfo['htmldaysep'], $data);
113 // kludge. = first monday of current week..
114 // loook for... <BR>2007-12-31 Mon
115 $start = preg_match('/<BR>([0-9]{4}-[0-9]{2}-[0-9]{2}) Mon/i', $odata, $matches);
116 $start = isset($matches[1]) ? $matches[1] : 0; // first monday..
119 foreach($days as $i=>$ddata) {
123 $cols = explode(',',$cinfo['htmlcols']);
124 $dn = strtotime($start) + (($i * $use_cols) * 24 * 60 * 60 );
125 $res = $this->$method($ddata,$cols,date('Y-m-d',$dn));
126 if (is_string($res)) {
131 if (!is_array($res)) {
134 $day_id = strtotime($start) + (($i * $use_cols ) * 24 * 60 * 60 );
136 if (empty($this->schedule[$chid][$day_id])) {
137 $this->schedule[$chid][$day_id] = array();
139 $this->schedule[$chid][$day_id] += $res;
146 $cols = explode(',',$cinfo['htmlcols']);
148 //print_r(array($data,$cols));
150 $this->schedule[$k][$this->date + ($dayoffset * 24 * 60 * 60 )] = $this->$method($data,$cols);
151 //print_r($this->schedule);
154 function parseTable($data,$colnames)
156 $rows = preg_split('/<tr[^>]*>/i', $data);
157 // print_r($rows);exit;
161 foreach($rows as $r) {
163 $cols = preg_split('/\<td[^>]*\>/i', trim($r));
166 //var_dump(count(array_values($cols)));
168 // print_r($cols);exit;
170 foreach($cols as $i=>$c) {
171 if (count(array_values($cols)) != count(array_values($colnames))) {
174 if (preg_match('/<table/i', $c)) {
178 $c = str_ireplace('<br>',' ', $c);
179 $c = str_ireplace(' ',' ', $c);
180 $c = str_replace("\n",' ', $c);
181 $c = str_replace("\r",' ', $c);
183 $rdata[$colnames[$i]] = trim(strip_tags($c));
187 if (count(array_values($rdata)) != count(array_values($colnames))) {
194 $this->debug(print_r($ret,true));
201 function parseJade($data, $colnames)
204 $rows = preg_split('#</li>#i', $data);
207 foreach($rows as $r) {
208 $r = str_ireplace(' ',' ', $r);
211 list($time,$r) = explode('</span>', $r, 2);
213 $rdata['hour'] = trim(strip_tags($time));
214 if (!strlen($rdata['hour'])) {
217 //list($title,$r) = explode('</em>', $r, 2);
218 list($title,$r) = explode('</p>', $r, 2);
219 $rdata['description'] = trim(strip_tags($title));
220 $rdata['description2'] = trim(strip_tags($r));
221 $rdata['day'] = $day;
229 function parseatv($data,$colnames, $day)
232 // if it's a day row..
234 $lines = explode("\n", trim($data));
235 //var_dump($lines[1]);
238 if (isset($lines[1]) && preg_match('/<div/', trim($lines[1]))) {
240 preg_match('/<BR>([0-9]{4}-[0-9]{2}-[0-9]{2})/i', $lines[1], $matches);
241 var_dump($matches[1]);
245 $rows = preg_split('/<tr[^>]*>/i', $data);
246 if ($day == '2011-10-29') {
247 //$this->debug(print_r($rows,true));
249 if ($day < date('Y-m-d')) {
250 // $this->debug("OLD DATA $day");
253 //$this->debug($day);
254 //$this->debug(print_r($rows,true));
260 foreach($rows as $r) {
262 $cols = preg_split('/<td[^>]*>/i', $r);
265 if (!isset($cols[2])) {
272 if (!preg_match('/^[0-9]+:[0-9]+/', $c)) {
275 //$this->debug("GOT HOUR: $c");
276 $rdata['hour'] = trim(array_shift(explode('<', $c)));
279 $kv = preg_split('/<br>/',$c);
282 $c = str_ireplace('<br>',' ', $c);
283 $c = str_ireplace(' ',' ', $c);
284 $c = str_replace("\n",' ', $c);
285 $c = str_replace("\r",' ', $c);
286 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
287 $rdata['description2'] = trim($c);
292 $c = str_ireplace('<br>',' ', $c);
293 $c = str_ireplace(' ',' ', $c);
294 $c = str_replace("\n",' ', $c);
295 $c = str_replace("\r",' ', $c);
296 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
297 $rdata['description'] = trim($c);
300 $rdata['day'] = $day;
306 //print_r($ret); exit;
314 function parseTableCells($data,$colnames, $day)
316 $rows = preg_split('/<tr[^>]*>/i', $data);
317 //$this->debug(print_r($rows,true));
324 foreach($rows as $r) {
326 $cols = preg_split('/<td[^>]*>/i', $r);
331 if (!preg_match('/^[0-9]+:[0-9]+\s/', $c)) {
335 $c = str_ireplace('<br>',' ', $c);
336 $c = str_ireplace(' ',' ', $c);
337 $c = str_replace("\n",' ', $c);
338 $c = str_replace("\r",' ', $c);
341 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
343 $kv = preg_split("/\s+/", $c, 2);
345 $rdata[$colnames[0]] = trim($kv[0]);
346 $rdata[$colnames[1]] = trim($kv[1]);
350 if (count(array_values($rdata)) != count(array_values($colnames))) {
353 $rdata['day'] = $day;
364 <tv generator-info-name="tv_grab_uk">
365 <channel id="bbc2.bbc.co.uk">
366 <display-name lang="en">BBC2</display-name>
368 <channel id="channel4.com">
369 <display-name lang="en">Channel 4</display-name>
372 <programme channel="bbc2.bbc.co.uk" start="20010829000500 +0100">
373 <title lang="en">The Phil Silvers Show</title>
375 Bilko claims he's had a close encounter with an alien in order
376 to be given some compassionate leave so he can visit an old
381 <programme channel="channel4.com" start="20010829095500 +0100">
382 <title lang="en">King of the Hill</title>
383 <sub-title lang="en">Meet the Propaniacs</sub-title>
385 Bobby tours with a comedy troupe who specialize in
386 propane-related mirth.
389 <actor>Mike Judge</actor>
390 <actor>Lane Smith</actor>
392 <category lang="en">animation</category>
400 //print_r($this->schedule);
401 $doc = new DomDocument('1.0', 'UTF-8');
402 $tv = $doc->createElement('tv');
403 $tv->setAttribute( 'generator-info-name','akpear_xml_tv');
404 $doc->appendChild($tv);
407 //$out = '<'.'?xml version="1.0" encoding="UTF-8"?.'>'."\n" .
408 // '<!DOCTYPE tv SYSTEM "xmltv.dtd">'."\n" ."\n" .
409 /// '<tv generator-info-name="akpear_xml_tv">'."\n";
411 foreach($this->channels as $k => $v) {
413 $chid = isset($v['id']) ? $v['id'] : $k;
414 if (isset($donec[$chid])) {
417 $donec[$chid] = true;
418 $ch = $doc->createElement('channel');
419 $ch->setAttribute('id', $chid);
420 $disp = $doc->createElement('display-name');
421 $disp->setAttribute('lang', 'en');
422 $disp->appendChild($doc->createTextNode($v['name']));
423 $ch->appendChild($disp);
424 $tv->appendChild($ch);
426 // '<channel id="'. $k .'">
427 // <display-name lang="en">'. $v['name'] .'</display-name>
430 //print_r($this->schedule);
431 foreach($this->schedule as $chan => $scheds) {
433 foreach($scheds as $day => $sched) {
440 foreach($sched as $item) {
443 $bits = explode(':', $item['hour']);
444 if ($bits[0] < $last) {
449 //var_dump($bits[0] + $hoffset);
450 $start = mktime(/*hmsmdy */
458 if ($start < strtotime(date("Y-m-d 00:00:00", strtotime('NOW - 1 DAY')))) {
461 $item['hoffset'] = $hoffset;
462 $item['hoffset_ar'] = $bits;
464 //$this->debug(print_r($item, true));
465 $start_str = date('YmdHis',$start) . ' ' . $this->config['gmtoffset'];
466 //var_dump($start_str);
467 //var_dump($this->channels);
468 $description = iconv($this->channels[$chan]['encoding'], 'UTF-8',$item['description'] .
469 (isset($item['description2']) ? (' ' . $item['description2']) : ''));
471 $this->debug(date("Y-m-d H:i - ", $start). $description);
474 $pg = $doc->createElement('programme');
476 $pg->setAttribute('channel', $chan);
477 $pg->setAttribute('start', $start_str);
479 $title = $doc->createElement('title');
480 $title->setAttribute('lang', 'zh');
481 $title->appendChild($doc->createTextNode($this->toTitle($description,$chan)));
482 $pg->appendChild($title);
484 $title = $doc->createElement('desc');
485 $title->setAttribute('lang', 'zh');
486 $title->appendChild($doc->createTextNode($description));
487 $pg->appendChild($title);
489 $tv->appendChild($pg);
490 //$out.= '<programme channel="'.$chan. '" start="'.$start_str. '">
491 // <title lang="zh">'. $this->toTitle($description) .'</title>
492 // <desc lang="zh">'. $description .'</desc>
493 // </programme>'."\n";
498 $doc->formatOutput = true;
502 return $doc->saveXML();
506 function toTitle($description, $chan)
508 // remove sponsor message.
510 @list($title, $fuldesc) = explode(">>",$description);
514 if (preg_match('/Followed\s*By/i', $title)) {
515 $bits = preg_split('/Followed\s*By/i', $title);
516 $title_pre = $bits[0] . ' Followed By ';
519 $title = preg_replace('#countdown to[a-z0-9 ]+#i', '' , $title); # NICAM Language
520 $title = preg_replace('#^(solar x|Samsung Digital)\s*#i', '' , $title); # known sponsors..
522 $title = preg_replace('#[a-z0-9 ]+(presents|special|blockbuster|movie of the month|showtime)\s*:\s*#i', '', $title);
524 $title = preg_replace('#\([a-z]+/[a-z]+\s*(|bilingual)\)#i', '' , $title); # NICAM Language
525 $title = preg_replace('#\(live\)#i', '' , $title); # live
526 $title = preg_replace('#\((s|c|l|e|cs|es|ecs|can|ce)[*]*\)#i', '' , $title); # Subtitle
527 $title = preg_replace('#\((pg\d*\w*)\)#i', '' , $title); # Adult
528 $title = preg_replace('#\(r\)#i', '' , $title); # Repeated
529 $title = trim($title, '/');
530 $title = trim($title);
532 $ret = $title_pre . $title;
534 // if (!strlen($ret)) {
535 // die("got $description : nothing to return");
537 if (in_array($chan, array('tvbpearl.hk', 'english.atvworld.hk'))) {
538 $enonly = preg_replace('#^[^a-z0-9]+#i', '', $title);
539 if (strlen($enonly) > 10) {
545 return $title_pre . $title;
552 if (empty($this->config['debug'])) {
564 $x->start($_SERVER['argv'][1]);