From e4fca96c4548cca8004ae8de038b786e01c3080b Mon Sep 17 00:00:00 2001 From: Ani Channarasappa Date: Wed, 18 Jan 2023 22:17:59 -0500 Subject: [PATCH] fix: changes to yahoo api requiring user agent and explicit response fields --- internal/quote/yahoo/currency.go | 6 ++++++ internal/quote/yahoo/quote.go | 9 ++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/internal/quote/yahoo/currency.go b/internal/quote/yahoo/currency.go index badb037..10be03e 100644 --- a/internal/quote/yahoo/currency.go +++ b/internal/quote/yahoo/currency.go @@ -44,6 +44,9 @@ func getCurrencyRatesFromCurrencyPairSymbols(client resty.Client, currencyPairSy res, err := client.R(). SetResult(Response{}). + SetHeader("Host", "query1.finance.yahoo.com"). + SetHeader("accept", "*/*"). + SetHeader("user-agent", "curl/7.68.0"). Get(url) if err != nil { @@ -78,6 +81,9 @@ func getCurrencyPairSymbols(client resty.Client, symbols []string, targetCurrenc url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US®ion=US&corsDomain=finance.yahoo.com&fields=regularMarketPrice,currency&symbols=%s", symbolsString) res, err := client.R(). SetResult(Response{}). + SetHeader("Host", "query1.finance.yahoo.com"). + SetHeader("accept", "*/*"). + SetHeader("user-agent", "curl/7.68.0"). Get(url) if err != nil { diff --git a/internal/quote/yahoo/quote.go b/internal/quote/yahoo/quote.go index 46ffc89..6bea40f 100644 --- a/internal/quote/yahoo/quote.go +++ b/internal/quote/yahoo/quote.go @@ -99,7 +99,7 @@ func transformResponseQuote(responseQuote ResponseQuote) c.AssetQuote { return assetQuote } - if responseQuote.MarketState == "POST" && responseQuote.PostMarketPrice == 0.0 { + if responseQuote.MarketState == "POSTPOST" && responseQuote.PostMarketPrice == 0.0 { assetQuote.Exchange.IsRegularTradingSession = false return assetQuote @@ -112,7 +112,7 @@ func transformResponseQuote(responseQuote ResponseQuote) c.AssetQuote { return assetQuote } - if responseQuote.MarketState == "POST" { + if responseQuote.MarketState == "POSTPOST" { assetQuote.QuotePrice.Price = responseQuote.PostMarketPrice assetQuote.QuotePrice.Change = (responseQuote.PostMarketChange + responseQuote.RegularMarketChange) assetQuote.QuotePrice.ChangePercent = responseQuote.PostMarketChangePercent + responseQuote.RegularMarketChangePercent @@ -162,8 +162,11 @@ func transformResponseQuotes(responseQuotes []ResponseQuote) []c.AssetQuote { func GetAssetQuotes(client resty.Client, symbols []string) func() []c.AssetQuote { return func() []c.AssetQuote { symbolsString := strings.Join(symbols, ",") - url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?lang=en-US®ion=US&corsDomain=finance.yahoo.com&symbols=%s", symbolsString) + url := fmt.Sprintf("https://query1.finance.yahoo.com/v7/finance/quote?fields=shortName,regularMarketChange,regularMarketChangePercent,regularMarketPrice,regularMarketPreviousClose,regularMarketOpen,regularMarketDayRange,regularMarketDayHigh,regularMarketDayLow,regularMarketVolume,postMarketChange,postMarketChangePercent,postMarketPrice,preMarketChange,preMarketChangePercent,preMarketPrice,fiftyTwoWeekHigh,fiftyTwoWeekLow,marketCap®ion=US&lang=en-US&symbols=%s", symbolsString) res, _ := client.R(). + SetHeader("Host", "query1.finance.yahoo.com"). + SetHeader("accept", "*/*"). + SetHeader("user-agent", "curl/7.68.0"). SetResult(Response{}). Get(url)