-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
110 lines (89 loc) · 2.63 KB
/
main.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
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
const { blue, green, red, bold } = require('chalk')
const { prompt } = require('inquirer')
const { providerList, getDataProvider } = require('./providers/index')
const hundredPercent = 100
const minuteMs = 60000
const pairs = {
'BTC/€': 'btceur',
'BTC/$': 'btcusd'
}
const symbolPair = {
'btceur': '€',
'btcusd': '$'
}
let chosenPair = pairs['BTC/€']
let symbol = symbolPair['btceur']
let lastTrigger
let lastPrice
let providerName
let getData
const triggerData = async () => {
const last = await getData(chosenPair)
lastTrigger = new Date()
displayBtcPrice(last)
}
const updateTitle = title => {
if (process.platform === 'win32') {
process.title = title
} else {
process.stdout.write(`\x1b]2;${title}\x1b\x5c`)
}
}
const displayBtcPrice = newLastPrice => {
if (lastPrice) {
const newPercent = (newLastPrice * 100) / lastPrice
if (newPercent > hundredPercent) {
const threshold = newPercent - hundredPercent
const thresholdString = threshold.toFixed(4)
console.log(`${blue(`[BTC/${symbol}]`)} Increase of ${green(`${thresholdString}%`)} - New price: ${bold(`${newLastPrice}${symbol}`)}`)
} else if (hundredPercent > newPercent) {
const threshold = hundredPercent - newPercent
const thresholdString = threshold.toFixed(4)
console.log(`${blue(`[BTC/${symbol}]`)} Decreased of ${red(`-${thresholdString}%`)} - New price: ${bold(`${newLastPrice}${symbol}`)}`)
} else {
console.log(`${blue(`[BTC/${symbol}]`)} No changes... Current price: ${bold(`${lastPrice}${symbol}`)}`)
}
}
updateTitle(`Follow BTC evolution on ${providerName}... Current price: ${newLastPrice}${symbol}`)
lastPrice = newLastPrice
}
const main = async () => {
updateTitle('Follow BTC evolution...')
const { chosenProviderName } = await prompt([{
type: 'list',
name: 'chosenProviderName',
message: `Provider?`,
choices: providerList,
default: 0
}])
providerName = chosenProviderName
getData = getDataProvider(providerName)
const { pair } = await prompt([{
type: 'list',
name: 'pair',
message: `Pair?`,
choices: Object.keys(pairs),
default: 0
}])
chosenPair = pairs[pair]
symbol = symbolPair[chosenPair]
let interval = 1
const { chosenInterval } = await prompt([{
type: 'input',
name: 'chosenInterval',
message: `Refresh interval (minutes):`,
default: interval,
validate(input) {
return isNaN(input) ?
'Please enter a valid number' :
true
}
}])
interval = chosenInterval * minuteMs
console.log('\n')
if (!lastTrigger) {
triggerData()
}
setInterval(() => triggerData(), interval)
}
main()