-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathat-station.js
executable file
·80 lines (70 loc) · 1.8 KB
/
at-station.js
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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const allStations = require('vbb-stations')
const path = require('path')
const level = require('level')
const pump = require('pump')
const through = require('through2')
const {stringify} = require('ndjson')
const pkg = require('./package.json')
const time = require('./lib/time')
const month = require('./lib/month')
const dayOfWeek = require('./lib/day-of-week')
const argv = mri(process.argv.slice(2), {
boolean: [
'help', 'h',
'version', 'v',
'db'
]
})
if (argv.help || argv.h) {
process.stdout.write(`
Usage:
delays-at-station <station-id> [--db <path-to-leveldb>]
Options:
--db Path to the LevelDB in \`record-vbb-delays\` format.
Default: vbb-delays.ldb
Examples:
delays-at-station 900000100002 >hackescher-markt.ndjson
\n`)
process.exit(0)
}
if (argv.version || argv.v) {
process.stdout.write(`delays-at-station v${pkg.version}\n`)
process.exit(0)
}
const showError = (err) => {
if (process.env.NODE_ENV === 'dev') console.error(err)
else console.error(err && err.message || (err + ''))
process.exit(1)
}
const stationId = argv._[0]
if (!stationId) showError('Invalid or missing station param.')
const [station] = allStations(stationId)
if (!station) showError('Station does not exist.')
const db = level(argv.db || 'vbb-delays.ldb', {
valueEncoding: 'json'
})
pump(
db.createValueStream(),
through.obj((dep, _, cb) => {
if (stationId !== dep.station.id) return cb()
if (dep.delay === null) return cb()
cb(null, {
delay: dep.delay,
time: time(dep),
month: month(dep),
line: dep.line && dep.line.name || null,
dayOfWeek: dayOfWeek(dep),
product: dep.line && dep.line.product || null
})
}),
stringify(),
process.stdout,
(err) => {
if (!err) return
console.error(err)
process.exitCode = 1
}
)