This repository has been archived by the owner on May 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtoken_websocketclient.py
57 lines (53 loc) · 1.82 KB
/
token_websocketclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import websockets
import asyncio
import json
import async_timeout
API_ENDPOINT = 'wss://alpha.thundervigil.com/ws'
API_KEY = '' # signup on thundervigil.com/signup to get API key
async def consumer_contract():
async with websockets.connect(API_ENDPOINT) as ws:
await ws.send(json.dumps({'command': 'register', 'key': API_KEY}))
ack = await ws.recv()
print(ack)
ack = json.loads(ack)
try:
ack_cmd = ack['command']
except KeyError:
print('Bad response')
await ws.close()
return
else:
if ack_cmd != 'register:ack':
print('Registration not acknowledged')
await ws.close()
return
sessionID = ack['sessionID']
# async for msg in ws:
# print(msg)
count = 0
while True:
if count > 1000:
await ws.send(json.dumps({'command': 'unregister', 'key': API_KEY}))
ack = await ws.recv()
print(ack)
await ws.close()
break
try:
async with async_timeout.timeout(10):
msg = await ws.recv()
try:
msg = json.loads(msg)
except:
print(msg)
else:
if msg['type'] != 'contractmon':
print(json.dumps(msg)+'\n\n')
count += 1
except asyncio.TimeoutError:
await ws.send(json.dumps({'command': 'heartbeat', 'sessionID': sessionID}))
ack = await ws.recv()
# print(ack)
try:
asyncio.get_event_loop().run_until_complete(consumer_contract())
except KeyboardInterrupt:
asyncio.get_event_loop().stop()