import time

from curl_cffi import requests

from models.weex import Weex1Hour

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',
    'appversion': '2.0.0',
    'bundleid': '',
    'cache-control': 'no-cache',
    'dnt': '1',
    'language': 'zh_CN',
    'origin': 'https://www.weeaxs.site',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.weeaxs.site/',
    'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Microsoft Edge";v="140"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'cross-site',
    'sidecar': '01bfdfef90d2d6213c86b09f3a00d696d366752996a0b6692a33969a67fcd243df',
    'terminalcode': '89adf61538715df59eb4f6414981484e',
    'terminaltype': '1',
    'traceid': 'mgoh8eoehxp15lxnbv',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0',
    'vs': 'G5h7sX78yNSykC9xtTmA7O2lSKqAk6Sp',
    'x-sig': '74ec6a65f3886f24a8b062e3b41edb1a',
    'x-timestamp': '1760320261454',
}

klineId = None
klineTime = None
contractId = None

while True:

    for i in range(10):
        print(i)
        params = {
            'languageType': '1',
            'sign': 'SIGN',
            'timeZone': 'string',
            'contractId': '10000002',
            'productCode': 'ethusdt',
            'priceType': 'LAST_PRICE',
            'klineType': 'HOUR_1',
            'limit': '570',
        }

        if klineId:
            params['nextKey.klineId'] = klineId
            params['nextKey.contractId'] = contractId

        if klineTime:
            params['nextKey.klineTime'] = klineTime

        response = requests.get('https://http-gateway1.janapw.com/api/v1/public/quote/v1/getKlineV2', params=params,
                                headers=headers)

        klineId = response.json()["data"]["nextKey"]["klineId"]
        klineTime = response.json()["data"]["nextKey"]["klineTime"]
        contractId = response.json()["data"]["nextKey"]["contractId"]

        for data in response.json()["data"]["dataList"]:
            # print(data)

            Weex1Hour.get_or_create(
                id=int(data[4]),
                defaults={
                    'open': float(data[3]),
                    'high': float(data[1]),
                    'low': float(data[2]),
                    'close': float(data[0]),
                }
            )

        # time.sleep(1)

    time.sleep(3600)
