Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix light rail ETA (route_list=null; end_service_status) #10

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 77 additions & 55 deletions src/lightRail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,61 +21,83 @@ export default function fetchEtas({
},
)
.then((response) => response.json())
.then(({ platform_list }) =>
platform_list.reduce(
(acc: Eta[], { route_list, platform_id }: any) => [
...acc,
...route_list
.filter(
({ route_no, dest_ch, dest_en, stop }: any) =>
route === route_no &&
(dest_ch === dest.zh || dest_en.includes("Circular")) &&
stop === 0,
)
.map(({ time_en, train_length }: any) => {
let waitTime = 0;
switch (time_en.toLowerCase()) {
case "arriving":
case "departing":
case "-":
waitTime = 0;
break;
default:
waitTime = parseInt(time_en, 10);
break;
}
const etaDate = new Date(
Date.now() + waitTime * 60 * 1000 + 8 * 3600000,
);
return {
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: `${platform_id}號月台 - ${Array(train_length)
.fill("▭")
.join("")}`,
en: `Platform ${platform_id} - ${Array(train_length)
.fill("▭")
.join("")}`,
},
dest: {
zh: "",
en: "",
},
co: "lightRail",
};
}, []),
],
[],
),
)
.then(({ platform_list }) => {
if (
platform_list.every(({ end_service_status }: any) => end_service_status)
) {
return [
{
eta: null,
remark: {
zh: "此站今日服務已經終止",
en: "This stop's service for today has ended",
},
dest: {
zh: "",
en: "",
},
co: "lightRail",
},
];
}
// How should `end_service_status` for only some platforms be handled? Would a light rail line stop at different platforms?
return platform_list.reduce(
(acc: Eta[], { route_list, platform_id }: any) => {
return [
...acc,
// route_list is null when there are no ETAs available
...(route_list ?? [])
.filter(
({ route_no, dest_ch, dest_en, stop }: any) =>
route === route_no &&
(dest_ch === dest.zh || dest_en.includes("Circular")) &&
stop === 0,
)
.map(({ time_en, train_length }: any) => {
let waitTime = 0;
switch (time_en.toLowerCase()) {
case "arriving":
case "departing":
case "-":
waitTime = 0;
break;
default:
waitTime = parseInt(time_en, 10);
break;
}
const etaDate = new Date(
Date.now() + waitTime * 60 * 1000 + 8 * 3600000,
);
return {
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: `${platform_id}號月台 - ${Array(train_length)
.fill("▭")
.join("")}`,
en: `Platform ${platform_id} - ${Array(train_length)
.fill("▭")
.join("")}`,
},
dest: {
zh: "",
en: "",
},
co: "lightRail",
};
}, []),
];
},
[]
);
})
.catch((e) => {
console.error(e);
return [];
Expand Down