hk-bus-eta 1.6.0 → 1.7.2

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 CHANGED
@@ -14,11 +14,23 @@ module.exports = {
14
14
  ...route_list
15
15
  .filter(({route_no, dest_ch, stop}) => route === route_no && dest_ch === dest.zh && stop === 0)
16
16
  .map( ({time_en}) => {
17
- const etaDate = new Date(Date.now() + parseInt(time_en, 10) * 60 * 1000).toISOString();
17
+ let waitTime = 0
18
+ switch ( time_en.toLowerCase() ) {
19
+ case 'arriving':
20
+ case 'departing':
21
+ case '-':
22
+ waitTime = 0;
23
+ break;
24
+ default:
25
+ waitTime = parseInt(time_en, 10)
26
+ break;
27
+ }
28
+ const etaDate = new Date(Date.now() + waitTime * 60 * 1000 + 8 * 3600000);
18
29
  return {
19
- eta: etaDate,
30
+ eta: `${etaDate.getUTCFullYear()}-${`0${etaDate.getUTCMonth() + 1}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}`
31
+ +`T${`0${etaDate.getUTCHours()}`.slice(-2)}:${`0${etaDate.getMinutes()}`.slice(-2)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`,
20
32
  remark: {
21
- zh: `${platform_id}號平台`,
33
+ zh: `${platform_id}號月台`,
22
34
  en: `Platform ${platform_id}`
23
35
  },
24
36
  co: 'lightRail'
@@ -26,6 +38,10 @@ module.exports = {
26
38
  }, [])
27
39
  ], [])
28
40
  ))
41
+ .catch(e => {
42
+ console.error(e);
43
+ return [];
44
+ })
29
45
  ),
30
46
  fetchStopEtas: ( stopId ) => []
31
47
  }
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
@@ -1,4 +1,4 @@
1
- export type Company = "kmb" | "nlb" | "ctb" | "nwfb" | 'lrtfeeder' | 'gmb' | 'lightRail'
1
+ export type Company = "kmb" | "nlb" | "ctb" | "nwfb" | 'lrtfeeder' | 'gmb' | 'lightRail' | 'mtr'
2
2
 
3
3
  export type Terminal = {
4
4
  en: string,
package/index.js CHANGED
@@ -5,6 +5,7 @@ const NlbApi = require('./Nlb')
5
5
  const LrtfeederApi = require('./Lrtfeeder')
6
6
  const GmbApi = require('./Gmb')
7
7
  const LightRailApi = require('./LightRail')
8
+ const MtrApi = require('./Mtr')
8
9
 
9
10
  module.exports = {
10
11
  // seq is 0-based here
@@ -30,6 +31,8 @@ module.exports = {
30
31
  _etas = _etas.concat( await GmbApi.fetchEtas({stopId: stops.gmb[seq], gtfsId, seq, bound: bound[company_id]}) )
31
32
  } else if ( company_id === 'lightRail' && stops.lightRail ) {
32
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 }) )
33
36
  }
34
37
  }
35
38
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hk-bus-eta",
3
- "version": "1.6.0",
3
+ "version": "1.7.2",
4
4
  "description": "Query the ETA (Estimated Time of Arrival) of HK Bus",
5
5
  "main": "index.js",
6
6
  "scripts": {