Skip to content

Commit

Permalink
Merge branch 'master' into feat-add-api-key-passing
Browse files Browse the repository at this point in the history
  • Loading branch information
lagerfeuer authored Jan 25, 2021
2 parents 8e51826 + 33a4d6c commit 7b12781
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 52 deletions.
44 changes: 22 additions & 22 deletions cryptocompare/cryptocompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
_URL_PAIRS = 'https://min-api.cryptocompare.com/data/pair/mapping/exchange?e={}'

# DEFAULTS
CURR = 'EUR'
CURRENCY = 'EUR'
LIMIT = 1440
###############################################################################

Expand Down Expand Up @@ -103,118 +103,118 @@ def get_coin_list(format: bool = False) -> Union[Dict, List, None]:
# TODO: add option to filter json response according to a list of fields


def get_price(coin: str, curr: str = CURR, full: bool = False) -> Optional[Dict]:
def get_price(coin: str, currency: str = CURRENCY, full: bool = False) -> Optional[Dict]:
"""
Get the current price of a coin in a given currency.
Get the currencyent price of a coin in a given currency.
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param full: full response or just the price (default: False)
:returns: dict of coin and currency price pairs
"""
if full:
return _query_cryptocompare(
_URL_PRICE_MULTI_FULL.format(
_format_parameter(coin), _format_parameter(curr))
_format_parameter(coin), _format_parameter(currency))
)
if isinstance(coin, list):
return _query_cryptocompare(
_URL_PRICE_MULTI.format(_format_parameter(coin),
_format_parameter(curr))
_format_parameter(currency))
)
return _query_cryptocompare(
_URL_PRICE.format(coin, _format_parameter(curr))
_URL_PRICE.format(coin, _format_parameter(currency))
)


