Skip to content

Instantly share code, notes, and snippets.

@uneasyguy
Last active January 30, 2021 14:31
Show Gist options
  • Save uneasyguy/2f2053e918e773791991bfe60060d6d8 to your computer and use it in GitHub Desktop.
Save uneasyguy/2f2053e918e773791991bfe60060d6d8 to your computer and use it in GitHub Desktop.
from time import sleep
from datetime import datetime
from binance.client import Client
SECONDS_IN_MINUTE = 60
def _sample_crude_api_limiting(headers, threshold=6, delay_buffer=5):
used_api_weight = headers.get('x-mbx-used-weight-1m', 0)
print(f'Used API Weight: {used_api_weight}')
if int(used_api_weight) >= threshold:
seconds_into_current_minute = int(datetime.now().strftime("%S"))
wait_time = (SECONDS_IN_MINUTE + delay_buffer) - seconds_into_current_minute
print(f'Going to sleep for {wait_time} seconds to avoid rate limit errors')
sleep(wait_time)
print('I am alive again!')
def main():
try:
x=0
while x < 15:
client = Client()
some_call_here = client.ping()
headers = client.response.headers
# print(f'headers: {headers}') #comment this line in to view all headers
_sample_crude_api_limiting(headers)
x+=1
except Exception as e:
print(f'Whoops, we ran into the following error: {e}')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment