Fixed null values

This commit is contained in:
Dionysus 2020-04-22 02:58:42 -04:00
parent b82fe23f80
commit b68288566a
Signed by: acidvegas
GPG Key ID: EF4B922DB85DC9DE
2 changed files with 11 additions and 1 deletions

1
irccex/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
*.db

View File

@ -6,6 +6,14 @@ import json
import time import time
import zlib import zlib
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): class CoinMarketCap(object):
def __init__(self, api_key): def __init__(self, api_key):
self.api_key = api_key self.api_key = api_key
@ -15,7 +23,7 @@ class CoinMarketCap(object):
def _api(self, _endpoint): def _api(self, _endpoint):
conn = http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15) conn = http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15)
conn.request('GET', '/v1/' + _endpoint, headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key}) conn.request('GET', '/v1/' + _endpoint, headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key})
response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8').replace(': null', ': "0"') response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8')
conn.close() conn.close()
return json.loads(response)['data'] return json.loads(response)['data']
@ -40,6 +48,7 @@ class CoinMarketCap(object):
return self.cache['ticker'] return self.cache['ticker']
else: else:
data = self._api('cryptocurrency/listings/latest?limit=5000') data = self._api('cryptocurrency/listings/latest?limit=5000')
data = replace_nulls(data)
self.cache['ticker'] = dict() self.cache['ticker'] = dict()
for item in data: for item in data:
self.cache['ticker'][item['symbol']] = { self.cache['ticker'][item['symbol']] = {