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 $this->schedule[$chid][strtotime($start) + (($i * $use_cols ) * 24 * 60 * 60 )] = $res;
138 $cols = explode(',',$cinfo['htmlcols']);
140 //print_r(array($data,$cols));
142 $this->schedule[$k][$this->date + ($dayoffset * 24 * 60 * 60 )] = $this->$method($data,$cols);
143 //print_r($this->schedule);
146 function parseTable($data,$colnames)
148 $rows = preg_split('/<tr[^>]*>/i', $data);
149 // print_r($rows);exit;
153 foreach($rows as $r) {
155 $cols = preg_split('/\<td[^>]*\>/i', trim($r));
158 //var_dump(count(array_values($cols)));
160 // print_r($cols);exit;
162 foreach($cols as $i=>$c) {
163 if (count(array_values($cols)) != count(array_values($colnames))) {
166 if (preg_match('/<table/i', $c)) {
170 $c = str_ireplace('<br>',' ', $c);
171 $c = str_ireplace(' ',' ', $c);
172 $c = str_replace("\n",' ', $c);
173 $c = str_replace("\r",' ', $c);
175 $rdata[$colnames[$i]] = trim(strip_tags($c));
179 if (count(array_values($rdata)) != count(array_values($colnames))) {
186 $this->debug(print_r($ret,true));
193 function parseJade($data, $colnames)
196 $rows = preg_split('#</li>#i', $data);
199 foreach($rows as $r) {
200 $r = str_ireplace(' ',' ', $r);
203 list($time,$r) = explode('</span>', $r, 2);
205 $rdata['hour'] = trim(strip_tags($time));
206 if (!strlen($rdata['hour'])) {
209 //list($title,$r) = explode('</em>', $r, 2);
210 list($title,$r) = explode('</p>', $r, 2);
211 $rdata['description'] = trim(strip_tags($title));
212 $rdata['description2'] = trim(strip_tags($r));
213 $rdata['day'] = $day;
221 function parseatv($data,$colnames, $day)
224 // if it's a day row..
226 $lines = explode("\n", trim($data));
227 //var_dump($lines[1]);
229 if (preg_match('/<div/', trim($lines[1]))) {
231 preg_match('/<BR>([0-9]{4}-[0-9]{2}-[0-9]{2})/i', $lines[1], $matches);
232 var_dump($matches[1]);
236 $rows = preg_split('/<tr[^>]*>/i', $data);
237 if ($day == '2011-10-29') {
238 //$this->debug(print_r($rows,true));
240 if ($day < date('Y-m-d')) {
243 //$this->debug($day);
244 //$this->debug(print_r($rows,true));
250 foreach($rows as $r) {
252 $cols = preg_split('/<td[^>]*>/i', $r);
255 if (!isset($cols[2])) {
262 if (!preg_match('/^[0-9]+:[0-9]+/', $c)) {
265 //$this->debug("GOT HOUR: $c");
266 $rdata['hour'] = trim(array_shift(explode('<', $c)));
269 $kv = preg_split('/<br>/',$c);
272 $c = str_ireplace('<br>',' ', $c);
273 $c = str_ireplace(' ',' ', $c);
274 $c = str_replace("\n",' ', $c);
275 $c = str_replace("\r",' ', $c);
276 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
277 $rdata['description2'] = trim($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['description'] = trim($c);
290 $rdata['day'] = $day;
296 //print_r($ret); exit;
304 function parseTableCells($data,$colnames, $day)
306 $rows = preg_split('/<tr[^>]*>/i', $data);
307 //$this->debug(print_r($rows,true));
314 foreach($rows as $r) {
316 $cols = preg_split('/<td[^>]*>/i', $r);
321 if (!preg_match('/^[0-9]+:[0-9]+\s/', $c)) {
325 $c = str_ireplace('<br>',' ', $c);
326 $c = str_ireplace(' ',' ', $c);
327 $c = str_replace("\n",' ', $c);
328 $c = str_replace("\r",' ', $c);
331 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
333 $kv = preg_split("/\s+/", $c, 2);
335 $rdata[$colnames[0]] = trim($kv[0]);
336 $rdata[$colnames[1]] = trim($kv[1]);
340 if (count(array_values($rdata)) != count(array_values($colnames))) {
343 $rdata['day'] = $day;
354 <tv generator-info-name="tv_grab_uk">
355 <channel id="bbc2.bbc.co.uk">
356 <display-name lang="en">BBC2</display-name>
358 <channel id="channel4.com">
359 <display-name lang="en">Channel 4</display-name>
362 <programme channel="bbc2.bbc.co.uk" start="20010829000500 +0100">
363 <title lang="en">The Phil Silvers Show</title>
365 Bilko claims he's had a close encounter with an alien in order
366 to be given some compassionate leave so he can visit an old
371 <programme channel="channel4.com" start="20010829095500 +0100">
372 <title lang="en">King of the Hill</title>
373 <sub-title lang="en">Meet the Propaniacs</sub-title>
375 Bobby tours with a comedy troupe who specialize in
376 propane-related mirth.
379 <actor>Mike Judge</actor>
380 <actor>Lane Smith</actor>
382 <category lang="en">animation</category>
390 //print_r($this->schedule);
391 $doc = new DomDocument('1.0', 'UTF-8');
392 $tv = $doc->createElement('tv');
393 $tv->setAttribute( 'generator-info-name','akpear_xml_tv');
394 $doc->appendChild($tv);
397 //$out = '<'.'?xml version="1.0" encoding="UTF-8"?.'>'."\n" .
398 // '<!DOCTYPE tv SYSTEM "xmltv.dtd">'."\n" ."\n" .
399 /// '<tv generator-info-name="akpear_xml_tv">'."\n";
401 foreach($this->channels as $k => $v) {
403 $chid = isset($v['id']) ? $v['id'] : $k;
404 if (isset($donec[$chid])) {
407 $donec[$chid] = true;
408 $ch = $doc->createElement('channel');
409 $ch->setAttribute('id', $chid);
410 $disp = $doc->createElement('display-name');
411 $disp->setAttribute('lang', 'en');
412 $disp->appendChild($doc->createTextNode($v['name']));
413 $ch->appendChild($disp);
414 $tv->appendChild($ch);
416 // '<channel id="'. $k .'">
417 // <display-name lang="en">'. $v['name'] .'</display-name>
420 //print_r($this->schedule);
421 foreach($this->schedule as $chan => $scheds) {
423 foreach($scheds as $day => $sched) {
430 foreach($sched as $item) {
433 $bits = explode(':', $item['hour']);
434 if ($bits[0] < $last) {
439 //var_dump($bits[0] + $hoffset);
440 $start = mktime(/*hmsmdy */
448 if ($start < strtotime(date("Y-m-d 00:00:00", strtotime('NOW - 1 DAY')))) {
451 $item['hoffset'] = $hoffset;
452 $item['hoffset_ar'] = $bits;
454 //$this->debug(print_r($item, true));
455 $start_str = date('YmdHis',$start) . ' ' . $this->config['gmtoffset'];
456 //var_dump($start_str);
457 //var_dump($this->channels);
458 $description = iconv($this->channels[$chan]['encoding'], 'UTF-8',$item['description'] .
459 (isset($item['description2']) ? (' ' . $item['description2']) : ''));
461 $this->debug(date("Y-m-d H:i - ", $start). $description);
464 $pg = $doc->createElement('programme');
466 $pg->setAttribute('channel', $chan);
467 $pg->setAttribute('start', $start_str);
469 $title = $doc->createElement('title');
470 $title->setAttribute('lang', 'zh');
471 $title->appendChild($doc->createTextNode($this->toTitle($description,$chan)));
472 $pg->appendChild($title);
474 $title = $doc->createElement('desc');
475 $title->setAttribute('lang', 'zh');
476 $title->appendChild($doc->createTextNode($description));
477 $pg->appendChild($title);
479 $tv->appendChild($pg);
480 //$out.= '<programme channel="'.$chan. '" start="'.$start_str. '">
481 // <title lang="zh">'. $this->toTitle($description) .'</title>
482 // <desc lang="zh">'. $description .'</desc>
483 // </programme>'."\n";
488 $doc->formatOutput = true;
492 return $doc->saveXML();
496 function toTitle($description, $chan)
498 // remove sponsor message.
500 list($title, $fuldesc) = explode(">>",$description);
504 if (preg_match('/Followed\s*By/i', $title)) {
505 $bits = preg_split('/Followed\s*By/i', $title);
506 $title_pre = $bits[0] . ' Followed By ';
509 $title = preg_replace('#countdown to[a-z0-9 ]+#i', '' , $title); # NICAM Language
510 $title = preg_replace('#^(solar x|Samsung Digital)\s*#i', '' , $title); # known sponsors..
512 $title = preg_replace('#[a-z0-9 ]+(presents|special|blockbuster|movie of the month|showtime)\s*:\s*#i', '', $title);
514 $title = preg_replace('#\([a-z]+/[a-z]+\s*(|bilingual)\)#i', '' , $title); # NICAM Language
515 $title = preg_replace('#\(live\)#i', '' , $title); # live
516 $title = preg_replace('#\((s|c|l|e|cs|es|ecs|can|ce)[*]*\)#i', '' , $title); # Subtitle
517 $title = preg_replace('#\((pg\d*\w*)\)#i', '' , $title); # Adult
518 $title = preg_replace('#\(r\)#i', '' , $title); # Repeated
519 $title = trim($title, '/');
520 $title = trim($title);
522 $ret = $title_pre . $title;
524 // if (!strlen($ret)) {
525 // die("got $description : nothing to return");
527 if (in_array($chan, array('tvbpearl.hk', 'english.atvworld.hk'))) {
528 $enonly = preg_replace('#^[^a-z0-9]+#i', '', $title);
529 if (strlen($enonly) > 10) {
535 return $title_pre . $title;
542 if (empty($this->config['debug'])) {
554 $x->start($_SERVER['argv'][1]);