~cytrogen/evi-run

ref: eee0a658137484bba7b1469e6391e46aca0a45fb evi-run/bot/utils/get_ton_course.py -rw-r--r-- 740 bytes
eee0a658 — Bendy chore: normalize line endings 6 months ago
                                                                                
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
from aiohttp import ClientSession

from redis.asyncio.client import Redis

url = "https://api.coingecko.com/api/v3/simple/price"


async def get_ton_course(redis: Redis):
    ton_price = await redis.get("ton_price")
    if ton_price:
        return ton_price

    params = {
        "ids": "the-open-network",
        "vs_currencies": "usd"
    }
    async with ClientSession() as session:
        async with session.get(url, ssl=False, params=params) as response:
            try:
                data = await response.json()
                ton_price = data["the-open-network"]["usd"]
                await redis.set("ton_price", ton_price, ex=5)
                return ton_price
            except Exception as e:
                return