-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconvert_routes_to_stops.coffee
40 lines (31 loc) · 1.11 KB
/
convert_routes_to_stops.coffee
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
fs = require 'fs'
json = require 'json'
async = require 'async'
_ = require 'underscore'
routeRegex = ///leid(\d*)\.json///
flatten = (array) -> [].concat array...
emitStops = (r) -> ([stop, r.routeNr] for stop in r.stops)
reduceToList = (memo, value) ->
[k, v] = value
memo[k] = memo[k] or []
memo[k].push v
memo
extractStops = (path, callback) ->
fs.readFile path, (err, data) ->
routeNr = (Number) path.match(routeRegex)[1]
stops = _(json.parse(data).timetables).chain()
.flatten()
.pluck('timetable')
.flatten()
.pluck('stop')
.uniq(false)
.value()
callback null,
routeNr: routeNr,
stops: stops
fs.readdir '.', (err, files) ->
routeFiles = _.filter files, (f) -> f.match(routeRegex)
async.map routeFiles, extractStops, (err, routes) ->
mappedPairs = flatten(_.map routes, emitStops)
stops = _([k, v] for k, v of _.reduce(mappedPairs, reduceToList, {})).sortBy (p) -> p[0]
(console.log "#{s[0]};#{s[1].join(';')}" for s in stops)