forked from mlwmlw/pcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.php
291 lines (268 loc) · 9.4 KB
/
parse.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
require 'vendor/autoload.php';
#$tender = get_tender('3.13.31.81', '3500500072');
if(isset($argv[1]))
$start = $argv[1];
else
$start = date('Ymd');//"20160101";
if(isset($argv[2]))
$range = $argv[2];
else
$range = 7;
for($i=0;$i < $range;$i++) {
$diff = $range - $i - 1;
$date = date('Ymd', strtotime("{$start} -{$diff} days"));
echo $date, "\n";
pcc($date);
}
#$mongo = new MongoClient();
#$award = $mongo->selectCollection('pcc', 'award');
function pcc($date) {
$result = get_list($date);
if(empty($result))
return null;
$types = array_unique(array_map(function($row) {
return $row['brief']['type'];
}, $result));
$tenders = array_filter($result, function($row) {
return in_array($row['brief']['type'], [
'公開招標更正公告',
'公開招標公告',
'公開取得報價單或企劃書更正公告',
'限制性招標(經公開評選或公開徵求)公告',
'限制性招標(經公開評選或公開徵求)更正公告']);
});
$tenders = array_map(function($row) use($date) {
preg_match('/^\w+?-(?:\d+-)?(\d+)$/', $row['filename'], $match);
$id = $match[1];
preg_match('/([^\d]+?)\d+-([^\d]+)/', $row['brief']['category'], $match);
$category = $match[1];
$sub_category = $match[2];
$tender = get_tender($row['unit_id'], $row['job_number'], $date);
if(empty($tender['detail']['採購資料:預算金額']))
$price = null;
else
$price = intval(str_replace(",", "", $tender['detail']['採購資料:預算金額']));
return array(
'_id' => $row['filename'],
'unit' => $row['unit_name'],
'key' => $row['filename'],
'filename' => $row['filename'],
'unit_id' => $row['unit_id'],
'id' => $row['job_number'],
'job_number' => $row['job_number'],
'name' => $row['brief']['title'],
'type' => $row['brief']['type'],
'category' => $category,
'sub_category' => $sub_category,
'price' => $price ? intval($price): null,
'publish' => new MongoDB\BSON\UTCDateTime(strtotime($date) * 1000)
);
}, $tenders);
$batch = new MongoDB\Driver\BulkWrite(['ordered' => true]);
foreach($tenders as $tender) {
$batch->update([
'_id' => $tender['_id']
],[
'$set' => $tender
], [
'multi' => true,
'upsert' => true,
]);
}
try {
$manager = new MongoDB\Driver\Manager();
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$r = $manager->executeBulkWrite('pcc.pcc', $batch, $writeConcern);
} catch(Exception $e) {
$r = null;
}
echo $date . ' tender upserted ' . $r->getInsertedCount(), ' nModified ', $r->getModifiedCount(), "\n";
$awards = array_filter($result, function($row) {
return in_array($row['brief']['type'], array('決標公告', '更正決標公告', '定期彙送'));
});
$awards = array_filter(array_map(function($row) use($date) {
$award = get_award($row['unit_id'], $row['job_number'], $date);
$tender = get_tender($row['unit_id'], $row['job_number'], $date);
if(empty($tender) || empty($award))
return NULL;
#preg_match('/^\w+?-\d-(\d+)$/', $tender['filename'], $match);
preg_match('/^\w+?-(?:\d+-)?(\d+)$/', $tender['filename'], $match);
$id = $match[1];
if(empty($tender['detail']['採購資料:預算金額']))
$price = null;
else
$price = intval(str_replace(",", "", $tender['detail']['採購資料:預算金額']));
$award['end_date'] = null;
if(isset($award['detail']['決標資料:決標公告日期'])) {
$award['end_date'] = new MongoDB\BSON\UTCDateTime(strtotime(preg_replace_callback('/(\d+)\/(\d+)\/(\d+)/', function($row) {
return ($row[1] + 1911) . '-' . $row[2] . '-' . $row[3];
}, $award['detail']['決標資料:決標公告日期'])) * 1000);
}
preg_match('/([^\d]+?)\d+-([^\d]+)/', $tender['brief']['category'], $match);
$candidates = array_map(function($row) {
preg_match('/^(\w+)/u', @$row['廠商業別'], $match_industry);
return array(
'_id' => @$row['廠商代碼']?:$row['廠商名稱'],
'name' => $row['廠商名稱'],
'awarding' => intval($row['是否得標'] =='是' ? 1: 0),
'org' => @$row['組織型態'],
'industry'=> @$match_industry[1],
"address" => @$row['廠商地址'],
"phone" => @$row['廠商電話'],
"country" => @$row['得標廠商國別'],
"amount" => isset($row['決標金額']) ? intval(str_replace(",", "", @$row['決標金額'])): null
);
}, isset($award['data']['投標廠商']) && is_array($award['data']['投標廠商']['投標廠商']) ? $award['data']['投標廠商']['投標廠商'] : array());
$category = $match[1];
$sub_category = $match[2];
return array(
'_id' => $row['filename'],
'unit' => $row['unit_name'],
'key' => $id,
'id' => $row['job_number'],
'job_number' => $row['job_number'],
'filename' => $row['filename'],
'unit_id' => $row['unit_id'],
'name' => $row['brief']['title'],
'type' => $row['brief']['type'],
'category' => $category,
'sub_category' => $sub_category,
'url' => $award['data']['url'],
'candidates' => array_values($candidates),
'merchants' => array_values(array_filter($candidates, function($row) {
return intval($row['awarding'] . '');
})),
//'publish' => new MongoDB\BSON\UTCDateTime(strtotime($date) * 1000),
'end_date' => $award['end_date']
);
}, $awards));
$batch = new MongoDB\Driver\BulkWrite(['ordered' => true]);
foreach($awards as $award) {
$batch->update([
'_id' => $award['_id']
],[
'$set' => $award
], [
'multi' => true,
'upsert' => true,
]);
}
try {
$manager = new MongoDB\Driver\Manager();
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$r = $manager->executeBulkWrite('pcc.award', $batch, $writeConcern);
} catch(Exception $e) {
$r = null;
}
echo $date . ' award upserted ' . $r->getInsertedCount(), ' nModified ', $r->getModifiedCount(), "\n";
$batch = new MongoDB\Driver\BulkWrite(['ordered' => true]);
foreach($awards as $award) {
if(empty($award['end_date']))
continue;
$batch->update([
'job_number' => $award['job_number'],
'unit_id' => $award['unit_id']
],[
'$set' => ['end_date' => $award['end_date'], 'award' => $award]
], [
'multi' => true,
'upsert' => true,
]);
}
try {
$manager = new MongoDB\Driver\Manager();
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$r = $manager->executeBulkWrite('pcc.pcc', $batch, $writeConcern);
}
catch(Exception $e) {
$r = null;
}
echo $date . ' tender end_date upserted ' . $r->getInsertedCount(), "\n";
}
function get_list($date) {
$base = "https://pcc.g0v.ronny.tw/api/listbydate?date=";
$api = $base . $date;
$exists = file_exists("list/{$date}.json");
$size = NULL;
if($exists) {
$stat = stat("list/{$date}.json");
$size = $stat['size'];
}
if($size && $size > 2) {
$result = json_decode(file_get_contents("list/{$date}.json"), TRUE);
}
else {
$result = json_decode(file_get_contents($api), TRUE);
file_put_contents("list/{$date}.json", json_encode($result));
}
if($result)
return $result['records'];
else
return array();
}
function get_tender_api($unit_id, $job_number, $date = FALSE)
{
if(file_exists("tender/{$unit_id}/{$job_number}.json") && filesize("tender/{$unit_id}/{$job_number}.json") > 0) {
$json = file_get_contents("tender/{$unit_id}/{$job_number}.json");
$res = json_decode($json, TRUE);
$max_date = max(array_map(function($r) {
return $r['date'];
}, $res['records']));
if($date && strtotime($max_date) < strtotime($date))
return get_tender_api_real($unit_id, $job_number);
else
return $res;
}
else {
return get_tender_api_real($unit_id, $job_number);
}
}
function get_tender_api_real($unit_id, $job_number) {
$url = "https://pcc.g0v.ronny.tw/api/tender?unit_id={$unit_id}&job_number={$job_number}";
$json = file_get_contents($url);
echo $url, " downloaded\n";
$result = json_decode($json, TRUE);
if(!file_exists("tender/{$unit_id}"))
mkdir("tender/{$unit_id}");
file_put_contents("tender/{$unit_id}/{$job_number}.json", $json);
return $result;
}
function get_tender($unit_id, $job_number, $date = FALSE) {
$result = get_tender_api($unit_id, $job_number, $date);
$records = array_filter($result['records'], function($row) {
return in_array($row['brief']['type'], array('公開招標更正公告', '公開招標公告', '公開取得報價單或企劃書更正公告', '限制性招標(經公開評選或公開徵求)公告'));
});
$record = array_pop($records);
return $record;
}
function get_award($unit_id, $job_number, $date = FALSE) {
$result = get_tender_api($unit_id, $job_number, $date);
$records = array_filter($result['records'], function($row) {
return in_array($row['brief']['type'], array('決標公告', '無法決標公告', '定期彙送'));
});
$record = array_pop($records);
$data = [];
if(!$record || !is_array($record['detail']))
return NULL;
foreach($record['detail'] as $key => $value) {
if(preg_match('/remind/', $key) || preg_match('/:理由$/', $key) || preg_match('/目的事業主管機關核准文號/', $key))
continue;
$split = explode(":", preg_replace('/(\d):/', ':$1:', $key));
$cursor = &$data;
foreach($split as $i => $s) {
if(count($split) == $i +1 && $value && !is_string($cursor))
$cursor[$s] = $value;
else if(count($split) != $i +1 && empty($cursor[$s])) {
$cursor[$s] = array();
}
else if(count($split) == $i +1){
#echo $key, " ", $s, " [", $value, "]\n";
continue;
}
$cursor = &$cursor[$s];
}
}
$record['data'] = $data;
return $record;
}