-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGetTickerData.py
65 lines (57 loc) · 2.41 KB
/
GetTickerData.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
import asyncio
import logging
import gzip
from io import BytesIO
from datetime import datetime
from aiowebsocket.converses import AioWebSocket
import json
import sqlite3
import ssl
ssl._create_default_https_context = ssl._create_unverified_context
conn = sqlite3.connect('btc_tick.db')
cursor = conn.cursor()
cursor.execute('create table if not exists tick (tradeId varchar(20) primary key, price float, amount float, ts bigint, direction varchar(20))')
cursor.close()
conn.commit()
async def startup(uri) :
async with AioWebSocket(remote) as aws :
converse = aws.manipulator
reqMsg = json.dumps({'sub':'market.btcusdt.trade.detail', 'id':1})
await converse.send(reqMsg)
while True:
rec = await converse.receive()
buff = BytesIO(rec)
f = gzip.GzipFile(fileobj=buff)
res = f.read().decode('utf-8')
rj = json.loads(res)
if 'ping' in rj :
backmsg = json.dumps({'pong':rj['ping']})
await converse.send(backmsg)
print(res, backmsg)
if 'tick' in rj :
cursor = conn.cursor()
for tick in rj['tick']['data'] :
try :
cursor.execute('insert into tick (tradeId, price, amount, ts, direction) values (?,?,?,?,?)', (tick['tradeId'], tick['price'], tick['amount'], tick['ts'], tick['direction']))
except sqlite3.IntegrityError :
print(sqlite3.IntegrityError)
cursor.close()
conn.commit()
print(rj['tick']['data'])
else :
print(rj)
# async with AioWebSocket(uri) as aws :
# converse = aws.manipulator
# message = '{"action":"subscribe","args":["QuoteBin5m:14"]}'
# await converse.send(message)
# print('{time}-Client send: {message}'.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), message=message))
# while True:
# mes = await converse.receive()
# print('{time}-Client receive: {rec}'.format(time=datetime.now().strftime('%Y-%m-%d %H:%M:%S'), rec=mes))
if __name__ == '__main__' :
logging.basicConfig(level=logging.DEBUG)
remote = 'wss://api.huobi.pro/ws'
try:
asyncio.get_event_loop().run_until_complete(startup(remote))
except KeyboardInterrupt as exc :
logging.info('Quit.')