diff --git a/hotbit/reverseApi.py b/hotbit/reverseApi.py index a4334f0..01198a3 100644 --- a/hotbit/reverseApi.py +++ b/hotbit/reverseApi.py @@ -1,12 +1,38 @@ import requests import requestsWS from decimal import * +from datetime import datetime +import time + +DT_FORMAT = "%d/%m/%Y %H:%M:%S" def round_down(value, decimals): # https://stackoverflow.com/questions/41383787/round-down-to-2-decimal-in-python with localcontext() as ctx: ctx.rounding = ROUND_DOWN return round(value, decimals) +def check_date_format(date_str): + try: + datetime.strptime(date_str, DT_FORMAT) + except ValueError: + print("This is the incorrect date string format. It should be DD-MM-YYYY") + return False + else: + return True + +def date_str_to_timestamp(date_str=None): + if isinstance(date_str, str): + print("date : ", date_str) + is_valid = check_date_format(date_str) + timestamp = time.mktime( + datetime.strptime(date_str, DT_FORMAT).timetuple() + ) + + return { + "is_valid": is_valid, + "timestamp": timestamp + } + class Hotbit: def __init__(self, auth): self.session = requests.Session() @@ -181,7 +207,50 @@ def order(self, price, amount, market, side, type="LIMIT", hide=False, use_disco } resp = self.session.post("https://www.hotbit.pro/v1/order/create?platform=web", headers=self.defaultHeaders, data=payload) return resp.json() + + def klineQuery(self, market, start, end, interval=60): + market = market.replace("/", "") + time_start_dict = date_str_to_timestamp(start) + time_end_dict = date_str_to_timestamp(end) + timestamp_start = int(time_start_dict.get("timestamp")) + timestamp_end = int(time_end_dict.get("timestamp")) + + payload = { + "method": "kline.query", + "params": [ + market, + timestamp_start, + timestamp_end, + interval + ], + "id": 100 + } + + resp = self.sessionWS.post( + 'wss://ws.hotbit.io/', + json=payload, + encryption="gzip", + identifiers={"id": 100} + ) + return resp.json() + + def priceQuery(self, market): + market = market.replace("/", "") + + payload = { + "method":"price.query", + "params":[market], + "id":100 + } + + resp = self.sessionWS.post( + 'wss://ws.hotbit.io/', + json=payload, + encryption="gzip", + identifiers={"id": 100} + ) + return resp.json() def customWS(self, whatToSend): resp = self.sessionWS.post('wss://ws.hotbit.io/', json=whatToSend, encryption="gzip").text @@ -189,4 +258,4 @@ def customWS(self, whatToSend): def customHTTP(self, url, whatToSend): resp = self.session.post(url, json=whatToSend).text - return resp \ No newline at end of file + return resp