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