import { fetch } from "https://esm.town/v/std/fetch";
export async function getReiseauskunftTrain(url) {
const { DOMParser } = await import(
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
function cleanTime(time) {
const cleaned = time
.trim()
.replace("Ankunft\nan ", "")
.replace("Abfahrt\nab ", "");
return cleaned === "" ? undefined : cleaned;
}
return fetch(url)
.then((res) => res.text())
.then((html) => new DOMParser().parseFromString(html, "text/html"))
.then((document) => {
return {
route: [
...document.querySelectorAll(
"#trainroute > .trainrow_1, .trainrow_2"
),
].map((row) => {
return {
station: {
code: $hanbzu.extractReiseauskunftDbStationCodeFromUrl(
row.querySelector(".station > a").getAttribute("href")
),
name: row.querySelector(".station > a").textContent.trim(),
},
arr: cleanTime(row.querySelector(".arrival").textContent),
dep: cleanTime(row.querySelector(".departure").textContent),
};
}),
remarks: document
.querySelector(".tqRemarks")
?.textContent?.trim()
?.replace("Hinweise\n", ""),
};
});
}