From 24ccc16d803de93d8fa49741eb7b5a07d54cdc25 Mon Sep 17 00:00:00 2001 From: Brandon Berhent Date: Sun, 30 Jun 2024 11:09:15 -0400 Subject: [PATCH] Fix ves --- config/coingecko.go | 2 +- kubernetes/kalium/price_cron.yaml | 1 + kubernetes/natrium/price_cron.yaml | 1 + net/prices.go | 60 ++++++++++++++++++++++++------ net/prices_test.go | 22 ----------- 5 files changed, 52 insertions(+), 34 deletions(-) diff --git a/config/coingecko.go b/config/coingecko.go index ff35cf2..11222ad 100644 --- a/config/coingecko.go +++ b/config/coingecko.go @@ -2,5 +2,5 @@ package config const NANO_CG_URL = "https://api.coingecko.com/api/v3/coins/nano?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false" const BANANO_CG_URL = "https://api.coingecko.com/api/v3/coins/banano?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false" -const DOLARTODAY_URL = "https://s3.amazonaws.com/dolartoday/data.json" +const DOLARTODAY_URL = "https://dolartoday.com/wp-admin/admin-ajax.php" const DOLARSI_URL = "https://www.dolarsi.com/api/api.php?type=valoresprincipales" diff --git a/kubernetes/kalium/price_cron.yaml b/kubernetes/kalium/price_cron.yaml index 5288ee0..bc54526 100644 --- a/kubernetes/kalium/price_cron.yaml +++ b/kubernetes/kalium/price_cron.yaml @@ -5,6 +5,7 @@ metadata: name: kalium-prices spec: schedule: "*/5 * * * *" + concurrencyPolicy: Forbid # Ensures no concurrent job runs jobTemplate: spec: template: diff --git a/kubernetes/natrium/price_cron.yaml b/kubernetes/natrium/price_cron.yaml index 6d8648b..5057da1 100644 --- a/kubernetes/natrium/price_cron.yaml +++ b/kubernetes/natrium/price_cron.yaml @@ -5,6 +5,7 @@ metadata: name: natrium-prices spec: schedule: "*/5 * * * *" + concurrencyPolicy: Forbid # Ensures no concurrent job runs jobTemplate: spec: template: diff --git a/net/prices.go b/net/prices.go index 3e01d85..c2603c5 100644 --- a/net/prices.go +++ b/net/prices.go @@ -4,8 +4,11 @@ import ( "encoding/json" "errors" "fmt" + "io" "io/ioutil" "net/http" + "net/url" + "regexp" "strconv" "strings" @@ -42,26 +45,61 @@ func MakeGetRequest(url string) ([]byte, error) { return body, nil } +// DolarTodayResponse structure based on expected JSON response func UpdateDolarTodayPrice() error { - rawResp, err := MakeGetRequest(config.DOLARTODAY_URL) + // Data to be sent in POST request + data := url.Values{} + data.Set("action", "dt_currency_calculator_handler") + data.Set("amount", "1") + + // Making the HTTP POST request + request, err := http.NewRequest(http.MethodPost, config.DOLARTODAY_URL, strings.NewReader(data.Encode())) if err != nil { - klog.Errorf("Error making dolar today request %s", err) + klog.Errorf("Error creating request %s", err) return err } - var dolarTodayResp models.DolarTodayResponse - err = json.Unmarshal(rawResp, &dolarTodayResp) + request.Header.Add("Content-Type", "application/x-www-form-urlencoded") + response, err := Client.Do(request) if err != nil { - klog.Errorf("Error unmarshalling response %s", err) + klog.Errorf("Error making dolar today request: %s", err) return err } + defer response.Body.Close() - if dolarTodayResp.Usd.LocalbitcoinRef > 0 { - fmt.Printf("%s %f\n", "DolarToday USD-VES", dolarTodayResp.Usd.LocalbitcoinRef) - database.GetRedisDB().Hset("prices", "dolartoday:usd-ves", dolarTodayResp.Usd.LocalbitcoinRef) - } else { - klog.Errorf("Error getting dolar today price") - return errors.New("Dolartoday localbitcoin ref was 0") + body, err := io.ReadAll(response.Body) + if err != nil { + klog.Errorf("Error reading response body: %s", err) + return err } + + fmt.Printf("Raw response: %s\n", string(body)) + + var dolarTodayResp map[string]string + err = json.Unmarshal(body, &dolarTodayResp) + if err != nil { + klog.Errorf("Error unmarshalling response: %s", err) + return err + } + + // Extracting the "Dólar Bitcoin" value + bitcoinValueStr, ok := dolarTodayResp["Dólar Bitcoin"] + if !ok || bitcoinValueStr == "" { + klog.Errorf("Invalid or missing 'Dólar Bitcoin' in response") + return errors.New("invalid response data") + } + + // Use regular expression to extract the numeric part + re := regexp.MustCompile(`\d+\.\d+`) + match := re.FindString(bitcoinValueStr) + if match == "" { + klog.Errorf("No numeric value found in 'Dólar Bitcoin' response") + return errors.New("no numeric value found") + } + + fmt.Printf("%s %s\n", "DolarToday USD-VES:", match) + + database.GetRedisDB().Hset("prices", "dolartoday:usd-ves", match) + return nil } diff --git a/net/prices_test.go b/net/prices_test.go index dbd299c..805ca52 100644 --- a/net/prices_test.go +++ b/net/prices_test.go @@ -17,28 +17,6 @@ func init() { Client = &mocks.MockClient{} } -func TestDolarTodayPrice(t *testing.T) { - // Mock redis client - os.Setenv("MOCK_REDIS", "true") - defer os.Unsetenv("MOCK_REDIS") - // Simulate response - mocks.GetDoFunc = func(req *http.Request) (*http.Response, error) { - return &http.Response{ - StatusCode: 304, - Header: http.Header{ - "Content-Type": []string{"application/json"}, - }, - Body: mocks.DolarTodayResponse, - }, nil - } - - err := UpdateDolarTodayPrice() - assert.Equal(t, nil, err) - dolarToday, err := database.GetRedisDB().Hget("prices", "dolartoday:usd-ves") - assert.Equal(t, nil, err) - assert.Equal(t, "8.15", dolarToday) -} - func TestDolarSiPrice(t *testing.T) { // Mock redis client os.Setenv("MOCK_REDIS", "true")