-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
executable file
·57 lines (46 loc) · 1.94 KB
/
index.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
const _ = require('lodash');
const path = require('path');
const notifier = require('node-notifier');
const formatNum = require('format-num');
const db = require('./db');
const getBitsoExchange = require('./services/bitso');
const defaultProps = require('./config/default.json');
const increaseIcon = 'increase.png';
const decreaseIcon = 'decrease.png';
module.exports = async function start(props) {
const _props = _.defaults(props, defaultProps);
try {
const previousPrices = db.getState();
const prices = await getBitsoExchange(_props.currencies);
_props.currencies.forEach((currency, index) => {
const displayCurrencyName = currency.split('_').join(' ').toUpperCase();
const displayPrice = prices[currency];
const price = Number(prices[currency]);
const previousPrice = Number(previousPrices[currency]);
const stable = price === previousPrice;
if (!stable) {
const increase = price > previousPrice;
const percent = calculatePercent(price, previousPrice).toFixed(4);
const notificationOptions = {
title: `${displayCurrencyName}: $${displayPrice}`,
message: !previousPrice ? ' ' : `${percent}%`,
icon: path.join(__dirname, 'icons', Boolean(increase) ? increaseIcon : decreaseIcon),
};
setTimeout(() => {
notifier.notify(notificationOptions);
}, 5000 * index);
} else {
console.info(`${displayCurrencyName} is stable`);
}
});
db.setState(prices);
} catch (e) {
console.error(e.name + ': ' + e.message);
} finally {
setTimeout(() => start(props), _props.timeToUpdate);
}
}
function calculatePercent(newPrice, oldPrice) {
const increaseValue = newPrice - oldPrice;
return (increaseValue / oldPrice) * 100;
}