-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateJsonStructuresForBetterSearching.js
368 lines (321 loc) · 11.3 KB
/
UpdateJsonStructuresForBetterSearching.js
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
const fs = require('fs');
const util = require('util');
const moment = require('moment');
const readFile = util.promisify(fs.readFile);
const ROUTES_THAT_NEED_UPDATES_DIR = "./docs/routes/";
const DEST_PATH = "./docs/Schedules/";
const LINE_PATH = "./docs/Lines/";
let Line = {
1: null,
2: null,
3: null,
4: null,
5: null,
6: null,
7: null
};
// readDirOfLines();
// TODO This hasn't been changed to Typescript yet
function readDirOfLines() {
fs.readdir(LINE_PATH, function (err, items) {
for (let i = 0; i < items.length; i++) {
saveLineInfoInArray(items[i]);
}
});
readStationInfoFile();
}
function saveLineInfoInArray(fileName) {
async function getJsonFile() {
return await readFile(LINE_PATH + fileName);
}
getJsonFile().then(jsonData => {
save(JSON.parse(jsonData), fileName);
});
function save(jsonData, fileName) {
fileName = fileName.replace('.json', '');
switch (fileName) {
case "RoundLine":
Line["1"] = jsonData;
break;
case "CoastLine":
Line["2"] = jsonData;
break;
case "PingxiLine":
Line["3"] = jsonData;
break;
case "NeiwanLiujaLine":
Line["4"] = jsonData;
break;
case "NeiwanLine":
Line["5"] = jsonData;
break;
case "JijiLine":
Line["6"] = jsonData;
break;
case "ShalunLine":
Line["7"] = jsonData;
break;
}
}
}
readStationInfoFile();
function readStationInfoFile() {
async function getJsonFile() {
return await readFile("./docs/stationInfo.json");
}
getJsonFile().then(stationInfo => {
readDir(JSON.parse(stationInfo));
});
}
function readDir(stationInfo) {
fs.readdir(ROUTES_THAT_NEED_UPDATES_DIR, function (err, items) {
for (let i = 0; i < items.length; i++) {
readJsonFile(items[i], stationInfo);
}
setTimeout(addRoutesToStationFromSpecialRoutes, 15000);
});
}
function readJsonFile(fileName, stationInfo) {
function onlyUnique(value, index, self) {
return self.indexOf(value) === index;
}
async function getJsonFile() {
return await readFile(ROUTES_THAT_NEED_UPDATES_DIR + fileName);
}
getJsonFile().then(jsonData => {
try {
alternateData(JSON.parse(jsonData));
} catch (e) {
console.log(e, fileName)
}
});
function alternateData(fileData) {
fileData.TrainInfos.map(function (el) {
el.StartStation = parseInt(el.TimeInfos[0].Station);
el.StartTime = moment(el.TimeInfos[0].DEPTime, 'HH:mm:ss').format('HH:mm');
el.EndStation = parseInt(el.TimeInfos[el.TimeInfos.length - 1].Station);
el.EndTime = moment(el.TimeInfos[el.TimeInfos.length - 1].DEPTime, 'HH:mm:ss').format('HH:mm');
el.Stations = {};
el.Routes = [];
let MiddleStation = el.TimeInfos[Math.round((el.TimeInfos.length - 1) / 2)].Station;
let beforeMiddleStation = el.TimeInfos[Math.round(((el.TimeInfos.length - 1) / 2) / 2)].Station;
let afterMiddleStation = el.TimeInfos[Math.round(((el.TimeInfos.length - 1) / 2) + ((el.TimeInfos.length - 1) / 2) / 2)].Station;
let newTimeInfo = {};
let isTaipeiAlreadyAdded = false;
// TODO remove the station 5170 from the list because it's not yet in use
el.TimeInfos = el.TimeInfos.filter(tel => tel.Station !== "5170");
el.TimeInfos.forEach(function (tel, index) {
if (parseInt(tel.Station) === 1001) {
tel.Station = '1008'
} else {
const result = stationInfo.stations.find((sel => parseInt(sel.traWebsiteCode) === parseInt(tel.Station)));
if (result == null) {
console.error(JSON.stringify(tel.Station))
} else {
tel.Station = result.時刻表編號;
}
}
let routes = stationInfo.stations.find((sel => sel.時刻表編號 === parseInt(tel.Station))).routeCode;
el.Routes = el.Routes.concat(routes);
if (el.Train === "1" || el.Train === "2"){
if (tel.Station === "1008" && isTaipeiAlreadyAdded){
newTimeInfo["_" + tel.Station] = {
"Station": tel.Station,
"Order": tel.Order,
"DepTime": moment(tel.DEPTime, 'HH:mm:ss').format('HH:mm'),
"ArrTime": moment(tel.ARRTime, 'HH:mm:ss').format('HH:mm'),
"Routes": routes
}
} else {
newTimeInfo[tel.Station] = {
"Station": tel.Station,
"Order": tel.Order,
"DepTime": moment(tel.DEPTime, 'HH:mm:ss').format('HH:mm'),
"ArrTime": moment(tel.ARRTime, 'HH:mm:ss').format('HH:mm'),
"Routes": routes
};
isTaipeiAlreadyAdded = true;
}
} else {
newTimeInfo[tel.Station] = {
"Station": tel.Station,
"Order": tel.Order,
"DepTime": moment(tel.DEPTime, 'HH:mm:ss').format('HH:mm'),
"ArrTime": moment(tel.ARRTime, 'HH:mm:ss').format('HH:mm'),
"Routes": routes
};
}
});
el.TimeInfos = newTimeInfo;
el.Routes = el.Routes.filter(onlyUnique);
el.MultiRoute = el.Routes.length !== 1;
el.trainType = getTrainType(el);
return el;
});
try {
let JsonExport = JSON.stringify({'TrainInfos': fileData.TrainInfos}, null, 4);
exportNewData(JsonExport);
exportSpecialLines();
} catch (e) {
console.log(e.error, fileName)
}
}
let types = [];
function getTrainType(el) {
switch (el.CarClass) {
case "1132":
return "Fast Local";
case "1130":
case "1131":
case '1112':
case '1134':
case '1154':
return "Local";
case "1140":
case "1150":
return "Ordinary";
case "1102":
case "1101":
return "Taroko";
case "1107":
return "Puyuma";
case '1104':
case '1105':
case '1106':
case "1108":
case "1109":
case '1100':
case '110A':
case "110B":
case "110C":
case "110D":
case "110E":
case "110F":
case "110G":
case "110H":
case "110K":
return "Tze-chiang";
case "1110":
case "1111":
case '1113':
case "1114":
case "1115":
return "Chu-kuang";
case "1120":
case "1121":
return "Fu-Hsing";
default:
types.push({CarClass: el.CarClass, Train: el.Train, Note: el.Note});
return "Special";
}
}
function exportNewData(newData) {
fs.writeFile(DEST_PATH + fileName, newData, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
}
let Specials = 0;
let special = [];
function ObjectLength(object) {
let length = 0;
for (let key in object) {
if (object.hasOwnProperty(key)) {
++length;
}
}
return length;
};
function getRouteNumbers(station) {
return findRightStation(station).routeCode;
}
function getTraWebsiteCode(station) {
return parseInt(findRightStation(station).traWebsiteCode);
}
function findRightStation(station) {
return stationInfo.stations.find((sel => sel.時刻表編號 === parseInt(station)))
}
function exportSpecialLines() {
console.log("!!!!!!!!!!!!!!!!!!!!Specials!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
console.log("Specials", Specials);
special = unique(special, ['endStation', 'startStation', 'route']);
let object = {
lenght: ObjectLength(special),
special: special
};
types = unique(types, ['CarClass']);
types = sort(types);
console.log("Different train types", types);
fs.writeFile(LINE_PATH + "specials.json", JSON.stringify(object), err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
})
}
function unique(arr, keyProps) {
const kvArray = arr.map(entry => {
const key = keyProps.map(k => entry[k]).join('|');
return [key, entry];
});
const map = new Map(kvArray);
return Array.from(map.values());
}
function sort(list) {
return list.sort(function (a, b) {
return ('' + a.CarClass).localeCompare(b.CarClass);
})
}
}
function addRoutesToStationFromSpecialRoutes() {
console.log("Read to do special line");
readSpecialRoute();
function readSpecialRoute() {
async function getJsonFile() {
return await readFile("./docs/Lines/specials.json");
}
getJsonFile().then(specialInfo => {
updateRouteList(JSON.parse(specialInfo));
})
}
function updateRouteList(specialInfo) {
if (specialInfo.lenght === 0) {
console.log("We're done")
} else {
readStationInfoFile(specialInfo);
}
}
function readStationInfoFile(specialInfo) {
async function getJsonFile() {
return await readFile("./docs/stationInfo.json");
}
getJsonFile().then(stationInfo => {
changeStationInfo(JSON.parse(stationInfo), specialInfo);
})
}
function changeStationInfo(stationInfo, specialInfo) {
specialInfo.special.forEach(function (el) {
Object.keys(el.details.TimeInfos).forEach(function (key) {
let station = stationInfo.stations.find((sel => sel.時刻表編號 === parseInt(key))).routeCode;
if (!(station.includes(el.details.mainRoute))) {
station.push(el.details.mainRoute);
}
})
});
exportChangedStationInfo(JSON.stringify(stationInfo, null, 4));
}
function exportChangedStationInfo(newData) {
fs.writeFile("./docs/stationInfo.json", newData, err => {
if (err) {
console.log('Error writing file', err)
} else {
console.log('Successfully wrote file')
}
});
readDirOfLines();
}
}