co2_response.py 525 Bytes
Newer Older
lcn's avatar
lcn committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import requests
import json
from pot_libs.aredis_util import aredis_utils


async def get_co2_price():
    url = "http://ets.cnemission.com/carbon/portalIndex/getRealTimeMarket?reportName=portalMarketRealTime"
    try:
        html = requests.get(url)
        data = json.loads(html.text)
        co2_price = float(data[0].get("CURRENT_PRICE")[:-1])
        await aredis_utils.RedisUtils().set("co2_price", co2_price)
    except:
        co2_price = await aredis_utils.RedisUtils().get("co2_price")
lcn's avatar
lcn committed
15
    return co2_price or 0