1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { fetch } from "https://esm.town/v/std/fetch";
import { extractReiseauskunftDbStationCodeFromUrl } from "https://esm.town/v/hanbzu/extractReiseauskunftDbStationCodeFromUrl";
export async function getReiseauskunftDeps(station, date, time) {
const { DOMParser } = await import(
"https://deno.land/x/deno_dom/deno-dom-wasm.ts"
);
function convertDateIsoToGerman(date) {
return [...date.matchAll(/^(\d{4})-0?(\d+)-0?(\d+)$/g)].map(
([, year, month, day]) =>
`${String(day).padStart(2, "0")}.${String(month).padStart(
2,
"0"
)}.${year.substring(2)}`
);
}
function extractStops(string) {
const regex =
/(\w+(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?(?: \w+)?) \n(\d{2}:\d{2})/g;
const matches = [...string.matchAll(regex)].map((match) => ({
station: match[1],
time: match[2],
}));
return matches;
}
return fetch(
`https://reiseauskunft.bahn.de/bin/bhftafel.exe/dn?${new URLSearchParams({
input: station,
date: convertDateIsoToGerman(date),
time: time,
boardType: "dep",
start: "Suchen",
}).toString()}`,
{
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded; charset=utf-8",
},
}
)
.then((res) => res.text())
.then((html) => new DOMParser().parseFromString(html, "text/html"))
.then((document) => {
const rows = document.querySelectorAll(
"#sqResult > table.result.stboard.dep > tbody > tr[id^='journeyRow']"
);
return [...rows].map((row) => {
const destinationAnchor = row.querySelector(".route > span > a");
const destinationName = destinationAnchor?.textContent?.trim();
return {
time: row.querySelector(".time")?.textContent?.trim(),
train: {
url: row
.querySelector(".train > a")
.getAttribute("href")
.split("?")[0],
name: [
...row.querySelectorAll(".train > a"),
][1]?.textContent?.trim(),
},
destination: {
name: destinationName,
code: extractReiseauskunftDbStationCodeFromUrl(
"https://reiseauskunft.bahn.de" +
destinationAnchor?.getAttribute("href")
),
},
stops: extractStops(
(row.querySelector(".route")?.textContent || "")
?.trim()
.substring(destinationName.length)
),
};
});
});
}