forked from alexyak/stocks
-
Notifications
You must be signed in to change notification settings - Fork 5
/
MMM-stocks.js
147 lines (113 loc) · 4.46 KB
/
MMM-stocks.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* Magic Mirror
* Module: stocks
*
* By Alex Yakhnin https://github.com/alexyak & Elan Trybuch https://github.com/elaniobro
* MIT Licensed.
*/
'use strict';
Module.register('MMM-stocks', { /*eslint-disable-line*/
result: [],
// Default module config.
defaults: {
apiKey: 'YOUR_KEY_HERE',
crypto: 'BTCUSDT,LTCUSDT,ETHUSDT',
separator: ' • ',
stocks: 'MSFT,AAPL,GOOG,INTC',
updateInterval: 60000
},
getStyles: function () {
return ['stocks.css'];
},
start: function () {
this.getStocks();
this.scheduleUpdate();
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement('marquee');
var count = 0;
var _this = this;
var separator = this.config.separator;
wrapper.className = 'medium bright';
if (this.result.length > 0) {
this.result.forEach(function (stock) {
var symbolElement = document.createElement('span');
var priceElement = document.createElement('span');
var changeElement = document.createElement('span');
var symbol = stock.symbol;
var lastPrice = stock.latestPrice;
var changePercentage = stock.changePercent;
var changeValue = stock.change;
symbolElement.className = 'stock__stock--symbol';
priceElement.className = 'stock__stock--price';
changeElement.className = 'stock__stock--change';
symbolElement.innerHTML = symbol + ' ';
wrapper.appendChild(symbolElement);
priceElement.innerHTML = '$' + _this.formatMoney(lastPrice, 2, '.', ',');
if (changePercentage > 0) {
changeElement.classList += ' up';
} else {
changeElement.classList += ' down';
}
var change = Math.abs(changeValue, -2);
changeElement.innerHTML = ' ' + change;
var divider = document.createElement('span');
if (count < _this.result.length - 1){
divider.innerHTML = separator;
}
wrapper.appendChild(priceElement);
wrapper.appendChild(changeElement);
wrapper.appendChild(divider);
count++;
});
}
return wrapper;
},
formatMoney: function (amount, decimalCount, decimal, thousands) {
try {
decimalCount = Math.abs(decimalCount);
decimalCount = isNaN(decimalCount) ? 2 : decimalCount;
var negativeSign = amount < 0 ? '-' : '';
var i = parseInt(amount = Math.abs(Number(amount) || 0).toFixed(decimalCount)).toString();
var j = (i.length > 3) ? i.length % 3 : 0;
return negativeSign + (j ? i.substr(0, j) + thousands : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousands) + (decimalCount ? decimal + Math.abs(amount - i).toFixed(decimalCount).slice(2) : '');
} catch (e) {
throw new Error(e);
}
},
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== 'undefined' && delay >= 0) {
nextLoad = delay;
}
var self = this;
setInterval(function () {
self.getStocks();
}, nextLoad);
},
roundValue: function (value) {
return Math.round(value * 100) / 100;
},
getStocks: function () {
var requestUrls = [];
var apiKey = this.config.apiKey;
var url = 'https://cloud.iexapis.com/v1/';
var stocksArray = this.config.stocks.split(',');
var cryptoArray = this.config.crypto.split(',');
cryptoArray.forEach(function (stock) {
var requestUrl = url + 'crypto/' + stock + '/quote?token=' + apiKey;
requestUrls.push(requestUrl);
});
stocksArray.forEach(function (stock) {
var requestUrl = url + 'stock/' + stock + '/quote?token=' + apiKey;
requestUrls.push(requestUrl);
});
this.sendSocketNotification('GET_STOCKS_MULTI', requestUrls);
},
socketNotificationReceived: function (notification, payload) {
if (notification === 'STOCKS_RESULT') {
this.result = payload;
this.updateDom(self.config.fadeSpeed);
}
},
});