Skip to content

Commit

Permalink
fix: changes to yahoo api requiring user agent and explicit response …
Browse files Browse the repository at this point in the history
…fields
  • Loading branch information
achannarasappa committed Jan 19, 2023
1 parent 76fee90 commit e4fca96
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 6 additions & 0 deletions internal/quote/yahoo/currency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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&region=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 {
Expand Down
9 changes: 6 additions & 3 deletions internal/quote/yahoo/quote.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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&region=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&region=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)

Expand Down

0 comments on commit e4fca96

Please sign in to comment.