From 0f28990beae1417097d0ae36e07f148a1316f0ad Mon Sep 17 00:00:00 2001 From: Reckless_Satoshi Date: Wed, 10 May 2023 13:57:33 -0700 Subject: [PATCH] Add new api for external prices (non-tor only) bitpay --- .env-sample | 2 +- api/tasks.py | 3 +++ api/utils.py | 23 +++++++++++++++++++++-- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/.env-sample b/.env-sample index 28d77782..f108bd1d 100644 --- a/.env-sample +++ b/.env-sample @@ -37,7 +37,7 @@ LND_GRPC_HOST='localhost:10009' REDIS_URL='redis://localhost:6379/1' # List of market price public APIs. If the currency is available in more than 1 API, will use median price. -MARKET_PRICE_APIS = https://blockchain.info/ticker, https://api.yadio.io/exrates/BTC +MARKET_PRICE_APIS = https://blockchain.info/ticker, https://api.yadio.io/exrates/BTC, https://bitpay.com/rates/BTC # Host e.g. robosats.com HOST_NAME = '' diff --git a/api/tasks.py b/api/tasks.py index 97ad3fd8..0caac7e8 100644 --- a/api/tasks.py +++ b/api/tasks.py @@ -169,6 +169,9 @@ def cache_market(): print("SOFT LIMIT REACHED. Could not fetch current external market prices.") return + if not exchange_rates: + return + results = {} for i in range( len(Currency.currency_dict.values()) diff --git a/api/utils.py b/api/utils.py index 4c0b6a5a..949e776a 100644 --- a/api/utils.py +++ b/api/utils.py @@ -108,8 +108,27 @@ def get_exchange_rates(currencies): except Exception: yadio_rates.append(np.nan) api_rates.append(yadio_rates) - except Exception: - print(f"Could not fetch BTC prices from {api_url}") + + # Tor proxied requests to bitpay.com will fail. Skip if USE_TOR is enabled. + elif "bitpay.com" in api_url and not USE_TOR: + headers = { + "X-Accept-Version": "2.0.0", + "Content-type": "application/json", + } + bitpay_prices = session.get(api_url, headers=headers).json() + bitpay_prices = { + item["code"]: item["rate"] for item in bitpay_prices["data"] + } + bitpay_rates = [] + for currency in currencies: + try: + bitpay_rates.append(float(bitpay_prices[currency])) + except Exception: + bitpay_rates.append(np.nan) + api_rates.append(bitpay_rates) + + except Exception as e: + print(f"Could not fetch BTC prices from {api_url}: {str(e)}") pass if len(api_rates) == 0: