diff options
author | terminaldweller <thabogre@gmail.com> | 2021-12-17 18:57:31 +0000 |
---|---|---|
committer | terminaldweller <thabogre@gmail.com> | 2021-12-17 18:57:31 +0000 |
commit | 0a97b56da384d988171ecd95a3198cda135f9546 (patch) | |
tree | cce4d29192c333b391a66531e9a17f240e81d9aa | |
parent | half-updateds the readme (diff) | |
download | hived-gecko.tar.gz hived-gecko.zip |
adding coingecko apigecko
-rw-r--r-- | hived/hived.go | 52 |
1 files changed, 47 insertions, 5 deletions
diff --git a/hived/hived.go b/hived/hived.go index d00f58d..1b7b8a4 100644 --- a/hived/hived.go +++ b/hived/hived.go @@ -41,6 +41,7 @@ var ( const ( cryptocomparePriceURL = "https://min-api.cryptocompare.com/data/price?" + coingeckoAPIURLv3 = "https://api.coingecko.com/api/v3" changellyURL = "https://api.changelly.com" TELEGRAM_BOT_TOKEN_ENV_VAR = "TELEGRAM_BOT_TOKEN" CHANGELLY_API_KEY_ENV_VAR = "CHANGELLY_API_KEY" @@ -109,7 +110,7 @@ type errorChanStruct struct { err error } -func sendGetToCryptoCompare( +func getPriceFromCryptoCompare( name, unit string, wg *sync.WaitGroup, priceChan chan<- priceChanStruct, @@ -148,6 +149,47 @@ func sendGetToCryptoCompare( errChan <- errorChanStruct{hasError: false, err: nil} } +func getPriceFromCoinGecko( + name, unit string, + wg *sync.WaitGroup, + priceChan chan<- priceChanStruct, + errChan chan<- errorChanStruct) { + defer wg.Done() + + params := "/simple/price?fsym=" + url.QueryEscape(name) + "&" + + "tsyms=" + url.QueryEscape(unit) + path := coingeckoAPIURLv3 + params + resp, err := http.Get(path) + if err != nil { + priceChan <- priceChanStruct{name: name, price: 0.} + errChan <- errorChanStruct{hasError: true, err: err} + log.Error().Err(err) + } + defer resp.Body.Close() + + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + priceChan <- priceChanStruct{name: name, price: 0.} + errChan <- errorChanStruct{hasError: true, err: err} + log.Error().Err(err) + } + + jsonBody := make(map[string]interface{}) + err = json.Unmarshal(body, &jsonBody) + if err != nil { + priceChan <- priceChanStruct{name: name, price: 0.} + errChan <- errorChanStruct{hasError: true, err: err} + log.Error().Err(err) + } + + price := jsonBody[name].(map[string]interface{})[unit].(float64) + + log.Info().Msg(string(body)) + + priceChan <- priceChanStruct{name: name, price: price} + errChan <- errorChanStruct{hasError: false, err: nil} +} + func priceHandler(w http.ResponseWriter, r *http.Request) { w.Header().Add("Content-Type", "application/json") if r.Method != "GET" { @@ -182,7 +224,7 @@ func priceHandler(w http.ResponseWriter, r *http.Request) { defer close(errChan) defer close(priceChan) wg.Add(1) - go sendGetToCryptoCompare(name, unit, &wg, priceChan, errChan) + go getPriceFromCryptoCompare(name, unit, &wg, priceChan, errChan) wg.Wait() select { @@ -248,8 +290,8 @@ func pairHandler(w http.ResponseWriter, r *http.Request) { defer close(errChan) wg.Add(2) - go sendGetToCryptoCompare(one, "USD", &wg, priceChan, errChan) - go sendGetToCryptoCompare(two, "USD", &wg, priceChan, errChan) + go getPriceFromCryptoCompare(one, "USD", &wg, priceChan, errChan) + go getPriceFromCryptoCompare(two, "USD", &wg, priceChan, errChan) wg.Wait() for i := 0; i < 2; i++ { @@ -339,7 +381,7 @@ func alertManager() { wg.Add(len(vars)) for i := range vars { - go sendGetToCryptoCompare(vars[i], "USD", &wg, priceChan, errChan) + go getPriceFromCryptoCompare(vars[i], "USD", &wg, priceChan, errChan) } wg.Wait() |