hk-bus-eta 1.3.2 → 1.4.0

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/Ctb.js CHANGED
@@ -1,8 +1,10 @@
1
+ const utils = require('./utils');
2
+
1
3
  module.exports = {
2
4
  co: 'ctb',
3
5
  fetchEtas: ({stopId, route, bound }) => (
4
6
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/CTB/${stopId}/${route}`, {
5
- cache: "reload"
7
+ cache: utils.isSafari ? 'default' : 'no-store'
6
8
  }).then(
7
9
  response => response.json()
8
10
  ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
@@ -17,7 +19,7 @@ module.exports = {
17
19
  ),
18
20
  fetchStopEtas: ( stopId ) => (
19
21
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/CTB/${stopId}`, {
20
- cache: "reload"
22
+ cache: utils.isSafari ? 'default' : 'no-store'
21
23
  }).then(
22
24
  response => response.json()
23
25
  ).then(({data}) => data.map( e => ({
package/Kmb.js CHANGED
@@ -1,10 +1,11 @@
1
+ const utils = require('./utils');
2
+
1
3
  module.exports = {
2
4
  co: 'kmb',
3
5
  fetchEtas: ({stopId, route, seq, serviceType, bound}) => (
4
- fetch(
5
- `https://data.etabus.gov.hk/v1/transport/kmb/eta/${stopId}/${route}/${serviceType}`,
6
- { cache: "reload" }
7
- ).then( response => response.json() )
6
+ fetch(`https://data.etabus.gov.hk/v1/transport/kmb/eta/${stopId}/${route}/${serviceType}`,{
7
+ cache: utils.isSafari ? 'default' : 'no-store'
8
+ }).then( response => response.json() )
8
9
  .then(({data}) => data.filter(e =>
9
10
  e.dir === bound
10
11
  && e.seq === seq + 1 // api return 1-based seq
@@ -20,7 +21,7 @@ module.exports = {
20
21
  ),
21
22
  fetchStopEtas: ( stopId ) => (
22
23
  fetch(`https://data.etabus.gov.hk/v1/transport/kmb/stop-eta/${stopId}`, {
23
- cache: "reload"
24
+ cache: utils.isSafari ? 'default' : 'no-store'
24
25
  })
25
26
  .then(response => response.json())
26
27
  .then(({data}) => data.map( e => ({
package/Lrtfeeder.js CHANGED
@@ -1,9 +1,11 @@
1
+ const utils = require('./utils');
2
+
1
3
  module.exports = {
2
4
  co: 'lrtfeeder',
3
5
  fetchEtas: ({stopId, route, language}) => (
4
6
  fetch(`https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule`, {
5
7
  method: "POST",
6
- cache: "reload",
8
+ cache: utils.isSafari ? 'default' : 'no-store',
7
9
  headers: {
8
10
  'Content-Type': 'application/json'
9
11
  },
@@ -15,14 +17,16 @@ module.exports = {
15
17
  .then(({busStop}) =>
16
18
  busStop.filter(({busStopId}) => busStopId === stopId)
17
19
  .reduce((ret, {bus: buses}) => ([...ret, ...buses.reduce( (etas, bus) => {
18
- const etaDate = new Date(Date.now() + parseInt(bus.arrivalTimeInSecond, 10) * 1000)
19
- if ( bus.arrivalTimeInSecond === "108000" ) return [...etas];
20
+ // hackily use +8 hours and use UTC hour to resolve timezone issue
21
+ const etaDate = new Date(Date.now() + parseInt(bus.arrivalTimeInSecond === "108000" ? bus.departureTimeInSecond : bus.arrivalTimeInSecond, 10) * 1000 + 8 * 3600000);
22
+
20
23
  return [
21
24
  ...etas,
22
25
  {
23
- eta: new Date(etaDate.toString().split('GMT')[0]+' UTC').toISOString(),
26
+ eta: `${etaDate.getUTCFullYear()}-${`0${etaDate.getUTCMonth() + 1}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}`
27
+ +`T${`0${etaDate.getUTCHours()}`.slice(-2)}:${`0${etaDate.getMinutes()}`.slice(-2)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`,
24
28
  remark: {
25
- [language]: bus.busRemark
29
+ [language]: bus.busRemark || ( bus.isScheduled === "1" ? ( language === "en" ? "Scheduled" : "預定班次" ) : "" )
26
30
  },
27
31
  co: 'lrtfeeder'
28
32
  }
package/Nlb.js CHANGED
@@ -1,3 +1,5 @@
1
+ const utils = require('./utils');
2
+
1
3
  module.exports = {
2
4
  co: 'nlb',
3
5
  fetchEtas: ({stopId, nlbId }) => (
@@ -16,8 +18,9 @@ module.exports = {
16
18
  response => response.json()
17
19
  ).then(({estimatedArrivals}) => {
18
20
  if ( !estimatedArrivals ) return []
21
+
19
22
  return estimatedArrivals.filter(eta => eta.estimatedArrivalTime).map(e => ({
20
- eta: e.estimatedArrivalTime,
23
+ eta: e.estimatedArrivalTime.replace(' ', 'T') + '.000+08:00',
21
24
  remark: {
22
25
  zh: '',
23
26
  en: ''
package/Nwfb.js CHANGED
@@ -1,8 +1,10 @@
1
+ const utils = require('./utils');
2
+
1
3
  module.exports = {
2
4
  co: 'nwfb',
3
5
  fetchEtas: ({stopId, route, bound }) => (
4
6
  fetch(`https://rt.data.gov.hk//v1/transport/citybus-nwfb/eta/NWFB/${stopId}/${route}`, {
5
- cache: "reload"
7
+ cache: utils.isSafari ? 'default' : 'no-store'
6
8
  }).then(
7
9
  response => response.json()
8
10
  ).then(({data}) => data.filter(eta => eta.eta && eta.dir === bound).map(e => ({
@@ -17,7 +19,7 @@ module.exports = {
17
19
  ),
18
20
  fetchStopEtas: ( stopId ) => (
19
21
  fetch(`https://rt.data.gov.hk/v1/transport/batch/stop-eta/NWFB/${stopId}`, {
20
- cache: "reload"
22
+ cache: utils.isSafari ? 'default' : 'no-store'
21
23
  }).then(
22
24
  response => response.json()
23
25
  ).then(({data}) => data.map( e => ({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hk-bus-eta",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Query the ETA (Estimated Time of Arrival) of HK Bus",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/utils.js ADDED
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ isSafari: navigator
3
+ && navigator.userAgent
4
+ && navigator.userAgent.includes('Safari/')
5
+ && !(navigator.userAgent.includes('Chrome/') || navigator.userAgent.includes('Chromium/'))
6
+ }