added extend symbol change for ws

This commit is contained in:
2026-04-30 04:32:49 +00:00
parent dc3409ac40
commit 1ac0909c21
20 changed files with 28960 additions and 1221 deletions

View File

@@ -3,16 +3,15 @@ import json
import logging
import socket
import traceback
from datetime import datetime, timezone
from datetime import datetime
from typing import AsyncContextManager
import math
import numpy as np
import pandas as pd
# import numpy as np
# import pandas as pd
import requests.packages.urllib3.util.connection as urllib3_cn # type: ignore
from sqlalchemy import text
# from sqlalchemy import text
import websockets
from sqlalchemy.ext.asyncio import create_async_engine
# from sqlalchemy.ext.asyncio import create_async_engine
import valkey
import os
from dotenv import load_dotenv
@@ -25,31 +24,30 @@ urllib3_cn.allowed_gai_family = allowed_gai_family
### Database ###
USE_DB: bool = False
USE_VK: bool = True
VK_FUND_RATE_ALL = 'fund_rate_aster_all'
VK_CHANNEL = 'fund_rate_aster_all'
CON: AsyncContextManager | None = None
VAL_KEY = None
CON: AsyncContextManager
VAL_KEY: valkey.Valkey
### Logging ###
load_dotenv()
LOG_FILEPATH: str = os.getenv("LOGS_PATH") + '/Fund_Rate_Aster_FR_ALL.log'
LOG_FILEPATH: str = f'{os.getenv(key="LOGS_PATH")}/Fund_Rate_Aster_FR_ALL.log'
### Globals ###
WSS_URL = "wss://fstream.asterdex.com/ws/!markPrice@arr"
WSS_URL: str = "wss://fstream.asterdex.com/ws/!markPrice@arr"
### Websocket ###
async def ws_stream():
async for websocket in websockets.connect(WSS_URL):
logging.info(f"Connected to {WSS_URL}")
logging.info(msg=f"Connected to {WSS_URL}")
try:
async for message in websocket:
if isinstance(message, str):
try:
data = json.loads(message)
data: dict = json.loads(message)
if data:
VAL_KEY.set(VK_FUND_RATE_ALL, json.dumps(data))
VAL_KEY.set(name=VK_CHANNEL, value=json.dumps(obj=data))
# print(f'VK_SAVED: {len(data)}')
continue
else:
@@ -61,39 +59,35 @@ async def ws_stream():
else:
raise ValueError(f'Type: {type(data)} not expected: {message}')
except websockets.ConnectionClosed as e:
logging.error(f'Connection closed: {e}')
logging.error(traceback.format_exc())
continue
logging.error(msg=f'Connection closed: {e}')
logging.error(msg=traceback.format_exc())
utils.send_tg_alert(msg=f'WS: {VK_CHANNEL} - Failure: {e}')
except Exception as e:
logging.error(f'Connection closed: {e}')
logging.error(traceback.format_exc())
logging.error(msg=f'Connection closed: {e}')
logging.error(msg=traceback.format_exc())
utils.send_tg_alert(msg=f'WS: {VK_CHANNEL} - Failure: {e}')
### Startup ###
async def main():
global VAL_KEY
global CON
if USE_VK:
VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0)
else:
VAL_KEY = None
logging.warning("VALKEY NOT BEING USED, NO DATA WILL BE PUBLISHED")
VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0)
if USE_DB:
engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
async with engine.connect() as CON:
# await create_rtds_btcusd_table(CON=CON)
await ws_stream()
raise NotImplementedError('DB not implemented for this ws.')
# engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
# async with engine.connect() as CON:
# await ws_stream()
else:
CON = None
logging.warning("DATABASE NOT BEING USED, NO DATA WILL BE RECORDED")
await ws_stream()
if __name__ == '__main__':
START_TIME = round(datetime.now().timestamp()*1000)
START_TIME: int = round(number=datetime.now().timestamp()*1000)
logging.info(f'Log FilePath: {LOG_FILEPATH}')
logging.info(msg=f'Log FilePath: {LOG_FILEPATH}')
logging.basicConfig(
force=True,
@@ -102,9 +96,9 @@ if __name__ == '__main__':
format='%(asctime)s - %(levelname)s - %(message)s',
filemode='w'
)
logging.info(f"STARTED: {START_TIME}")
logging.info(msg=f"STARTED: {START_TIME}")
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("Stream stopped")
logging.info(msg="Stream stopped")