UNPKG

hk-bus-eta

Version:

Query the ETA (Estimated Time of Arrival) of HK Bus/Minibus/MTR/Lightrail

66 lines 2.63 kB
import { isSafari } from "./utils"; export default function fetchEtas({ stopId, route, language, }) { return fetch(`https://rt.data.gov.hk/v1/transport/mtr/bus/getSchedule`, { method: "POST", cache: isSafari ? "default" : "no-store", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ language: language, routeName: route, }), }) .then((response) => response.json()) .then(({ busStop, routeStatusRemarkTitle }) => { if (busStop.length === 0) { if (routeStatusRemarkTitle && typeof routeStatusRemarkTitle === "string") { return [ { eta: "", remark: { [language]: routeStatusRemarkTitle, }, dest: { zh: "", en: "", }, co: "lrtfeeder", }, ]; } } return busStop .filter(({ busStopId }) => busStopId === stopId) .reduce((ret, { bus: buses }) => [ ...ret, ...buses.reduce((etas, bus) => { // hackily use +8 hours and use UTC hour to resolve timezone issue const etaDate = new Date(Date.now() + parseInt(bus.arrivalTimeInSecond === "108000" ? bus.departureTimeInSecond : bus.arrivalTimeInSecond, 10) * 1000 + 8 * 3600000); return [ ...etas, { eta: `${etaDate.getUTCFullYear()}-${`0${etaDate.getUTCMonth() + 1}`.slice(-2)}-${`0${etaDate.getUTCDate()}`.slice(-2)}` + `T${`0${etaDate.getUTCHours()}`.slice(-2)}:${`0${etaDate.getMinutes()}`.slice(-2)}:${`0${etaDate.getSeconds()}`.slice(-2)}+08:00`, remark: { zh: bus.busRemark || (bus.isScheduled === "1" ? "預定班次" : ""), en: bus.busRemark || (bus.isScheduled === "1" ? "Scheduled" : ""), }, dest: { zh: "", en: "", }, co: "lrtfeeder", }, ]; }, []), ], []); }); } //# sourceMappingURL=lrtfeeder.js.map