From 935a53d42746c4e73b49bf224f439293fd64228a Mon Sep 17 00:00:00 2001 From: Christopher Huemmer Date: Mon, 7 Nov 2022 18:32:40 +0100 Subject: [PATCH] Fixes issue with missing name property for certain assets --- app/services/yahooService.js | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/services/yahooService.js b/app/services/yahooService.js index 8e771f0..733d4a4 100644 --- a/app/services/yahooService.js +++ b/app/services/yahooService.js @@ -106,14 +106,10 @@ function getHistoricalPrices(ticker, options) { } try { - var longName = body.split(`"${ticker}":{"sourceInterval"`)[1] - .split("longName")[1] - .split(":")[1] - .split(",")[0].replace(/"/g, ''); - - var prices = body.split("HistoricalPriceStore\":{\"prices\"\:")[1].split("}]")[0] + '}]'; - - jsonPrices = JSON.parse(prices); + var json = getQuoteDataFromBodyAsJson(body) + var entity = json[ticker] + var longName = (getLongName(entity)) ? getLongName(entity) : getShortName(entity) + jsonPrices = getHistoricalDataFromBodyAsJson(body) const array = jsonPrices.slice((page - 1) * limit, page * limit); resolve({ longName, ticker, array }); @@ -127,11 +123,15 @@ function getHistoricalPrices(ticker, options) { // Helper functions function getQuoteDataFromBodyAsJson(body) { const dataStore = body - .split(`"StreamDataStore":`)[1] - .split(`,"QuoteSummaryStore"`)[0]; + .split(`"StreamDataStore":`)[1] + .split(`,"QuoteSummaryStore"`)[0]; return JSON.parse(dataStore)['quoteData']; } +function getHistoricalDataFromBodyAsJson(body) { + return JSON.parse(body.split("HistoricalPriceStore\":{\"prices\"\:")[1].split("}]")[0] + '}]'); +} + function getPrice(entity) { return entity.regularMarketPrice.fmt; }