hk-bus-eta-skpracta
Version:
Query the ETA (Estimated Time of Arrival) of HK Bus/Minibus/MTR/Lightrail (skpracta flavored)
49 lines (46 loc) • 1.25 kB
text/typescript
import type { Eta, RouteListEntry, StopList } from "./type";
import { isSafari } from "./utils";
interface fetchEtasProps {
route: RouteListEntry["route"];
bound: RouteListEntry["bound"]["mtr"];
stopId: string;
stopList: StopList;
}
export default function fetchEtas({
stopId,
route,
bound,
stopList,
}: fetchEtasProps): Promise<Eta[]> {
return fetch(
`https://rt.data.gov.hk/v1/transport/mtr/getSchedule.php?line=${route}&sta=${stopId}`,
{
cache: isSafari ? "default" : "no-store",
},
)
.then((response) => response.json())
.then(({ data, status }) =>
status === 0
? []
: data[`${route}-${stopId}`][
bound.endsWith("UT") ? "UP" : "DOWN"
].reduce(
(acc: Eta[], { time, plat, dest }: any) => [
...acc,
{
eta: time.replace(" ", "T") + "+08:00",
remark: {
zh: `${plat}號月台`,
en: `Platform ${plat}`,
},
dest: {
zh: stopList[dest].name.zh,
en: stopList[dest].name.en,
},
co: "mtr",
},
],
[],
),
);
}