Захотел добавить асинхронности в код, чтобы быстрее получать ответы от API.
import aiohttp
import asyncio
async def bybit(a, session):
try:
async with session.get(f"https://api.bybit.com/derivatives/v3/public/tickers?category=linear&symbol={a.upper()}USDT") as resp:
by_req = await resp.json()
return f"\n<b>ByBit</b>:\n<i>Bid</i>: <code>{by_req['result']['list'][0]['bidPrice']}</code> <i>Ask</i>: <code>{by_req['result']['list'][0]['askPrice']}</code>"
except:
return False
async def binance(a, session):
try:
async with session.get("https://api.binance.com/api/v3/depth", params = {'symbol' : f'{a.upper()}USDT', 'limit' : 1}) as resp:
r = await resp.json()
return f"\n<b>Binance</b>:\n<i>Bid</i>: <code>{float(r['bids'][0][0])}</code> <i>Ask</i>: <code>{float(r['asks'][0][0])}</code>"
except:
return False
async def huobi(a, session):
try:
async with session.get(f"https://api.huobi.pro/market/detail/merged?symbol={a.lower()}usdt") as resp:
p = await resp.json()
return f"\n<b>Huobi</b>:\n<i>Bid</i>: <code>{p['tick']['bid'][0]}</code> <i>Ask</i>: <code>{p['tick']['ask'][0]}</code>"
except:
return False
async def mexc(a, session):
try:
async with session.get("https://api.mexc.com/api/v3/depth", params = {'symbol' : f'{a.upper()}USDT', 'limit' : 1}) as resp:
k = await resp.json()
return f"\n<b>Mexc</b>:\n<i>Bid</i>: <code>{k['bids'][0][0]}</code> <i>Ask</i>: <code>{k['asks'][0][0]}</code>"
except:
return False
async def kucoin(a, session):
try:
async with session.get(f"https://api.kucoin.com/api/v1/market/orderbook/level1?symbol={a.upper()}-USDT") as resp:
kk = await resp.json()
return f"\n<b>Kukoin</b>:\n<i>Bid</i>: <code>{kk['data']['bestBid']}</code> <i>Ask</i>: <code>{kk['data']['bestAsk']}</code>"
except:
return False
async def gate(a, session):
try:
async with session.get(f"https://data.gateapi.io/api2/1/ticker/{a.lower()}_usdt") as resp:
g = await resp.json()
return f"\n<b>Gate</b>:\n<i>Bid</i>: <code>{g['highestBid']}</code> <i>Ask</i>: <code>{g['lowestAsk']}</code>"
except:
return False
async def bibox(a, session):
try:
async with session.get(f"https://api.bibox.com/api/v4/marketdata/order_book?symbol={a.upper()}_USDT") as resp:
bibs = await resp.text()
bib = json.loads(bibs)
return f"\n<b>Bibox</b>:\n<i>Bid</i>: <code>{bib['b'][0][0]}</code> <i>Ask</i>: <code>{bib['a'][0][0]}</code>"
except:
return False
async def hotbit(a, session):
try:
async with session.get(f"https://api.hotbit.io/api/v1/order.depth?market={a.upper()}/USDT&limit=10&interval=1e-8") as resp:
hbs = await resp.text()
hb = json.loads(hbs)
return f"\n<b>Hotbit</b>:\n<i>Bid</i>: <code>{hb['result']['bids'][0][0]}</code> <i>Ask</i>: <code>{hb['result']['asks'][0][0]}</code>"
except:
return False
async def lbank(a, session):
try:
async with session.get(f"https://api.lbkex.com/v2/depth.do", params = {'symbol' : f'{a.lower()}_usdt', 'size' : 10}) as resp:
lb = await resp.json()
return f"\n<b>Lbank</b>:\n<i>Bid</i>: <code>{lb['data']['bids'][0][0]}</code> <i>Ask</i>: <code>{lb['data']['asks'][0][0]}</code>"
except:
return False
@dp.message_handler(lambda message: "!price" in message.text)
async def price(message: types.Message):
if not message.chat.type == 'private':
if not message.chat.type == 'private':
async with aiohttp.ClientSession() as session:
coins = message.text[7:].lower()
result = []
funcs = (binance, bybit , huobi, mexc, kucoin, gate, bibox, hotbit, lbank)
print('work')
for func in funcs:
if call := asyncio.ensure_future(func(coins, session)):
result.append(call)
await asyncio.gather(*result)
print(result)
Это если что часть кода, из тг бота. Библиотека aiogram
После исполнения кода, вместо ожидаемого ответа, в списке выводит это:
Причем, если выводить принты в функциях, то там все четко. Но почему то он не возвращает эти данные...