summaryrefslogtreecommitdiff
path: root/src/bahnhofname.gleam
diff options
context:
space:
mode:
Diffstat (limited to 'src/bahnhofname.gleam')
-rw-r--r--src/bahnhofname.gleam53
1 files changed, 40 insertions, 13 deletions
diff --git a/src/bahnhofname.gleam b/src/bahnhofname.gleam
index d75a8f6..3b9bbc3 100644
--- a/src/bahnhofname.gleam
+++ b/src/bahnhofname.gleam
@@ -4,6 +4,7 @@ import gleam/http.{Get}
import gleam/bit_builder.{BitBuilder}
import gleam/erlang/process
import gleam/erlang/atom
+import gleam/erlang/file
import gleam/io
import gleam/int
import gleam/string
@@ -129,6 +130,7 @@ fn lookup_station(
request: Request(t),
stations: Map(String, String),
ds100s: Map(String, String),
+ leitpunkte: Map(String, String),
baseurl: String,
fuzzy: fn (String) -> List(String)
) -> Response(BitBuilder) {
@@ -140,13 +142,29 @@ fn lookup_station(
200,
"ds100 → Name: " <> baseurl <> "/NN\n" <> "Name → ds100: " <> baseurl <> "/Nürnberg Hbf",
)
+ Request(method: Get, path: "/ds100/" <> path, ..) ->
+ path
+ |> unpercent
+ |> string.uppercase
+ |> lookup_exact(ds100s)
+ Request(method: Get, path: "/name/" <> path, ..) ->
+ path
+ |> unpercent
+ |> lookup_by_name(stations, ds100s, fuzzy)
+ Request(method: Get, path: "/leitpunkt/" <> path, ..) ->
+ path
+ |> unpercent
+ |> string.uppercase
+ |> lookup_exact(leitpunkte)
Request(method: Get, path: "/" <> path, ..) -> {
let path = unpercent(path)
let by_ds100 = lookup_exact(path, ds100s)
+ let by_lp = lookup_exact(path, leitpunkte)
- case by_ds100.0 {
- 200 -> by_ds100
+ case #(by_ds100.0, by_lp.0) {
+ #(200, _) -> by_ds100
+ #(_, 200) -> by_lp
_ -> lookup_by_name(path, stations, ds100s, fuzzy)
}
}
@@ -183,26 +201,25 @@ fn fetch_data() -> Result(String, hackney.Error) {
Ok(string.replace(response.body, "�", "ü"))
}
-fn read_csv() -> List(#(String, String)) {
- // let assert Ok(contents) = file.read(path)
- let assert Ok(contents) = fetch_data()
+fn read_csv(contents) -> List(List(String)) {
contents
// the file doesn't use quotes, so this is fine
|> string.split(on: "\n")
// drop CSV header
|> list.drop(1)
|> list.map(fn(a) { string.split(a, on: ";") })
- |> list.filter_map(fn(fields) {
- case fields {
- [_, ds100, name, ..] -> Ok(#(name, ds100))
- _ -> Error(fields)
- }
- })
}
pub fn main() {
+ let assert Ok(bahn_ril100) = fetch_data()
let baseurl = "https://bahnhof.name"
- let stations = read_csv()
+ let stations = read_csv(bahn_ril100)
+ |> list.filter_map(fn(fields) {
+ case fields {
+ [_, ds100, name, ..] -> Ok(#(name, ds100))
+ _ -> Error(fields)
+ }
+ })
let stationmap =
stations
|> map.from_list
@@ -220,6 +237,16 @@ pub fn main() {
-> map.from_list([#("id", ds100), #("name", name)]
)}}))
+ let assert Ok(leitpunkte_raw) = file.read("data/leitpunkte.csv")
+ let leitpunkte =
+ read_csv(leitpunkte_raw)
+ |> list.filter_map(fn(fields) {
+ case fields {
+ [lp, _, ds100] -> Ok(#(lp, ds100))
+ _ -> Error(fields)
+ }
+ })
+ |> map.from_list
let fuzzy = fn(searchterm: String) -> List(String) {
let query = query_new()
@@ -259,7 +286,7 @@ pub fn main() {
let _ = mist.run_service(
2345,
- fn(req) { lookup_station(req, stationmap, ds100map, baseurl, fuzzy) },
+ fn(req) { lookup_station(req, stationmap, ds100map, leitpunkte, baseurl, fuzzy) },
max_body_limit: 100,
)
process.sleep_forever()