-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
node_helper.js
14 lines (10 loc) · 3.03 KB
/
node_helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*! *****************************************************************************
mmm-jast
Version 2.9.5
A minimalistic stock ticker based on Yahoo's finance API for the MagicMirror² platform.
Please submit bugs at https://github.com/jalibu/MMM-Jast/issues
Licence: MIT
This file is auto-generated. Do not edit.
***************************************************************************** */
"use strict";var e=require("node_helper"),r=require("logger"),t=require("yahoo-finance2");function s(e){return e&&e.__esModule?e:{default:e}}function n(e){if(e&&e.__esModule)return e;var r=Object.create(null);return e&&Object.keys(e).forEach((function(t){if("default"!==t){var s=Object.getOwnPropertyDescriptor(e,t);Object.defineProperty(r,t,s.get?s:{enumerable:!0,get:function(){return e[t]}})}})),r.default=e,Object.freeze(r)}var o=n(e),a=n(r),i=s(t);function c(e,r,t,s){return new(t||(t=Promise))((function(n,o){function a(e){try{c(s.next(e))}catch(e){o(e)}}function i(e){try{c(s.throw(e))}catch(e){o(e)}}function c(e){var r;e.done?n(e.value):(r=e.value,r instanceof t?r:new t((function(e){e(r)}))).then(a,i)}c((s=s.apply(e,r||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;class u{static requestStocks(e){return c(this,void 0,void 0,(function*(){var r;const t=[],s=[];for(const r of e.stocks)s.push(i.default.quoteSummary(r.symbol,{modules:["price"]}));const n=yield Promise.all(s.map((e=>e.catch((e=>e)))));for(const[s,o]of n.entries())if(o instanceof Error)a.warn(`API request for ${e.stocks[s].symbol} failed:`,o.message);else if(o.price){const n={symbol:e.stocks[s].symbol,name:e.stocks[s].name,quantity:e.stocks[s].quantity,hidden:e.stocks[s].hidden,purchasePrice:e.stocks[s].purchasePrice};if("GBp"===o.price.currency&&(o.price.regularMarketPrice/=100,o.price.regularMarketChange/=100,o.price.currency="GBP"),e.maxChangeAge>0){const t=(new Date).getTime()-e.maxChangeAge;try{t>Date.parse(o.price.regularMarketTime)&&(o.price.regularMarketPreviousClose=null===(r=o.price)||void 0===r?void 0:r.regularMarketPrice,o.price.regularMarketChange=0,o.price.regularMarketChangePercent=0)}catch(e){a.warn("Could not parse lastChange date",e)}}t.push({price:o.price,meta:n})}else a.warn(`Response for ${e.stocks[s].symbol} does not satisfy expected payload.`);return t}))}}const l=["regularMarketChange","regularMarketChangePercent","regularMarketPrice","currency","longName","regularMarketPreviousClose"];module.exports=o.create({start(){a.log(`${this.name} helper method started...`)},socketNotificationReceived(e,r){return c(this,void 0,void 0,(function*(){if(e.includes("JAST_STOCKS_REQUEST")){const t=e.substring(20);let s=yield u.requestStocks(r);s=s.filter((e=>l.every((r=>!!Object.prototype.hasOwnProperty.call(e.price,r)||(a.warn(`Skipped symbol '${e.meta.symbol}' as it's response did not have required property '${r}'. This is usually the case when a symbol is misspelled`),!1)))));const n={lastUpdate:Date.now(),stocks:s};this.sendSocketNotification(`JAST_STOCKS_RESPONSE-${t}`,n)}else a.warn(`${e} is invalid notification`)}))}});