Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#236 support currencies for markets widget #238

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions internal/feed/primitives.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ type DNSStatsBlockedDomain struct {
type MarketRequest struct {
Name string `yaml:"name"`
Symbol string `yaml:"symbol"`
Currency string `yaml:"currency"`
ChartLink string `yaml:"chart-link"`
SymbolLink string `yaml:"symbol-link"`
}
Expand Down
39 changes: 39 additions & 0 deletions internal/feed/yahoo.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package feed

import (
"encoding/json"
"fmt"
"log/slog"
"net/http"
Expand All @@ -27,6 +28,30 @@ type marketResponseJson struct {
// TODO: allow changing chart time frame
const marketChartDays = 21

func FetchUSDExchangeRate(currency string) (float64, error) {
url := fmt.Sprintf("https://query1.finance.yahoo.com/v8/finance/chart/USD%s=X?range=1d&interval=1d", currency)
resp, err := http.Get(url)
if err != nil {
return 0, fmt.Errorf("failed to fetch exchange rate: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var response marketResponseJson
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return 0, fmt.Errorf("failed to decode response: %w", err)
}

if len(response.Chart.Result) == 0 {
return 0, fmt.Errorf("no result in response")
}

return response.Chart.Result[0].Meta.RegularMarketPrice, nil
}

func FetchMarketsDataFromYahoo(marketRequests []MarketRequest) (Markets, error) {
requests := make([]*http.Request, 0, len(marketRequests))

Expand Down Expand Up @@ -80,6 +105,20 @@ func FetchMarketsDataFromYahoo(marketRequests []MarketRequest) (Markets, error)
currency = response.Chart.Result[0].Meta.Currency
}

if marketRequests[i].Currency != "" {
exchangeRate, err := FetchUSDExchangeRate(marketRequests[i].Currency)
if err != nil {
slog.Error("Failed to fetch USD exchange rate", "error", err)
continue
}

if response.Chart.Result[0].Meta.Currency == "USD" {
response.Chart.Result[0].Meta.RegularMarketPrice *= exchangeRate
previous *= exchangeRate
currency = currencyToSymbol[marketRequests[i].Currency]
}
}

markets = append(markets, Market{
MarketRequest: marketRequests[i],
Price: response.Chart.Result[0].Meta.RegularMarketPrice,
Expand Down