import _thread
from websocket import create_connection
from mysqlOpertion import connect_mysql
import gzip
import time
import json
def connect():
while(1):
try:
print('')
ws = create_connection("wss://www.huobi.br.com/-/s/pro/ws")
return ws
break
except Exception as err:
time.sleep(1)
def getsubconfig():
tradeStr=[
"""{"sub": "market.elfusdt.trade.detail","id": "id1"}""",
"""{"sub": "market.btcusdt.trade.detail","id": "id2"}""",
"""{"sub": "market.htusdt.trade.detail","id": "id3"}""",
"""{"sub": "market.swftcbtc.trade.detail","id": "id4"}""",
"""{"sub": "market.topcbtc.trade.detail","id": "id6"}""",
"""{"sub": "market.bchusdt.trade.detail","id": "id7"}""",
"""{"sub": "market.ethusdt.trade.detail","id": "id8"}""",
"""{"sub": "market.etcusdt.trade.detail","id": "id9"}""",
"""{"sub": "market.eosusdt.trade.detail","id": "id9"}""",
"""{"sub": "market.ltcusdt.trade.detail","id": "id9"}"""
]
return tradeStr
def sub(ws,tradeStr):
ws.send(tradeStr)
def getResponse(ws,tradeStr):
db = connect_mysql.connect()
while 1 :
try :
compressData=ws.recv()
except Exception :
run(tradeStr,1)
try :
result=gzip.decompress(compressData).decode('utf-8')
except:
continue
if result[:7] == '{"ping"':
ts=result[8:21]
pong='{"pong":'+ts+'}'
ws.send(pong)
else:
if result[:5] == '{"ch"':
result = json.loads(result)
n = 0
while n < len(result['tick']['data']) :
connect_mysql.commit('%s' % result['ch'].split('.')[1],result['tick']['data'][n]['price'],result['tick']['data'][n]['amount'],"'%s'" % result["tick"]["data"][n]["direction"], "'%s'" % result['ch'].split('.')[1],db);
n += 1
def subCoin(tradeStr):
ws=connect()
sub(ws,tradeStr)
getResponse(ws,tradeStr)
def run(tradeStr,test):
subCoin(tradeStr)
def main():
tradeStr=getsubconfig()
try :
tradeStr = getsubconfig()
i = 0
while i < len(tradeStr) :
_thread.start_new_thread( run, (tradeStr[i],i) )
i += 1
except Exception as err :
print(err)
while 1:
pass
if __name__ == '__main__':
main()