def get_historical_price(coin: str, curr: str = CURR, timestamp: Timestamp = time.time(),
def get_historical_price(coin: str, currency: str = CURRENCY, timestamp: Timestamp = time.time(),
exchange: str = 'CCCAGG') -> Optional[Dict]:
"""
Get the price of a coin in a given currency during a specific time.
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param timestamp: point in time
:param exchange: the exchange to use
:returns: dict of coin and currency price pairs
"""
return _query_cryptocompare(
_URL_HIST_PRICE.format(coin,
_format_parameter(curr),
_format_parameter(currency),
_format_timestamp(timestamp),
_format_parameter(exchange))
)


def get_historical_price_day(coin: str, curr: str = CURR, limit: int = LIMIT,
def get_historical_price_day(coin: str, currency: str = CURRENCY, limit: int = LIMIT,
exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]:
"""
Get historical price (day).
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param limit: number of data points (max. 2000)
:param exchange: exchange to use (default: 'CCCAGG')
:param toTs: return data before this timestamp. (Unix epoch time or datetime object)
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_DAY.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
_URL_HIST_PRICE_DAY.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None


def get_historical_price_hour(coin: str, curr: str = CURR, limit: int = LIMIT,
def get_historical_price_hour(coin: str, currency: str = CURRENCY, limit: int = LIMIT,
exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]:
"""
Get historical price (hourly).
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param limit: number of data points (max. 2000)
:param exchange: exchange to use (default: 'CCCAGG')
:param toTs: return data before this timestamp. (Unix epoch time or datetime object)
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_HOUR.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
_URL_HIST_PRICE_HOUR.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None


def get_historical_price_minute(coin: str, curr: str = CURR, limit: int = LIMIT,
def get_historical_price_minute(coin: str, currency: str = CURRENCY, limit: int = LIMIT,
exchange: str = 'CCCAGG', toTs: Timestamp = time.time()) -> Optional[Dict]:
"""
Get historical price (minute).
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param limit: number of data points (max. 2000)
:param exchange: exchange to use (default: 'CCCAGG')
:param toTs: return data before this timestamp. (Unix epoch time or datetime object)
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_MINUTE.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
_URL_HIST_PRICE_MINUTE.format(coin, _format_parameter(currency), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None


def get_avg(coin: str, curr: str = CURR, exchange: str = 'CCCAGG') -> Optional[Dict]:
def get_avg(coin: str, currency: str = CURRENCY, exchange: str = 'CCCAGG') -> Optional[Dict]:
"""
Get the average price
:param coin: symbolic name of the coin (e.g. BTC)
:param curr: short hand description of the currency (e.g. EUR)
:param currency: short hand description of the currency (e.g. EUR)
:param exchange: exchange to use (default: 'CCCAGG')
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(_URL_AVG.format(
coin, curr, _format_parameter(exchange)))
coin, currency, _format_parameter(exchange)))
if response:
return response['RAW']
return None
Expand Down
50 changes: 26 additions & 24 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,40 +1,42 @@
astroid==2.4.2
attrs==20.1.0
attrs==20.3.0
autopep8==1.5.4
certifi==2020.6.20
chardet==3.0.4
coverage==5.2.1
coveralls==2.1.2
certifi==2020.12.5
chardet==4.0.0
coverage==5.3.1
coveralls==3.0.0
doc8==0.8.1
docopt==0.6.2
docutils==0.16
filelock==3.0.12
idna==2.10
isort==5.4.2
lazy-object-proxy==1.5.1
iniconfig==1.1.1
isort==5.7.0
lazy-object-proxy==1.4.3
mccabe==0.6.1
more-itertools==8.4.0
mypy==0.782
more-itertools==8.6.0
mypy==0.800
mypy-extensions==0.4.3
packaging==20.4
pbr==5.4.5
packaging==20.8
pbr==5.5.1
pluggy==0.13.1
public==2020.7.1
py==1.9.0
public==2020.12.3
py==1.10.0
pycodestyle==2.6.0
Pygments==2.7.4
pylint==2.6.0
pyparsing==2.4.7
pytest==6.0.1
pytest-cov==2.10.1
pytest-mypy==0.6.2
query-string==2020.7.1
requests==2.24.0
restructuredtext-lint==1.3.1
pytest==6.2.1
pytest-cov==2.11.1
pytest-mypy==0.8.0
query-string==2020.12.3
requests==2.25.1
restructuredtext-lint==1.3.2
six==1.15.0
stevedore==3.2.0
toml==0.10.1
typed-ast==1.4.1
typing-extensions==3.7.4.2
urllib3==1.25.10
stevedore==3.3.0
toml==0.10.2
typed-ast==1.4.2
typing-extensions==3.7.4.3
urllib3==1.26.2
wcwidth==0.2.5
wrapt==1.12.1
13 changes: 7 additions & 6 deletions tests/test_cryptocompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ def test_get_price(self):
coin = 'BTC'
price = cryptocompare.get_price(coin)
self.assertCoinAndCurrInPrice(coin, 'EUR', price)
price = cryptocompare.get_price(coin, curr='USD')

price = cryptocompare.get_price(coin, currency='USD')
self.assertCoinAndCurrInPrice(coin, 'USD', price)
currencies = ['EUR', 'USD', 'GBP']
price = cryptocompare.get_price(coin, curr=currencies)
price = cryptocompare.get_price(coin, currency=currencies)
self.assertCoinAndCurrInPrice(coin, currencies, price)
coins = ['BTC', 'XMR']
price = cryptocompare.get_price(coins, curr=currencies)
price = cryptocompare.get_price(coins, currency=currencies)
self.assertCoinAndCurrInPrice(coins, currencies, price)

def test_get_price_full(self):
Expand All @@ -62,23 +63,23 @@ def test_price_day(self):
coin = 'BTC'
curr = 'USD'
price = cryptocompare.get_historical_price_day(
coin, curr=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime(2019, 6, 6))
coin, currency=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime(2019, 6, 6))
for frame in price:
self.assertIn('time', frame)

def test_price_hour(self):
coin = 'BTC'
curr = 'USD'
price = cryptocompare.get_historical_price_hour(
coin, curr=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime(2019, 6, 6, 12))
coin, currency=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime(2019, 6, 6, 12))
for frame in price:
self.assertIn('time', frame)

def test_price_minute(self):
coin = 'BTC'
curr = 'USD'
price = cryptocompare.get_historical_price_minute(
coin, curr=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime.now())
coin, currency=curr, limit=3, exchange='CCCAGG', toTs=datetime.datetime.now())
for frame in price:
self.assertIn('time', frame)

Expand Down

0 comments on commit 7b12781

Please sign in to comment.