hk-bus-eta 1.5.2 → 1.7.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LightRail.js +36 -0
- package/Mtr.js +27 -0
- package/index.d.ts +1 -1
- package/index.js +7 -1
- package/package.json +1 -1
package/LightRail.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const utils = require('./utils');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
co: 'lightRail',
|
|
5
|
+
fetchEtas: ({stopId, route, dest }) => (
|
|
6
|
+
fetch(`https://rt.data.gov.hk/v1/transport/mtr/lrt/getSchedule?station_id=${stopId.slice(2)}`, {
|
|
7
|
+
cache: utils.isSafari ? 'default' : 'no-store',
|
|
8
|
+
|
|
9
|
+
}).then( response => response.json() )
|
|
10
|
+
.then(({platform_list}) => (
|
|
11
|
+
platform_list
|
|
12
|
+
.reduce((acc, {route_list, platform_id}) => [
|
|
13
|
+
...acc,
|
|
14
|
+
...route_list
|
|
15
|
+
.filter(({route_no, dest_ch, stop}) => route === route_no && dest_ch === dest.zh && stop === 0)
|
|
16
|
+
.map( ({time_en}) => {
|
|
17
|
+
const etaDate = new Date(Date.now() + parseInt(time_en === '-' ? '0' : time_en, 10) * 60 * 1000 + 8 * 3600000);
|
|
18
|
+
return {
|
|
19
|
+
eta: `${etaDate.getUTCFullYear()}-${`0${etaDate.getUTCMonth() + 1}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}`
|
|
20
|
+
+`T${`0${etaDate.getUTCHours()}`.slice(-2)}:${`0${etaDate.getMinutes()}`.slice(-2)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`,
|
|
21
|
+
remark: {
|
|
22
|
+
zh: `${platform_id}號月台`,
|
|
23
|
+
en: `Platform ${platform_id}`
|
|
24
|
+
},
|
|
25
|
+
co: 'lightRail'
|
|
26
|
+
}
|
|
27
|
+
}, [])
|
|
28
|
+
], [])
|
|
29
|
+
))
|
|
30
|
+
.catch(e => {
|
|
31
|
+
console.error(e);
|
|
32
|
+
return [];
|
|
33
|
+
})
|
|
34
|
+
),
|
|
35
|
+
fetchStopEtas: ( stopId ) => []
|
|
36
|
+
}
|
package/Mtr.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
const utils = require('./utils');
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
co: 'mtr',
|
|
5
|
+
fetchEtas: ({stopId, route, bound }) => (
|
|
6
|
+
fetch(`https://rt.data.gov.hk/v1/transport/mtr/getSchedule.php?line=${route}&sta=${stopId}`, {
|
|
7
|
+
cache: utils.isSafari ? 'default' : 'no-store',
|
|
8
|
+
}).then( response => response.json() )
|
|
9
|
+
.then(({data, status}) => (
|
|
10
|
+
status === 0
|
|
11
|
+
? []
|
|
12
|
+
: data[`${route}-${stopId}`][bound.slice(-2,1) === 'U' ? 'UP' : 'DOWN']
|
|
13
|
+
.reduce((acc, {time, plat, dest}) => [
|
|
14
|
+
...acc,
|
|
15
|
+
{
|
|
16
|
+
eta: time.replace(' ', 'T')+'+08:00',
|
|
17
|
+
remark: {
|
|
18
|
+
zh: `${plat}號月台`,
|
|
19
|
+
en: `Platform ${plat}`
|
|
20
|
+
},
|
|
21
|
+
co: 'mtr'
|
|
22
|
+
}
|
|
23
|
+
], [])
|
|
24
|
+
))
|
|
25
|
+
),
|
|
26
|
+
fetchStopEtas: ( stopId ) => []
|
|
27
|
+
}
|
package/index.d.ts
CHANGED
package/index.js
CHANGED
|
@@ -4,10 +4,12 @@ const CtbApi = require('./Ctb')
|
|
|
4
4
|
const NlbApi = require('./Nlb')
|
|
5
5
|
const LrtfeederApi = require('./Lrtfeeder')
|
|
6
6
|
const GmbApi = require('./Gmb')
|
|
7
|
+
const LightRailApi = require('./LightRail')
|
|
8
|
+
const MtrApi = require('./Mtr')
|
|
7
9
|
|
|
8
10
|
module.exports = {
|
|
9
11
|
// seq is 0-based here
|
|
10
|
-
fetchEtas: async ( {route, stops, bound, seq, serviceType, co, nlbId, gtfsId, language }) => {
|
|
12
|
+
fetchEtas: async ( {route, stops, bound, dest, seq, serviceType, co, nlbId, gtfsId, language }) => {
|
|
11
13
|
let _etas = []
|
|
12
14
|
for ( const company_id of co ) {
|
|
13
15
|
if (company_id === 'kmb' && stops.kmb ){
|
|
@@ -27,6 +29,10 @@ module.exports = {
|
|
|
27
29
|
_etas = _etas.concat( await LrtfeederApi.fetchEtas({stopId: stops.lrtfeeder[seq], route, language}))
|
|
28
30
|
} else if ( company_id === 'gmb' && stops.gmb ) {
|
|
29
31
|
_etas = _etas.concat( await GmbApi.fetchEtas({stopId: stops.gmb[seq], gtfsId, seq, bound: bound[company_id]}) )
|
|
32
|
+
} else if ( company_id === 'lightRail' && stops.lightRail ) {
|
|
33
|
+
_etas = _etas.concat( await LightRailApi.fetchEtas({ stopId: stops.lightRail[seq], route, dest }) )
|
|
34
|
+
} else if ( company_id === 'mtr' && stops.mtr ) {
|
|
35
|
+
_etas = _etas.concat( await MtrApi.fetchEtas({ stopId: stops.mtr[seq], route, bound: bound.mtr }) )
|
|
30
36
|
}
|
|
31
37
|
}
|
|
32
38
|
|