I made a rough prototype that works with websockets, let's try to integrate it.
import websockets
import asyncio
import json
import requests
import sys
def get_listen_key(api_key):
print('getting listen key')
url = 'https://api.binance.com/api/v1/userDataStream'
resp = requests.post(url, headers={'X-MBX-APIKEY': api_key})
return resp.json()['listenKey']
async def binance_notify(listen_key):
async with websockets.connect(f'wss://stream.binance.com:9443/ws/{listen_key}') as ws:
print('connected to websocket')
while True:
msg = await ws.recv()
data = json.loads(msg)
if data['e'] == 'executionReport':
pair = data['s']
kind = data['S'].lower()
price = data['p']
amount = data['q']
state = data['X'].lower()
print(kind, amount, pair, price, state)
if __name__ == '__main__':
listen_key = get_listen_key(sys.argv[1])
loop = asyncio.get_event_loop()
loop.run_until_complete(binance_notify(listen_key))
I made a rough prototype that works with websockets, let's try to integrate it.