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 = $matches[1]; // first monday..
117 foreach($days as $i=>$ddata) {
118 $cols = explode(',',$cinfo['htmlcols']);
119 $dn = strtotime($start) + (($i ) * 24 * 60 * 60 );
120 $this->schedule[$chid][strtotime($start) + (($i ) * 24 * 60 * 60 )] = $this->$method($ddata,$cols,date('Y-m-d',$dn));
127 $cols = explode(',',$cinfo['htmlcols']);
129 //print_r(array($data,$cols));
131 $this->schedule[$k][$this->date + ($dayoffset * 24 * 60 * 60 )] = $this->$method($data,$cols);
132 //print_r($this->schedule);
135 function parseTable($data,$colnames)
137 $rows = preg_split('/<tr[^>]*>/i', $data);
138 // print_r($rows);exit;
142 foreach($rows as $r) {
144 $cols = preg_split('/\<td[^>]*\>/i', trim($r));
147 //var_dump(count(array_values($cols)));
149 // print_r($cols);exit;
151 foreach($cols as $i=>$c) {
152 if (count(array_values($cols)) != count(array_values($colnames))) {
155 if (preg_match('/<table/i', $c)) {
159 $c = str_ireplace('<br>',' ', $c);
160 $c = str_ireplace(' ',' ', $c);
161 $c = str_replace("\n",' ', $c);
162 $c = str_replace("\r",' ', $c);
164 $rdata[$colnames[$i]] = trim(strip_tags($c));
168 if (count(array_values($rdata)) != count(array_values($colnames))) {
175 $this->debug(print_r($ret,true));
182 function parseJade($data, $colnames)
185 $rows = preg_split('#</li>#i', $data);
188 foreach($rows as $r) {
189 $r = str_ireplace(' ',' ', $r);
192 list($time,$r) = explode('</span>', $r, 2);
194 $rdata['hour'] = trim(strip_tags($time));
195 if (!strlen($rdata['hour'])) {
198 //list($title,$r) = explode('</em>', $r, 2);
199 list($title,$r) = explode('</p>', $r, 2);
200 $rdata['description'] = trim(strip_tags($title));
201 $rdata['description2'] = trim(strip_tags($r));
202 $rdata['day'] = $day;
210 function parseatv($data,$colnames, $day)
214 $rows = preg_split('/<tr[^>]*>/i', $data);
215 //$this->debug(print_r($rows,true));
221 foreach($rows as $r) {
223 $cols = preg_split('/<td[^>]*>/i', $r);
231 if (!preg_match('/^[0-9]+:[0-9]+/', $c)) {
235 $rdata['time'] = trim(array_shift(explode('<', $c)));
238 $kv = preg_split('/<br>/',$c);
241 $c = str_ireplace('<br>',' ', $c);
242 $c = str_ireplace(' ',' ', $c);
243 $c = str_replace("\n",' ', $c);
244 $c = str_replace("\r",' ', $c);
245 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
246 $rdata['description2'] = trim($c);
251 $c = str_ireplace('<br>',' ', $c);
252 $c = str_ireplace(' ',' ', $c);
253 $c = str_replace("\n",' ', $c);
254 $c = str_replace("\r",' ', $c);
255 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
256 $rdata['description'] = trim($c);
259 $rdata['day'] = $day;
265 //print_r($ret); exit;
273 function parseTableCells($data,$colnames, $day)
275 $rows = preg_split('/<tr[^>]*>/i', $data);
276 //$this->debug(print_r($rows,true));
283 foreach($rows as $r) {
285 $cols = preg_split('/<td[^>]*>/i', $r);
290 if (!preg_match('/^[0-9]+:[0-9]+\s/', $c)) {
294 $c = str_ireplace('<br>',' ', $c);
295 $c = str_ireplace(' ',' ', $c);
296 $c = str_replace("\n",' ', $c);
297 $c = str_replace("\r",' ', $c);
300 $c = preg_replace('/\<[^>]+\>/', ' ', $c);
302 $kv = preg_split("/\s+/", $c, 2);
304 $rdata[$colnames[0]] = trim($kv[0]);
305 $rdata[$colnames[1]] = trim($kv[1]);
309 if (count(array_values($rdata)) != count(array_values($colnames))) {
312 $rdata['day'] = $day;
323 <tv generator-info-name="tv_grab_uk">
324 <channel id="bbc2.bbc.co.uk">
325 <display-name lang="en">BBC2</display-name>
327 <channel id="channel4.com">
328 <display-name lang="en">Channel 4</display-name>
331 <programme channel="bbc2.bbc.co.uk" start="20010829000500 +0100">
332 <title lang="en">The Phil Silvers Show</title>
334 Bilko claims he's had a close encounter with an alien in order
335 to be given some compassionate leave so he can visit an old
340 <programme channel="channel4.com" start="20010829095500 +0100">
341 <title lang="en">King of the Hill</title>
342 <sub-title lang="en">Meet the Propaniacs</sub-title>
344 Bobby tours with a comedy troupe who specialize in
345 propane-related mirth.
348 <actor>Mike Judge</actor>
349 <actor>Lane Smith</actor>
351 <category lang="en">animation</category>
359 //print_r($this->schedule);
360 $doc = new DomDocument('1.0', 'UTF-8');
361 $tv = $doc->createElement('tv');
362 $tv->setAttribute( 'generator-info-name','akpear_xml_tv');
363 $doc->appendChild($tv);
366 //$out = '<'.'?xml version="1.0" encoding="UTF-8"?.'>'."\n" .
367 // '<!DOCTYPE tv SYSTEM "xmltv.dtd">'."\n" ."\n" .
368 /// '<tv generator-info-name="akpear_xml_tv">'."\n";
370 foreach($this->channels as $k => $v) {
372 $chid = isset($v['id']) ? $v['id'] : $k;
373 if (isset($donec[$chid])) {
376 $donec[$chid] = true;
377 $ch = $doc->createElement('channel');
378 $ch->setAttribute('id', $chid);
379 $disp = $doc->createElement('display-name');
380 $disp->setAttribute('lang', 'en');
381 $disp->appendChild($doc->createTextNode($v['name']));
382 $ch->appendChild($disp);
383 $tv->appendChild($ch);
385 // '<channel id="'. $k .'">
386 // <display-name lang="en">'. $v['name'] .'</display-name>
389 //print_r($this->schedule);
390 foreach($this->schedule as $chan => $scheds) {
392 foreach($scheds as $day => $sched) {
399 foreach($sched as $item) {
401 $bits = explode(':', $item['hour']);
402 if ($bits[0] < $last) {
406 //var_dump($bits[0] + $hoffset);
407 $start = mktime(/*hmsmdy */
416 $start_str = date('YmdHis',$start) . ' ' . $this->config['gmtoffset'];
417 //var_dump($start_str);
418 //var_dump($this->channels);
419 $description = iconv($this->channels[$chan]['encoding'], 'UTF-8',$item['description'] .
420 (isset($item['description2']) ? (' ' . $item['description2']) : ''));
422 $pg = $doc->createElement('programme');
424 $pg->setAttribute('channel', $chan);
425 $pg->setAttribute('start', $start_str);
427 $title = $doc->createElement('title');
428 $title->setAttribute('lang', 'zh');
429 $title->appendChild($doc->createTextNode($this->toTitle($description,$chan)));
430 $pg->appendChild($title);
432 $title = $doc->createElement('desc');
433 $title->setAttribute('lang', 'zh');
434 $title->appendChild($doc->createTextNode($description));
435 $pg->appendChild($title);
437 $tv->appendChild($pg);
438 //$out.= '<programme channel="'.$chan. '" start="'.$start_str. '">
439 // <title lang="zh">'. $this->toTitle($description) .'</title>
440 // <desc lang="zh">'. $description .'</desc>
441 // </programme>'."\n";
446 $doc->formatOutput = true;
450 return $doc->saveXML();
454 function toTitle($description, $chan)
456 // remove sponsor message.
458 list($title, $fuldesc) = explode(">>",$description);
462 if (preg_match('/Followed\s*By/i', $title)) {
463 $bits = preg_split('/Followed\s*By/i', $title);
464 $title_pre = $bits[0] . ' Followed By ';
467 $title = preg_replace('#countdown to[a-z0-9 ]+#i', '' , $title); # NICAM Language
468 $title = preg_replace('#^(solar x|Samsung Digital)\s*#i', '' , $title); # known sponsors..
470 $title = preg_replace('#[a-z0-9 ]+(presents|special|blockbuster|movie of the month|showtime)\s*:\s*#i', '', $title);
472 $title = preg_replace('#\([a-z]+/[a-z]+\s*(|bilingual)\)#i', '' , $title); # NICAM Language
473 $title = preg_replace('#\(live\)#i', '' , $title); # live
474 $title = preg_replace('#\((s|c|l|e|cs|es|ecs|can|ce)[*]*\)#i', '' , $title); # Subtitle
475 $title = preg_replace('#\((pg\d*\w*)\)#i', '' , $title); # Adult
476 $title = preg_replace('#\(r\)#i', '' , $title); # Repeated
477 $title = trim($title, '/');
478 $title = trim($title);
480 $ret = $title_pre . $title;
482 // if (!strlen($ret)) {
483 // die("got $description : nothing to return");
485 if (in_array($chan, array('tvbpearl.hk', 'english.atvworld.hk'))) {
486 $enonly = preg_replace('#^[^a-z0-9]+#i', '', $title);
487 if (strlen($enonly) > 10) {
493 return $title_pre . $title;
500 if (empty($this->config['debug'])) {
512 $x->start($_SERVER['argv'][1]);