From e5f75a8235ad6cba9eaf75ed556af9084cbf37d6 Mon Sep 17 00:00:00 2001 From: acidvegas Date: Wed, 22 Apr 2020 02:57:55 -0400 Subject: [PATCH] Fixed null values --- coinmarketcap.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/coinmarketcap.py b/coinmarketcap.py index 6d8201b..382dc0d 100644 --- a/coinmarketcap.py +++ b/coinmarketcap.py @@ -6,6 +6,15 @@ import json import time import zlib +# Find a better way to do this... +def replace_nulls(json_elem): + if isinstance(json_elem, list): + return [replace_nulls(elem) for elem in json_elem] + elif isinstance(json_elem, dict): + return {key: replace_nulls(value) for key, value in json_elem.items()} + else: + return '0' if json_elem is None else json_elem + class CoinMarketCap(object): def __init__(self, api_key): self.api_key = api_key @@ -39,7 +48,7 @@ class CoinMarketCap(object): if time.time() - self.last['ticker'] < 300: return self.cache['ticker'] else: - data = self._api('cryptocurrency/listings/latest?limit=5000') + data = replace_nulls(self._api('cryptocurrency/listings/latest?limit=5000')) self.cache['ticker'] = dict() for item in data: self.cache['ticker'][item['id']] = {