import requests
from DrissionPage import ChromiumPage, ChromiumOptions

from bit_tools import openBrowser
from models.weex import Weex1, Weex1Hour, Weex15, Weex30

if __name__ == '__main__':

    # bit_port = openBrowser(id="8dcb4f744cf64ab190e465e153088515")

    response = requests.post(
                f"http://127.0.0.1:50326/api/browser/start",
                json={"envId": 146473},
                headers={
            "Authorization": f"Bearer asp_174003986c9b0799677c5b2c1adb76e402735d753bc91a91",
            "Content-Type": "application/json"
        }
            )

    co = ChromiumOptions()
    co.set_local_port(port=response.json()["data"]["port"])


    page = ChromiumPage(addr_or_opts=co)

    page.set.window.max()

    page.listen.start("public/quote/v1/getKlineV2")

    page.get(url="https://www.weeaxs.site/zh-CN/futures/ETH-USDT")

    for _ in range(10000):
        res = page.listen.wait()  # 等待并获取一个数据包

        print(_)
        print(res.response.url)

        for data in res.response.body['data']["dataList"]:
            Weex30.get_or_create(
                id=int(data[4]),
                defaults={
                    'open': float(data[3]),
                    'high': float(data[1]),
                    'low': float(data[2]),
                    'close': float(data[0]),
                }
            )
