forked from No1CharlesWu/okcoin_app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
okcoin_websocket.py
89 lines (63 loc) · 2.67 KB
/
okcoin_websocket.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import websocket
import zlib
import json
import datetime
from data_filter import global_data_filter
def websocket_ticker(symbol='btc', event='addChannel', binary=True):
channel = 'ok_sub_spotcny_' + symbol + '_ticker'
return {'event': event, 'channel': channel, 'binary': str(binary)}
def websocket_incremental_depth(symbol='btc', event='addChannel', binary=True):
channel = 'ok_sub_spot_' + symbol + '_depth'
return {'event': event, 'channel': channel, 'binary': str(binary)}
def websocket_depth(symbol='btc', size=60, event='addChannel', binary=True):
channel = 'ok_sub_spotcny_' + symbol + '_depth_' + str(size)
return {'event': event, 'channel': channel, 'binary': str(binary)}
def websocket_trades(symbol='btc', event='addChannel', binary=True):
channel = 'ok_sub_spotcny_' + symbol + '_trades'
return {'event': event, 'channel': channel, 'binary': str(binary)}
def websocket_kline(symbol='btc', time='1min', event='addChannel', binary=True):
channel = 'ok_sub_spotcny_' + symbol + '_kline_' + time
return {'event': event, 'channel': channel, 'binary': str(binary)}
def on_open(self):
l = list()
l.append(websocket_ticker())
l.append(websocket_depth())
l.append(websocket_trades())
l.append(websocket_kline())
self.send(str(l))
def inflate(data):
decompress = zlib.decompressobj(
-zlib.MAX_WBITS # see above
)
inflated = decompress.decompress(data)
inflated += decompress.flush()
return inflated
def on_message(self, evt):
if type(evt) == bytes:
evt = str(inflate(evt), 'utf-8') # data decompress
data = json.loads(evt)[0]
# print(datetime.now(), data)
global_data_filter.websocket_add_data(data)
print('--------------------------------------------')
# for i, d in enumerate(global_data_filter.get_kline_list()):
# print(i, datetime.datetime.fromtimestamp(d['timestamp']/1000), d)
# for i, d in enumerate(global_data_filter.get_trades_list()):
# print(i, d)
def on_error(self, error):
print(error)
# tws = websocket.WebSocketApp(host, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close)
# tws.run_forever(ping_interval=20)
def on_close(self):
print('DISCONNECT')
def websocket_stop():
ws.close()
def websocket_start():
ws.run_forever(ping_interval=20)
url = "wss://real.okcoin.cn:10440/websocket/okcoinapi"
api_key = 'c3b622bc-8255-40f2-9585-138928ae376d'
secret_key = '7C1DDC1745C93B87BE1643A689938459'
websocket.enableTrace(True)
host = url
ws = websocket.WebSocketApp(host, on_open=on_open, on_message=on_message, on_error=on_error, on_close=on_close)
if __name__ == "__main__":
websocket_start()