OSDN Git Service

mod: EPGの更新と同時に予約情報を更新するように変更、ログビューアの追加
[epgrec/epgrec.git] / getepg.php
1 #!/usr/bin/php
2 <?php
3   $script_path = dirname( __FILE__ );
4   chdir( $script_path );
5   include_once( $script_path . '/config.php');
6   include_once( INSTALL_PATH . '/DBRecord.class.php' );
7   include_once( INSTALL_PATH . '/Reservation.class.php' );
8   include_once( INSTALL_PATH . '/Keyword.class.php' );
9   include_once( INSTALL_PATH . '/Settings.class.php' );
10   include_once( INSTALL_PATH . '/storeProgram.inc.php' );
11   include_once( INSTALL_PATH . '/recLog.inc.php' );
12   
13   // 後方互換性
14   if( ! defined( "BS_EPG_CHANNEL" )  ) define( "BS_EPG_CHANNEL",  "211"  );
15   if( ! defined( "CS1_EPG_CHANNEL" ) ) define( "CS1_EPG_CHANNEL", "CS8"  );
16   if( ! defined( "CS2_EPG_CHANNEL" ) ) define( "CS2_EPG_CHANNEL", "CS24" );
17   
18   function check_file( $file ) {
19         // ファイルがないなら無問題
20         if( ! file_exists( $file ) ) return true;
21         
22         // 1時間以上前のファイルなら削除してやり直す
23         if( (time() - filemtime( $file )) > 3600 ) {
24                 @unlink( $file );
25                 return true;
26         }
27         
28         return false;
29   }
30   
31   // 並列化が可能か執念深く調べる
32   $use_para = false;
33   $use_para = (function_exists( "pcntl_fork" ) && function_exists( "posix_setsid" ) && function_exists( "pcntl_signal" ) && function_exists("pcntl_setpriority"));
34   if( ! $use_para ) {
35         reclog("getepg:: 並列実行が行えないPHP環境です" );
36   }
37   else {
38         reclog("getepg:: 並列実行を使用します" );
39   }
40   
41   $settings = Settings::factory();
42   
43   $temp_xml_bs  = $settings->temp_xml."_bs";
44   $temp_xml_cs1 = $settings->temp_xml."_cs1";
45   $temp_xml_cs2 = $settings->temp_xml."_cs2";
46   $temp_xml_gr  = $settings->temp_xml."_gr";
47   
48   if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
49   
50   // BSを処理する
51   if( $settings->bs_tuners != 0 ) {
52         // 録画重複チェック
53         $num = DBRecord::countRecords(  RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
54         if( ($num == 0) && check_file($temp_xml_bs) ) {
55                 $cmdline = "CHANNEL=".BS_EPG_CHANNEL." DURATION=180 TYPE=BS TUNER=0 MODE=0 OUTPUT=".$settings->temp_data." ".DO_RECORD . " >/dev/null 2>&1";
56                 exec( $cmdline );
57                 $cmdline = $settings->epgdump." /BS ".$settings->temp_data." ".$temp_xml_bs;
58                 exec( $cmdline );
59                 if( $use_para ) {
60                         $cmdline = INSTALL_PATH."/storeProgram.php BS ".$temp_xml_bs." >/dev/null 2>&1 &";
61                         exec( $cmdline );
62                 }
63                 else {
64                         storeProgram( "BS", $temp_xml_bs );
65                         if( file_exists( $temp_xml_bs ) ) @unlink( $temp_xml_bs );
66                 }
67                 if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
68         }
69
70         // CS
71         if ($settings->cs_rec_flg != 0) {
72                 $num = DBRecord::countRecords(  RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
73                 if( ($num == 0) && check_file($temp_xml_cs1) ) {
74                         $cmdline = "CHANNEL=".CS1_EPG_CHANNEL." DURATION=120 TYPE=CS TUNER=0 MODE=0 OUTPUT=".$settings->temp_data." ".DO_RECORD . " >/dev/null 2>&1";
75                         exec( $cmdline );
76                         $cmdline = $settings->epgdump." /CS ".$settings->temp_data." ".$temp_xml_cs1;
77                         exec( $cmdline );
78                         if( $use_para ) {
79                                 $cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs1." >/dev/null 2>&1 &";
80                                 exec( $cmdline );
81                         }
82                         else {
83                                 storeProgram( "CS", $temp_xml_cs1 );
84                                 if( file_exists( $temp_xml_cs1 ) ) @unlink( $temp_xml_cs1 );
85                         }
86                         if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
87                 }
88                 $num = DBRecord::countRecords(  RESERVE_TBL, "WHERE complete = '0' AND (type = 'BS' OR type = 'CS') AND endtime > now() AND starttime < addtime( now(), '00:03:05')" );
89                 if( ($num == 0) && check_file($temp_xml_cs2) ) {
90                         $cmdline = "CHANNEL=".CS2_EPG_CHANNEL." DURATION=120 TYPE=CS TUNER=0 MODE=0 OUTPUT=".$settings->temp_data." ".DO_RECORD . " >/dev/null 2>&1";
91                         exec( $cmdline );
92                         $cmdline = $settings->epgdump." /CS ".$settings->temp_data." ".$temp_xml_cs2;
93                         exec( $cmdline );
94                         if( $use_para ) {
95                                 $cmdline = INSTALL_PATH."/storeProgram.php CS ".$temp_xml_cs2." >/dev/null 2>&1 &";
96                                 exec( $cmdline );
97                         }
98                         else {
99                                 storeProgram( "CS", $temp_xml_cs2 );
100                                 if( file_exists( $temp_xml_cs2 ) ) @unlink( $temp_xml_cs2 );
101                         }
102                         if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
103                 }
104         }
105   }
106   
107   // 地上波を処理する
108   if( $settings->gr_tuners != 0 ) {
109         foreach( $GR_CHANNEL_MAP as $key=>$value ){
110                 // 録画重複チェック
111                 $num = DBRecord::countRecords(  RESERVE_TBL, "WHERE complete = '0' AND type = 'GR' AND endtime > now() AND starttime < addtime( now(), '00:01:10')" );
112                 if( ($num == 0) && check_file($temp_xml_gr.$value."") ) {
113                         $cmdline = "CHANNEL=".$value." DURATION=60 TYPE=GR TUNER=0 MODE=0 OUTPUT=".$settings->temp_data." ".DO_RECORD . " >/dev/null 2>&1";
114                         exec( $cmdline );
115                         $cmdline = $settings->epgdump." ".$key." ".$settings->temp_data." ".$temp_xml_gr.$value."";
116                         exec( $cmdline );
117                         if( $use_para ) {
118                                 $cmdline = INSTALL_PATH."/storeProgram.php GR ".$temp_xml_gr.$value." >/dev/null 2>&1 &";
119                                 exec( $cmdline );
120                         }
121                         else {
122                                 storeProgram( "GR", $temp_xml_gr.$value );
123                                 if( file_exists( $temp_xml_gr.$value ) ) @unlink( $temp_xml_gr.$value );
124                         }
125                         if( file_exists( $settings->temp_data ) ) @unlink( $settings->temp_data );
126                 }
127         }
128   }
129   // 並列ならこれで終わりにする
130   if( $use_para ) exit();
131   
132   garbageClean();                       //  不要プログラム削除
133   doKeywordReservation();       // キーワード予約
134   exit();
135 ?>