From 08ba3f5967c38ed6af016f3b7029b68475186a40 Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 09:35:34 +0800 Subject: [PATCH 1/7] fix libcryptomarket.api not found --- libcryptomarket/__init__.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libcryptomarket/__init__.py b/libcryptomarket/__init__.py index 6d5350e..5facc07 100644 --- a/libcryptomarket/__init__.py +++ b/libcryptomarket/__init__.py @@ -5,3 +5,6 @@ __author__ = """Gavin Chan""" __email__ = 'gavincyi@gmail.com' __version__ = '0.1.0' +__all__ = ['api'] + +from . import api From 276beeb189b8cd797167605683ff01f027bd3d8d Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 09:45:54 +0800 Subject: [PATCH 2/7] update cryptocompare coinlist api --- libcryptomarket/api/cryptocompare_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcryptomarket/api/cryptocompare_api.py b/libcryptomarket/api/cryptocompare_api.py index 524e241..b1146c4 100644 --- a/libcryptomarket/api/cryptocompare_api.py +++ b/libcryptomarket/api/cryptocompare_api.py @@ -51,7 +51,7 @@ class CryptocompareHisto: def get_coinlist(): """Return general info for all coins available. """ - url = API_URL + "coinlist" + url = API_URL + "all/coinlist" return requests.get(url).json() From 809f835f740ec8dfdeb05cdcb3a0fd097bbcc4d4 Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 10:21:26 +0800 Subject: [PATCH 3/7] prevent error at this level when no data returned --- libcryptomarket/historical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcryptomarket/historical.py b/libcryptomarket/historical.py index df25247..410f79d 100644 --- a/libcryptomarket/historical.py +++ b/libcryptomarket/historical.py @@ -87,7 +87,7 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None, data = pd.DataFrame([CryptocompareHisto(**e).__dict__ for e in data]) # Filter only valid time range - if from_time is not None and to_time is not None: + if len(data) and from_time is not None and to_time is not None: data = data[(data['r_time'] >= from_time) & (data['r_time'] <= to_time)] From 7cb66629b33e3860650299f7cd7a7e43e1b7a739 Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 10:21:48 +0800 Subject: [PATCH 4/7] raise error and log api messages --- libcryptomarket/api/cryptocompare_api.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libcryptomarket/api/cryptocompare_api.py b/libcryptomarket/api/cryptocompare_api.py index b1146c4..3cc8227 100644 --- a/libcryptomarket/api/cryptocompare_api.py +++ b/libcryptomarket/api/cryptocompare_api.py @@ -1,10 +1,13 @@ #!/bin/python import requests from datetime import datetime +import logging API_URL = "https://min-api.cryptocompare.com/data/" MAX_QUERY_LIMIT = 2000 +logger = logging.getLogger(__name__) + class CryptocompareCoinlist: """Cryptocompare coinlist. @@ -83,4 +86,16 @@ def get_histo(period, fsym, tsym, e, limit=None, toTs=None): if toTs is not None: params["toTs"] = toTs - return requests.get(url, params=params).json() + r = requests.get(url, params=params) + + # Errors and warnings + r.raise_for_status() + + # The api raises a 200 for a warning + rjson = r.json() + if len(rjson['Data']) == 0: + logger.warning(rjson["Message"]) + elif rjson.get("Message", None): + logger.info(rjson["Message"]) + + return rjson From 813ca0b924df0b716632a0bf90f45c29f131ce9e Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 11:03:04 +0800 Subject: [PATCH 5/7] better logging --- libcryptomarket/api/cryptocompare_api.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libcryptomarket/api/cryptocompare_api.py b/libcryptomarket/api/cryptocompare_api.py index 3cc8227..ff7faad 100644 --- a/libcryptomarket/api/cryptocompare_api.py +++ b/libcryptomarket/api/cryptocompare_api.py @@ -88,14 +88,12 @@ def get_histo(period, fsym, tsym, e, limit=None, toTs=None): r = requests.get(url, params=params) - # Errors and warnings + # Raise html error status r.raise_for_status() - # The api raises a 200 for a warning + # The api raises a 200 for a warning, but passes a message rjson = r.json() - if len(rjson['Data']) == 0: - logger.warning(rjson["Message"]) - elif rjson.get("Message", None): - logger.info(rjson["Message"]) + if rjson.get("Message", None): + logger.warning('api returned message %r, for url %r', rjson["Message"], r.url) return rjson From 2972908cb99d456a841afd5b311393dd280e2649 Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 11:03:23 +0800 Subject: [PATCH 6/7] handle no data being returned without exception --- libcryptomarket/historical.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libcryptomarket/historical.py b/libcryptomarket/historical.py index 410f79d..e6eb121 100644 --- a/libcryptomarket/historical.py +++ b/libcryptomarket/historical.py @@ -85,9 +85,12 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None, else: data += func()['Data'] + if len(data) == 0: + return data + data = pd.DataFrame([CryptocompareHisto(**e).__dict__ for e in data]) # Filter only valid time range - if len(data) and from_time is not None and to_time is not None: + if from_time is not None and to_time is not None: data = data[(data['r_time'] >= from_time) & (data['r_time'] <= to_time)] From 7351c794ed43b11fdd187cc9c5e85eb4275c554a Mon Sep 17 00:00:00 2001 From: wassname Date: Fri, 10 Nov 2017 11:03:46 +0800 Subject: [PATCH 7/7] handle to_symb longer than 3 chars on cryptocompare --- libcryptomarket/historical.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libcryptomarket/historical.py b/libcryptomarket/historical.py index e6eb121..b1e070f 100644 --- a/libcryptomarket/historical.py +++ b/libcryptomarket/historical.py @@ -42,7 +42,7 @@ def get_historical_prices(source='cryptocompare', symbol=None, exchange=None, # Parse from (first 3) and to (last 3) symbol from the parameter # symbol. from_sym = symbol[:3] - to_sym = symbol[3:6] + to_sym = symbol[3:] func = partial(get_histo, period=period, fsym=from_sym, tsym=to_sym, e=exchange)