|
| 1 | +""" |
| 2 | +Python wrapper for cryptocompare API |
| 3 | +""" |
| 4 | + |
| 5 | +import requests |
| 6 | +import time |
| 7 | +import quo |
| 8 | +import datetime |
| 9 | +import typing |
| 10 | +import os |
| 11 | +from typing import Union, Optional, List, Dict |
| 12 | +Timestamp = Union[datetime.datetime, datetime.date, int, float] |
| 13 | + |
| 14 | +# API |
| 15 | +_API_KEY_PARAMETER = "" |
| 16 | +_URL_COIN_LIST = 'https://www.cryptocompare.com/api/data/coinlist?' |
| 17 | +_URL_PRICE = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms={}&tsyms={}' |
| 18 | +_URL_PRICE_MULTI = 'https://min-api.cryptocompare.com/data/pricemulti?fsyms={}&tsyms={}' |
| 19 | +_URL_PRICE_MULTI_FULL = 'https://min-api.cryptocompare.com/data/pricemultifull?fsyms={}&tsyms={}' |
| 20 | +_URL_HIST_PRICE = 'https://min-api.cryptocompare.com/data/pricehistorical?fsym={}&tsyms={}&ts={}&e={}' |
| 21 | +_URL_HIST_PRICE_DAY = 'https://min-api.cryptocompare.com/data/histoday?fsym={}&tsym={}&limit={}&e={}&toTs={}' |
| 22 | +_URL_HIST_PRICE_HOUR = 'https://min-api.cryptocompare.com/data/histohour?fsym={}&tsym={}&limit={}&e={}&toTs={}' |
| 23 | +_URL_HIST_PRICE_MINUTE = 'https://min-api.cryptocompare.com/data/histominute?fsym={}&tsym={}&limit={}&e={}&toTs={}' |
| 24 | +_URL_AVG = 'https://min-api.cryptocompare.com/data/generateAvg?fsym={}&tsym={}&e={}' |
| 25 | +_URL_EXCHANGES = 'https://www.cryptocompare.com/api/data/exchanges?' |
| 26 | +_URL_PAIRS = 'https://min-api.cryptocompare.com/data/pair/mapping/exchange?e={}' |
| 27 | + |
| 28 | +# DEFAULTS |
| 29 | +CURRENCY = 'USD' |
| 30 | +LIMIT = 1440 |
| 31 | +############################################################################### |
| 32 | + |
| 33 | + |
| 34 | +def _query_cryptocompare(url: str, errorCheck: bool = True, api_key: str = None) -> Optional[Dict]: |
| 35 | + """ |
| 36 | + Query the url and return the result or None on failure. |
| 37 | + |
| 38 | + :param url: the url |
| 39 | + :param errorCheck: run extra error checks (default: True) |
| 40 | + :returns: respones, or nothing if errorCheck=True |
| 41 | + :api_key: optional, if you want to add an API Key |
| 42 | + """ |
| 43 | + api_key_parameter = _set_api_key_parameter(api_key) |
| 44 | + try: |
| 45 | + response = requests.get(url + api_key_parameter).json() |
| 46 | + except Exception as e: |
| 47 | + quo.echo('Error getting coin information. %s' % str(e)) |
| 48 | + return None |
| 49 | + if errorCheck and (response.get('Response') == 'Error'): |
| 50 | + quo.echo('[ERROR] %s' % response.get('Message')) |
| 51 | + return None |
| 52 | + return response |
| 53 | + |
| 54 | + |
| 55 | +def _format_parameter(parameter: object) -> str: |
| 56 | + """ |
| 57 | + Format the parameter depending on its type and return |
| 58 | + the string representation accepted by the API. |
| 59 | + |
| 60 | + :param parameter: parameter to format |
| 61 | + """ |
| 62 | + if isinstance(parameter, list): |
| 63 | + return ','.join(parameter) |
| 64 | + |
| 65 | + else: |
| 66 | + return str(parameter) |
| 67 | + |
| 68 | + |
| 69 | +def _format_timestamp(timestamp: Timestamp) -> int: |
| 70 | + """ |
| 71 | + Format the timestamp depending on its type and return |
| 72 | + the integer representation accepted by the API. |
| 73 | + |
| 74 | + :param timestamp: timestamp to format |
| 75 | + """ |
| 76 | + if isinstance(timestamp, datetime.datetime) or isinstance(timestamp, datetime.date): |
| 77 | + return int(time.mktime(timestamp.timetuple())) |
| 78 | + return int(timestamp) |
| 79 | + |
| 80 | + |
| 81 | +def _set_api_key_parameter(api_key: str = None) -> str: |
| 82 | + if api_key is None: |
| 83 | + api_key = os.getenv('CRYPTOCOMPARE_API_KEY') |
| 84 | + if api_key is not None: |
| 85 | + _API_KEY = "&api_key={}".format(api_key) |
| 86 | + return _API_KEY |
| 87 | + return "" |
| 88 | + |
| 89 | +############################################################################### |
| 90 | + |
| 91 | + |
| 92 | +def coin_list(format: bool = False) -> Union[Dict, List, None]: |
| 93 | + """ |
| 94 | + Get the coin list (all available coins). |
| 95 | + |
| 96 | + :param format: format as python list (default: False) |
| 97 | + :returns: dict or list of available coins |
| 98 | + """ |
| 99 | + response = _query_cryptocompare(_URL_COIN_LIST, False) |
| 100 | + if response: |
| 101 | + response = typing.cast(Dict, response['Data']) |
| 102 | + return list(response.keys()) if format else response |
| 103 | + return None |
| 104 | + |
| 105 | +# TODO: add option to filter json response according to a list of fields |
| 106 | + |
| 107 | + |
| 108 | +def get_price(coin: str, currency: str = CURRENCY, full: bool = False) -> Optional[Dict]: |
| 109 | + """ |
| 110 | + Get the currencyent price of a coin in a given currency. |
| 111 | + |
| 112 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 113 | + :param currency: short hand description of the currency (e.g. EUR) |
| 114 | + :param full: full response or just the price (default: False) |
| 115 | + :returns: dict of coin and currency price pairs |
| 116 | + """ |
| 117 | + if full: |
| 118 | + return _query_cryptocompare( |
| 119 | + _URL_PRICE_MULTI_FULL.format( |
| 120 | + _format_parameter(coin), _format_parameter(currency)) |
| 121 | + ) |
| 122 | + if isinstance(coin, list): |
| 123 | + return _query_cryptocompare( |
| 124 | + _URL_PRICE_MULTI.format(_format_parameter(coin), |
| 125 | + _format_parameter(currency)) |
| 126 | + ) |
| 127 | + return _query_cryptocompare( |
| 128 | + _URL_PRICE.format(coin, _format_parameter(currency)) |
| 129 | + ) |
| 130 | + |
| 131 | + |
| 132 | +def price(coin: str, currency: str = CURRENCY, timestamp: Timestamp = time.time(), |
| 133 | + exchange: str = 'CCCAGG') -> Optional[Dict]: |
| 134 | + """ |
| 135 | + Get the price of a coin in a given currency during a specific time. |
| 136 | + |
| 137 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 138 | + :param currency: short hand description of the currency (e.g. EUR) |
| 139 | + :param timestamp: point in time |
| 140 | + :param exchange: the exchange to use |
| 141 | + :returns: dict of coin and currency price pairs |
| 142 | + """ |
| 143 | + return _query_cryptocompare( |
| 144 | + _URL_HIST_PRICE.format(coin, |
| 145 | + _format_parameter(currency), |
| 146 | + _format_timestamp(timestamp), |
| 147 | + _format_parameter(exchange)) |
| 148 | + ) |
| 149 | + |
| 150 | + |
| 151 | +def price_day(coin: str, currency: str = CURRENCY, limit: int = LIMIT, |
| 152 | + exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]: |
| 153 | + """ |
| 154 | + Get historical price (day). |
| 155 | + |
| 156 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 157 | + :param currency: short hand description of the currency (e.g. EUR) |
| 158 | + :param limit: number of data points (max. 2000) |
| 159 | + :param exchange: exchange to use (default: 'CCCAGG') |
| 160 | + :param toTs: return data before this timestamp. (Unix epoch time or datetime object) |
| 161 | + :returns: dict of coin and currency price pairs |
| 162 | + """ |
| 163 | + response = _query_cryptocompare( |
| 164 | + _URL_HIST_PRICE_DAY.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs))) |
| 165 | + if response: |
| 166 | + return response['Data'] |
| 167 | + return None |
| 168 | + |
| 169 | + |
| 170 | +def price_hour(coin: str, currency: str = CURRENCY, limit: int = LIMIT, |
| 171 | + exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]: |
| 172 | + """ |
| 173 | + Get historical price (hourly). |
| 174 | + |
| 175 | + |
| 176 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 177 | + :param currency: short hand description of the currency (e.g. EUR) |
| 178 | + :param limit: number of data points (max. 2000) |
| 179 | + :param exchange: exchange to use (default: 'CCCAGG') |
| 180 | + :param toTs: return data before this timestamp. (Unix epoch time or datetime object) |
| 181 | + :returns: dict of coin and currency price pairs |
| 182 | + """ |
| 183 | + response = _query_cryptocompare( |
| 184 | + _URL_HIST_PRICE_HOUR.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs))) |
| 185 | + if response: |
| 186 | + return response['Data'] |
| 187 | + return None |
| 188 | + |
| 189 | + |
| 190 | +def price_minute(coin: str, currency: str = CURRENCY, limit: int = LIMIT, |
| 191 | + exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]: |
| 192 | + """ |
| 193 | + Get historical price (minute). |
| 194 | + |
| 195 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 196 | + :param currency: short hand description of the currency (e.g. EUR) |
| 197 | + :param limit: number of data points (max. 2000) |
| 198 | + :param exchange: exchange to use (default: 'CCCAGG') |
| 199 | + :param toTs: return data before this timestamp. (Unix epoch time or datetime object) |
| 200 | + :returns: dict of coin and currency price pairs |
| 201 | + """ |
| 202 | + response = _query_cryptocompare( |
| 203 | + _URL_HIST_PRICE_MINUTE.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs))) |
| 204 | + if response: |
| 205 | + return response['Data'] |
| 206 | + return None |
| 207 | + |
| 208 | + |
| 209 | +def get_average(coin: str, currency: str = CURRENCY, exchange: str = 'CCCAGG') -> Optional[Dict]: |
| 210 | + """ |
| 211 | + Get the average price |
| 212 | + |
| 213 | + :param coin: symbolic name of the coin (e.g. BTC) |
| 214 | + :param currency: short hand description of the currency (e.g. EUR) |
| 215 | + :param exchange: exchange to use (default: 'CCCAGG') |
| 216 | + :returns: dict of coin and currency price pairs |
| 217 | + """ |
| 218 | + response = _query_cryptocompare(_URL_AVG.format( |
| 219 | + coin, currency, _format_parameter(exchange))) |
| 220 | + if response: |
| 221 | + return response['RAW'] |
| 222 | + return None |
| 223 | + |
| 224 | + |
| 225 | +def get_exchanges() -> Optional[Dict]: |
| 226 | + """ |
| 227 | + Get the list of available exchanges. |
| 228 | + |
| 229 | + :returns: list of available exchanges |
| 230 | + """ |
| 231 | + response = _query_cryptocompare(_URL_EXCHANGES) |
| 232 | + if response: |
| 233 | + return response['Data'] |
| 234 | + return None |
| 235 | + |
| 236 | + |
| 237 | +def get_pairs(exchange: str = None) -> Optional[Dict]: |
| 238 | + """ |
| 239 | + Get the list of available pairs for a particular exchange or for |
| 240 | + all exchanges (if exchange is None) |
| 241 | + |
| 242 | + :param exchange: exchange to use (default: None) |
| 243 | + :returns: list of available exchanges |
| 244 | + """ |
| 245 | + if exchange is None: |
| 246 | + response = _query_cryptocompare(_URL_PAIRS.split('?')[0]) |
| 247 | + |
| 248 | + else: |
| 249 | + response = _query_cryptocompare(_URL_PAIRS.format(exchange)) |
| 250 | + if response: |
| 251 | + return response['Data'] |
| 252 | + return None |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + |
| 257 | + |
| 258 | +__version__ = "1.0.1" |
0 commit comments