-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprices.js
executable file
·57 lines (41 loc) · 1.2 KB
/
prices.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
#!/usr/bin/env node
'use strict'
const mri = require('mri')
const so = require('so')
const prices = require('db-prices')
const where = require('./where')
const render = require('./render')
const argv = mri(process.argv.slice(2), {
boolean: ['help', 'h']
})
if (argv.help || argv.h) {
process.stdout.write(`\
Usage: db-prices [from] [to] [options]
Arguments:
from Station number (e.g. 8011160).
to Station number (e.g. 8000261).
Options:
--days -d The number of days to show. Default: 7`)
process.exit(0)
}
(async () => {
const from = /[0-9]+/.test(argv._[0])
? +argv._[0]
: await where('From where?')
const to = /[0-9]+/.test(argv._[1])
? +argv._[1]
: await where('To where?')
const now = new Date()
const days = new Array(argv.days || argv.d || 7)
.fill(null, 0, argv.days || argv.d || 7)
.map((_, i) => new Date(now.getFullYear(), now.getMonth(), now.getDate() + i + 1))
const byDay = await Promise.all(days.map(async (when) => {
const res = await prices(from, to, when)
return res.sort((a, b) => a.price.amount - b.price.amount)[0]
}))
process.stdout.write(render(byDay) + '\n')
})()
.catch((err) => {
console.error(err)
process.exit(1)
})