Astranome

ESP32BinAPI

Mar 20th, 2021 (edited)
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.44 KB | None | 0 0
  1. import urequests
  2. import ntptime
  3. import time
  4. import json
  5.  
  6. def ask():
  7.     current_rates = urequests.get('https://api.binance.com/api/v1/depth?symbol=BTCUSDT'); current_rates = json.loads(current_rates.text)
  8.  
  9.     #bid=float(current_rates['bids'][0][0]) ; bid
  10.     ask=float(current_rates['asks'][0][0]) ; ask
  11.     return ask
  12.  
  13. def bid():
  14.     current_rates = urequests.get('https://api.binance.com/api/v1/depth?symbol=BTCUSDT'); current_rates = json.loads(current_rates.text)
  15.  
  16.     bid=float(current_rates['bids'][0][0]) ; bid
  17.     #ask=float(current_rates['asks'][0][0]) ; ask
  18.     return bid
  19.  
  20.  
  21. def btime():
  22.     btime = urequests.request(method= 'GET', url='https://api.binance.com/api/v1/time', data="" ); btime = json.loads(btime.text)
  23.     btime=int(btime['serverTime'])
  24.     return btime
  25. #btime ()
  26.  
  27.  
  28. def price():
  29.     response = urequests.request(method= 'GET', url="https://api.binance.com/api/v3/ticker/price?symbol=LTCUSDT", data="")
  30.     if 'code' in response.text:
  31.         raise Exception(response.text)
  32.     print (response.json())
  33.  
  34. def sync():
  35.     #if needed, overwrite default time server
  36.     ntptime.host = "1.europe.pool.ntp.org"
  37.  
  38.     try:
  39.         print("Local time before synchronization:%s" %str(time.localtime()))
  40.         #make sure to have internet connection
  41.         ntptime.settime()
  42.         print("Local time after synchronization:%s" %str(time.localtime()))
  43.     except:
  44.         print("Error syncing time")
  45. #sync()
  46.    
Add Comment
Please, Sign In to add comment