From bd3489ea71da5cdc6fa8ae2820dc7023cdad16d0 Mon Sep 17 00:00:00 2001 From: Chris Berkhout Date: Sat, 26 Aug 2023 10:38:31 +0200 Subject: [PATCH] Handle coinmarketcap return null for some prices. --- src/pricehist/sources/coinmarketcap.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/pricehist/sources/coinmarketcap.py b/src/pricehist/sources/coinmarketcap.py index 313c493..7da1a25 100644 --- a/src/pricehist/sources/coinmarketcap.py +++ b/src/pricehist/sources/coinmarketcap.py @@ -57,7 +57,8 @@ class CoinMarketCap(BaseSource): for item in data.get("quotes", []): d = item["time_open"][0:10] amount = self._amount(next(iter(item["quote"].values())), series.type) - prices.append(Price(d, amount)) + if amount is not None: + prices.append(Price(d, amount)) output_base, output_quote = self._output_pair(series.base, series.quote, data) @@ -155,12 +156,14 @@ class CoinMarketCap(BaseSource): return parsed["data"] def _amount(self, data, type): - if type in ["mid"]: + if type in ["mid"] and data["high"] is not None and data["low"] is not None: high = Decimal(str(data["high"])) low = Decimal(str(data["low"])) return sum([high, low]) / 2 - else: + elif type in data and data[type] is not None: return Decimal(str(data[type])) + else: + return None def _output_pair(self, base, quote, data): data_base = data["symbol"]