summaryrefslogtreecommitdiff
path: root/src/bahnhofname.gleam
blob: d75a8f6adb37f218c153edc323ce9371fce57dd8 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import gleam/http/response.{Response}
import gleam/http/request.{Request}
import gleam/http.{Get}
import gleam/bit_builder.{BitBuilder}
import gleam/erlang/process
import gleam/erlang/atom
import gleam/io
import gleam/int
import gleam/string
import gleam/bit_string
import gleam/list
import gleam/map.{Map}
import gleam/uri
import gleam/hackney
import mist

external type Index
external type Field

external fn index_new(atom.Atom) -> Index =
  "Elixir.Haystack.Index" "new"

external fn index_ref(Index, Field) -> Index =
  "Elixir.Haystack.Index" "ref"

external fn index_field(Index, Field) -> Index =
  "Elixir.Haystack.Index" "field"

external fn field_term(String) -> Field =
  "Elixir.Haystack.Index.Field" "term"

external fn field_new(String) -> Field =
  "Elixir.Haystack.Index.Field" "new"

external fn index_add(Index, List(a)) -> Index =
  "Elixir.Haystack.Index" "add"

external fn index_search(Index, String) -> List(Map(atom.Atom, String)) =
  "Elixir.Haystack.Index" "search"

pub external fn inspect(a) -> a =
  "Elixir.IO" "inspect"

external type Query
external type Clause
external type Expression
external fn query_new() -> Query =
  "Elixir.Haystack.Query" "new"
external fn query_clause(Query, Clause) -> Query =
  "Elixir.Haystack.Query" "clause"
external fn query_run(Query, Index) -> List(Map(atom.Atom, String)) =
  "Elixir.Haystack.Query" "run"
external fn clause_new(atom.Atom) -> Clause =
  "Elixir.Haystack.Query.Clause" "new"
external fn query_expressions(Clause, List(Expression)) -> Clause =
  "Elixir.Haystack.Query.Clause" "expressions"
external fn query_expression_new(atom.Atom, List(#(atom.Atom, String))) -> Expression =
  "Elixir.Haystack.Query.Expression" "new"

external fn tokenize(String) -> List(Map(atom.Atom, String)) =
  "Elixir.Haystack.Tokenizer" "tokenize"

fn unpercent(encoded: String) -> String {
  let #([head], chunks) =
    encoded
    |> string.split(on: "%")
    |> list.split(at: 1)

  let assert Ok(res) =
    chunks
    |> list.map(fn(str) {
      case string.length(str) < 2 {
        True -> bit_string.from_string(str)
        False -> {
          let assert Ok(codepoint) =
            str
            |> string.slice(at_index: 0, length: 2)
            |> int.base_parse(16)
          <<codepoint:8, string.drop_left(str, 2):utf8>>
        }
      }
    })
    |> list.prepend(bit_string.from_string(head))
    |> bit_string.concat
    |> bit_string.to_string
  res
}

/// Looks up a query in a Map by exact value, no fuzzy matching.
fn lookup_exact(query: String, lookup: Map(String, String)) -> #(Int, String) {
  case map.get(lookup, query) {
    Ok(result) -> #(200, result)
    _ -> #(404, "??")
  }
}

/// Looks up a station by its name, with fuzzy matching.
fn lookup_by_name(
  query: String,
  stations: Map(String, String),
  ds100s: Map(String, String),
  fuzzy: fn(String) -> List(String)
) -> #(Int, String) {
  io.println(query)

  case map.get(stations, query) {
    Ok(ds100) -> #(200, ds100)
    _ -> {
      let results = fuzzy(query)
        |> list.filter_map(fn (res) { map.get(ds100s, string.uppercase(res)) })
      case results {
        // results -> {
        //   let names = results
        //     |> list.map (fn (res) {
        //     map.get(ds100s, string.uppercase(res))
        //     |> result.map(fn(a) { "/" <> a })
        //     |> result.unwrap("/")})
        //   #(200, string.join(names, "\n"))
        // }
        [res] -> #(302, res)
        [res, ..] -> #(302, res)
        _ -> #(404, "??")
      }
    }
  }
}

fn lookup_station(
  request: Request(t),
  stations: Map(String, String),
  ds100s: Map(String, String),
  baseurl: String,
  fuzzy: fn (String) -> List(String)
) -> Response(BitBuilder) {
  let #(code, text) = case request {
    // blackhole favicon.ico requests instead of using the index
    Request(method: Get, path: "/favicon.ico", ..) -> #(404, "")
    Request(method: Get, path: "/help", ..)
    | Request(method: Get, path: "/", ..) -> #(
      200,
      "ds100 → Name: " <> baseurl <> "/NN\n" <> "Name → ds100: " <> baseurl <> "/Nürnberg Hbf",
    )
    Request(method: Get, path: "/" <> path, ..) -> {
      let path = unpercent(path)

      let by_ds100 = lookup_exact(path, ds100s)

      case by_ds100.0 {
        200 -> by_ds100
        _ -> lookup_by_name(path, stations, ds100s, fuzzy)
      }
    }
    _ -> #(404, "intended usage is e.g. curl " <> baseurl <> "/FF")
  }
  let body = bit_builder.from_string(text)

  response.new(code)
  |> response.prepend_header(
    "x-data-source",
    "https://data.deutschebahn.com/dataset/data-betriebsstellen.html",
  )
  |> response.prepend_header(
    "x-sources-at",
    "https://stuebinm.eu/git/bahnhof.name",
  )
  |> response.prepend_header("content-type", "text/plain; charset=utf8")
  |> fn (a) { case code == 302 {
        True -> response.prepend_header(a, "location", text)
        _ -> a
     } }
  |> response.set_body(body)
}

fn fetch_data() -> Result(String, hackney.Error) {
  let assert Ok(uri) =
    uri.parse(
      "https://download-data.deutschebahn.com/static/datasets/betriebsstellen/DBNetz-Betriebsstellenverzeichnis-Stand2021-10.csv",
    )
  let assert Ok(request) = request.from_uri(uri)
  let assert Ok(response) = hackney.send(request)

  // some ü are corrupted for some reason
  Ok(string.replace(response.body, "�", "ü"))
}

fn read_csv() -> List(#(String, String)) {
  // let assert Ok(contents) = file.read(path)
  let assert Ok(contents) = fetch_data()
  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 baseurl = "https://bahnhof.name"
  let stations = read_csv()
  let stationmap =
    stations
    |> map.from_list
  let ds100map =
    stations
    |> list.map(fn(a) { #(a.1, a.0) })
    |> map.from_list
  let ref = atom.create_from_string("ref")
  let index =  index_new(atom.create_from_string("stations"))
    |> index_ref(field_term("id"))
    |> index_field(field_new("name"))
    |> index_add(stations
        |> list.map(fn(tuple) {case tuple {
          #(name, ds100)
          -> map.from_list([#("id", ds100), #("name", name)]
    )}}))


  let fuzzy = fn(searchterm: String) -> List(String) {
    let query = query_new()
    let match = atom.create_from_string("match")
    let field = atom.create_from_string("field")
    let term = atom.create_from_string("term")
    let expressions = tokenize(inspect(searchterm))
      |> list.filter_map(fn (a) { map.get(a, atom.create_from_string("v")) })
      |> list.map(fn (token) { query_expression_new(match, [#(field, "name"), #(term, token)]) })
    let clause = query_expressions(clause_new(atom.create_from_string("all")), expressions)
    let query = query_clause(query, clause)

    let matches = query_run(query, index)
      |> list.filter_map(fn (a) { map.get(a, ref) })

    inspect(matches)
    case list.length(matches) > 5 {
      True -> {
        let query = query_new()
        let clause = query_expressions(
          clause_new(atom.create_from_string("all")),
          [query_expression_new(match, [#(field, "name"), #(term, "hbf")]) , ..expressions]
        )
        let query = query_clause(query, clause)
        let narrow = query_run(query, index)
          |> list.filter_map(fn (a) { map.get(a, ref) })
        case narrow {
          [] -> matches
          _ -> narrow
        }
      }
      _ -> matches
    }
  }

  io.println("compiled index, starting server …")

  let _ = mist.run_service(
    2345,
    fn(req) { lookup_station(req, stationmap, ds100map, baseurl, fuzzy) },
    max_body_limit: 100,
  )
  process.sleep_forever()
}