←︎ irccex :: b682885


1
commit b68288566a6c92a22949cd39634e54f5a792b196 (HEAD -> master, origin/master)
2
Author: acidvegas <acid.vegas@acid.vegas>
3
Date:   Wed Apr 22 02:58:42 2020 -0400
4
5
    Fixed null values
6
---
7
 irccex/.gitignore            |  1 +
8
 irccex/core/coinmarketcap.py | 11 ++++++++++-
9
 2 files changed, 11 insertions(+), 1 deletion(-)
10
11
diff --git a/irccex/.gitignore b/irccex/.gitignore
12
new file mode 100644
13
index 0000000..98e6ef6
14
--- /dev/null
15
+++ b/irccex/.gitignore
16
@@ -0,0 +1 @@
17
+*.db
18
diff --git a/irccex/core/coinmarketcap.py b/irccex/core/coinmarketcap.py
19
index 3d2cfe3..467efe6 100644
20
--- a/irccex/core/coinmarketcap.py
21
+++ b/irccex/core/coinmarketcap.py
22
@@ -6,6 +6,14 @@ import json
23
 import time
24
 import zlib
25
 
26
+def replace_nulls(json_elem):
27
+	if isinstance(json_elem, list):
28
+		return [replace_nulls(elem) for elem in json_elem]
29
+	elif isinstance(json_elem, dict):
30
+		return {key: replace_nulls(value) for key, value in json_elem.items()}
31
+	else:
32
+		return '0' if json_elem is None else json_elem
33
+
34
 class CoinMarketCap(object):
35
 	def __init__(self, api_key):
36
 		self.api_key = api_key
37
@@ -15,7 +23,7 @@ class CoinMarketCap(object):
38
 	def _api(self, _endpoint):
39
 		conn = http.client.HTTPSConnection('pro-api.coinmarketcap.com', timeout=15)
40
 		conn.request('GET', '/v1/' + _endpoint, headers={'Accept':'application/json', 'Accept-Encoding':'deflate, gzip', 'X-CMC_PRO_API_KEY':self.api_key})
41
-		response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8').replace(': null', ': "0"')
42
+		response = zlib.decompress(conn.getresponse().read(), 16+zlib.MAX_WBITS).decode('utf-8')
43
 		conn.close()
44
 		return json.loads(response)['data']
45
 
46
@@ -40,6 +48,7 @@ class CoinMarketCap(object):
47
 			return self.cache['ticker']
48
 		else:
49
 			data = self._api('cryptocurrency/listings/latest?limit=5000')
50
+			data = replace_nulls(data)
51
 			self.cache['ticker'] = dict()
52
 			for item in data:
53
 				self.cache['ticker'][item['symbol']] = {