Skip to content

Commit

Permalink
Moved formatting of timestamps to its own function, and removed the t…
Browse files Browse the repository at this point in the history
…oTs parameter from get_historical_price_minute, since historical data is unavailable without an API key.
  • Loading branch information
duppie4000 committed Feb 20, 2020
1 parent 715dea6 commit a82e28f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
25 changes: 19 additions & 6 deletions cryptocompare/cryptocompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,24 @@ def _format_parameter(parameter: object) -> str:
"""
if isinstance(parameter, list):
return ','.join(parameter)
elif isinstance(parameter, datetime.datetime):
return str(time.mktime(parameter.timetuple()))

else:
return str(parameter)


def _format_timestamp(timestamp) -> int:
"""
Format the timestamp depending on its type and return
the integer representation accepted by the API.
:param timestamp: timestamp to format
"""
if isinstance(timestamp, datetime.datetime):
return int(time.mktime(timestamp.timetuple()))
else:
return int(timestamp)


###############################################################################


Expand Down Expand Up @@ -110,7 +123,7 @@ def get_historical_price(coin: str, curr: str = CURR, timestamp: Timestamp = tim
:returns: dict of coin and currency price pairs
"""
return _query_cryptocompare(_URL_HIST_PRICE.format(coin, _format_parameter(curr),
_format_parameter(timestamp), _format_parameter(exchange)))
_format_timestamp(timestamp), _format_parameter(exchange)))


def get_historical_price_day(coin: str, curr: str = CURR, limit: int = LIMIT,
Expand All @@ -126,7 +139,7 @@ def get_historical_price_day(coin: str, curr: str = CURR, limit: int = LIMIT,
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_DAY.format(coin, _format_parameter(curr), limit, exchange, _format_parameter(toTs)))
_URL_HIST_PRICE_DAY.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None
Expand All @@ -146,7 +159,7 @@ def get_historical_price_hour(coin: str, curr: str = CURR, limit: int = LIMIT,
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_HOUR.format(coin, _format_parameter(curr), limit, exchange, _format_parameter(toTs)))
_URL_HIST_PRICE_HOUR.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None
Expand All @@ -165,7 +178,7 @@ def get_historical_price_minute(coin: str, curr: str = CURR, limit: int = LIMIT,
:returns: dict of coin and currency price pairs
"""
response = _query_cryptocompare(
_URL_HIST_PRICE_MINUTE.format(coin, _format_parameter(curr), limit, exchange, _format_parameter(toTs)))
_URL_HIST_PRICE_MINUTE.format(coin, _format_parameter(curr), limit, exchange, _format_timestamp(toTs)))
if response:
return response['Data']
return None
Expand Down
2 changes: 1 addition & 1 deletion tests/test_cryptocompare.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def test_price_hour(self):
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(2019,6,6,12,55))
price = cryptocompare.get_historical_price_minute(coin, curr=curr, limit=3, exchange='CCCAGG')
for frame in price:
self.assertIn('time', frame)

Expand Down

0 comments on commit a82e28f

Please sign in to comment.