Mercurial > epgrec.yaz
comparison storeProgram.php @ 111:3bed74eca373
change: EPG??????????????????????
| author | epgrec@park.mda.or.jp <yoneda@recorder.localnet.mda.or.jp> |
|---|---|
| date | Sat, 13 Mar 2010 16:45:29 +0900 |
| parents | d9b9d1d903a1 |
| children | b3ba2658a228 |
comparison
equal
deleted
inserted
replaced
| 108:4bd07b03c694 | 111:3bed74eca373 |
|---|---|
| 1 #!/usr/bin/php | 1 #!/usr/bin/php |
| 2 <?php | 2 <?php |
| 3 $script_path = dirname( __FILE__ ); | |
| 4 chdir( $script_path ); | |
| 5 include_once( $script_path . '/config.php'); | |
| 3 | 6 |
| 4 $type = $argv[1]; // BS CS GR | 7 $type = $argv[1]; // BS CS GR |
| 5 $file = $argv[2]; // XMLファイル | 8 $file = $argv[2]; // XMLファイル |
| 6 | |
| 7 | 9 |
| 8 // SIGTERMシグナル | 10 // SIGTERMシグナル |
| 9 function handler( $signo = 0 ) { | 11 function handler( $signo = 0 ) { |
| 10 global $file; | 12 global $file; |
| 11 if( file_exists( $file ) ) { | 13 if( file_exists( $file ) ) { |
| 22 if( pcntl_fork() != 0 ) | 24 if( pcntl_fork() != 0 ) |
| 23 exit; | 25 exit; |
| 24 pcntl_signal(SIGTERM, "handler"); | 26 pcntl_signal(SIGTERM, "handler"); |
| 25 } | 27 } |
| 26 | 28 |
| 27 | |
| 28 // デーモン化 | 29 // デーモン化 |
| 29 daemon(); | 30 daemon(); |
| 30 // プライオリティ低に | 31 // プライオリティ低に |
| 31 pcntl_setpriority(20); | 32 pcntl_setpriority(20); |
| 32 | 33 |
| 33 include_once('config.php'); | |
| 34 include_once( INSTALL_PATH . '/DBRecord.class.php' ); | 34 include_once( INSTALL_PATH . '/DBRecord.class.php' ); |
| 35 include_once( INSTALL_PATH . '/Reservation.class.php' ); | 35 include_once( INSTALL_PATH . '/Reservation.class.php' ); |
| 36 include_once( INSTALL_PATH . '/Keyword.class.php' ); | 36 include_once( INSTALL_PATH . '/Keyword.class.php' ); |
| 37 include_once( INSTALL_PATH . '/Settings.class.php' ); | 37 include_once( INSTALL_PATH . '/Settings.class.php' ); |
| 38 include_once( INSTALL_PATH . '/storeProgram.inc.php' ); | |
| 38 | 39 |
| 39 $settings = Settings::factory(); | 40 $settings = Settings::factory(); |
| 40 | 41 |
| 41 if( file_exists( $file ) ) { | 42 if( file_exists( $file ) ) { |
| 42 storeProgram( $type, $file ); | 43 storeProgram( $type, $file ); |
| 43 @unlink( $file ); | 44 @unlink( $file ); |
| 44 } | 45 } |
| 45 | 46 garbageClean(); // 不要プログラム削除 |
| 46 // 不要なプログラムの削除 | 47 doKeywordReservation(); // キーワード予約 |
| 47 // 8日以上前のプログラムを消す | |
| 48 $arr = array(); | |
| 49 $arr = DBRecord::createRecords( PROGRAM_TBL, "WHERE endtime < subdate( now(), 8 )" ); | |
| 50 foreach( $arr as $val ) $val->delete(); | |
| 51 | |
| 52 // 8日以上先のデータがあれば消す | |
| 53 $arr = array(); | |
| 54 $arr = DBRecord::createRecords( PROGRAM_TBL, "WHERE starttime > adddate( now(), 8 )" ); | |
| 55 foreach( $arr as $val ) $val->delete(); | |
| 56 | |
| 57 // キーワード自動録画予約 | |
| 58 $arr = array(); | |
| 59 $arr = Keyword::createKeywords(); | |
| 60 foreach( $arr as $val ) { | |
| 61 try { | |
| 62 $val->reservation(); | |
| 63 } | |
| 64 catch( Exception $e ) { | |
| 65 // 無視 | |
| 66 } | |
| 67 } | |
| 68 exit(); | |
| 69 | |
| 70 function storeProgram( $type, $xmlfile ) { | |
| 71 global $BS_CHANNEL_MAP, $GR_CHANNEL_MAP, $CS_CHANNEL_MAP; | |
| 72 // チャンネルマップファイルの準備 | |
| 73 $map = array(); | |
| 74 if( $type == "BS" ) $map = $BS_CHANNEL_MAP; | |
| 75 else if( $type == "GR") $map = $GR_CHANNEL_MAP; | |
| 76 else if( $type == "CS") $map = $CS_CHANNEL_MAP; | |
| 77 | |
| 78 // XML parse | |
| 79 $xml = @simplexml_load_file( $xmlfile ); | |
| 80 if( $xml === false ) { | |
| 81 return; // XMLが読み取れないなら何もしない | |
| 82 } | |
| 83 // channel抽出 | |
| 84 foreach( $xml->channel as $ch ) { | |
| 85 $disc = $ch['id']; | |
| 86 try { | |
| 87 // チャンネルデータを探す | |
| 88 $num = DBRecord::countRecords( CHANNEL_TBL , "WHERE channel_disc = '" . $disc ."'" ); | |
| 89 if( $num == 0 ) { | |
| 90 if( array_key_exists( "$disc", $map ) ) { | |
| 91 // チャンネルデータがないなら新規作成 | |
| 92 $rec = new DBRecord( CHANNEL_TBL ); | |
| 93 $rec->type = $type; | |
| 94 $rec->channel = $map["$disc"]; | |
| 95 $rec->channel_disc = $disc; | |
| 96 $rec->name = $ch->{'display-name'}; | |
| 97 } | |
| 98 } | |
| 99 else { | |
| 100 // 存在した場合も、とりあえずチャンネル名は更新する | |
| 101 $rec = new DBRecord(CHANNEL_TBL, "channel_disc", $disc ); | |
| 102 $rec->name = $ch->{'display-name'}; | |
| 103 } | |
| 104 } | |
| 105 catch( Exception $e ) { | |
| 106 // 無視 | |
| 107 } | |
| 108 } | |
| 109 // channel 終了 | |
| 110 | |
| 111 // programme 取得 | |
| 112 | |
| 113 foreach( $xml->programme as $program ) { | |
| 114 $channel_disc = $program['channel']; | |
| 115 if( ! array_key_exists( "$channel_disc", $map ) ) continue; | |
| 116 | |
| 117 $channel = $map["$channel_disc"]; | |
| 118 $starttime = str_replace(" +0900", '', $program['start'] ); | |
| 119 $endtime = str_replace( " +0900", '', $program['stop'] ); | |
| 120 $title = $program->title; | |
| 121 $desc = $program->desc; | |
| 122 $cat_ja = ""; | |
| 123 $cat_en = ""; | |
| 124 foreach( $program->category as $cat ) { | |
| 125 if( $cat['lang'] == "ja_JP" ) $cat_ja = $cat; | |
| 126 if( $cat['lang'] == "en" ) $cat_en = $cat; | |
| 127 } | |
| 128 $program_disc = md5( $channel_disc . $starttime . $endtime ); | |
| 129 // printf( "%s %s %s %s %s %s %s \n", $program_disc, $channel, $starttime, $endtime, $title, $desc, $cat_ja ); | |
| 130 try { | |
| 131 // カテゴリを処理する | |
| 132 $category_disc = md5( $cat_ja . $cat_en ); | |
| 133 $num = DBRecord::countRecords(CATEGORY_TBL, "WHERE category_disc = '".$category_disc."'" ); | |
| 134 $cat_rec = null; | |
| 135 if( $num == 0 ) { | |
| 136 // 新規カテゴリの追加 | |
| 137 $cat_rec = new DBRecord( CATEGORY_TBL ); | |
| 138 $cat_rec->name_jp = $cat_ja; | |
| 139 $cat_rec->name_en = $cat_en; | |
| 140 $cat_rec->category_disc = $category_disc; | |
| 141 } | |
| 142 else | |
| 143 $cat_rec = new DBRecord(CATEGORY_TBL, "category_disc" , $category_disc ); | |
| 144 // | |
| 145 $channel_rec = new DBRecord(CHANNEL_TBL, "channel_disc", $channel_disc ); | |
| 146 $num = DBRecord::countRecords(PROGRAM_TBL, "WHERE program_disc = '".$program_disc."'" ); | |
| 147 if( $num == 0 ) { | |
| 148 // 新規番組 | |
| 149 // 重複チェック 同時間帯にある番組 | |
| 150 $options = "WHERE channel_disc = '".$channel_disc."' ". | |
| 151 "AND starttime < '". $endtime ."' AND endtime > '".$starttime."'"; | |
| 152 $battings = DBRecord::countRecords(PROGRAM_TBL, $options ); | |
| 153 if( $battings > 0 ) { | |
| 154 // 重複発生=おそらく放映時間の変更 | |
| 155 $records = DBRecord::createRecords(PROGRAM_TBL, $options ); | |
| 156 foreach( $records as $rec ) { | |
| 157 // 自動録画予約された番組は放映時間変更と同時にいったん削除する | |
| 158 try { | |
| 159 $reserve = new DBRecord(RESERVE_TBL, "program_id", $rec->id ); | |
| 160 if( $reserve->autorec ) { | |
| 161 Reservation::cancel( $reserve->id ); | |
| 162 } | |
| 163 } | |
| 164 catch( Exception $e ) { | |
| 165 //無視 | |
| 166 } | |
| 167 // 番組削除 | |
| 168 $rec->delete(); | |
| 169 } | |
| 170 } | |
| 171 // // | |
| 172 $rec = new DBRecord( PROGRAM_TBL ); | |
| 173 $rec->channel_disc = $channel_disc; | |
| 174 $rec->channel_id = $channel_rec->id; | |
| 175 $rec->type = $type; | |
| 176 $rec->channel = $channel_rec->channel; | |
| 177 $rec->title = $title; | |
| 178 $rec->description = $desc; | |
| 179 $rec->category_id = $cat_rec->id; | |
| 180 $rec->starttime = $starttime; | |
| 181 $rec->endtime = $endtime; | |
| 182 $rec->program_disc = $program_disc; | |
| 183 } | |
| 184 else { | |
| 185 // 番組内容更新 | |
| 186 $rec = new DBRecord( PROGRAM_TBL, "program_disc", $program_disc ); | |
| 187 $rec->title = $title; | |
| 188 $rec->description = $desc; | |
| 189 $rec->category_id = $cat_rec->id; | |
| 190 } | |
| 191 } | |
| 192 catch(Exception $e) { | |
| 193 exit( $e->getMessage() ); | |
| 194 } | |
| 195 } | |
| 196 } | |
| 197 ?> | 48 ?> |
