-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_helper.js
69 lines (63 loc) · 2.06 KB
/
node_helper.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
const yahooFinance = require('yahoo-finance2').default;
const NodeHelper = require("node_helper");
String.prototype.hashCode = function() {
let hash = 0
if (this.length == 0) {
return hash;
}
for (let i = 0; i < this.length; i++) {
let char = this.charCodeAt(i);
hash = ((hash<<5)-hash)+char;
hash = hash & hash;
}
return hash;
}
module.exports = NodeHelper.create({
start: function() {
this.fields = [
'symbol',
'regularMarketTime',
'regularMarketPrice',
'regularMarketPreviousClose',
'regularMarketChange',
'regularMarketChangePercent'
];
this.config = null;
},
socketNotificationReceived: function(notifyID, payload) {
if (notifyID == "INIT") {
this.config = payload;
console.log("[MKTINDEX] Initialized.");
} else if (notifyID == "UPDATE") {
// Query immediately
this.callAPI(this.config, (notifyID, payload) => {
this.sendSocketNotification(notifyID, payload);
})
}
},
getQuotes: async function(symbolList) {
this.log("Querying: " + symbolList);
return await yahooFinance.quote(symbolList, {fields: this.fields}, {validateResult: false});
},
callAPI: function(cfg, callback) {
this.log("Query API for current market summary");
this.getQuotes(cfg.symbols).then(quotes => {
this.log("Quotes returned = " + quotes.length);
if (quotes.length == 0) {
console.log("[MKTINDEX] Data Error: There is no available data");
} else {
this.log("Sending result: " + quotes.length + " items");
callback('UPDATE', quotes);
}
})
.catch(err => {
console.error("[MKTINDEX] API Error: ", err.message);
return;
});
},
log: function (msg) {
if (this.config && this.config.debug) {
console.log(this.name + ": ", (msg));
}
},
})