import requests
import pandas as pd

headers = {
    'accept': 'application/json, text/plain, */*',
    'accept-language': 'zh,zh-CN;q=0.9,zh-HK;q=0.8,en;q=0.7',
    'cache-control': 'no-cache',
    'origin': 'https://www.websea.com',
    'pragma': 'no-cache',
    'priority': 'u=1, i',
    'referer': 'https://www.websea.com/',
    'sec-ch-ua': '"Chromium";v="140", "Not=A?Brand";v="24", "Google Chrome";v="140"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    'sec-fetch-dest': 'empty',
    'sec-fetch-mode': 'cors',
    'sec-fetch-site': 'same-site',
    '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',
}

if __name__ == '__main__':
    datas = []
    import datetime

    # 定义开始日期和结束日期
    start_date = datetime.datetime(2025, 8, 28)
    end_date = datetime.datetime(2025, 9, 26)

    # 初始化当前日期为开始日期
    current_date = start_date

    # 循环遍历日期范围
    while current_date <= end_date:
        # 获取当天开始时刻（00:00:00）
        start_of_day = current_date.replace(hour=0, minute=0, second=0, microsecond=0)
        # 获取当天结束时刻（23:59:59）
        end_of_day = current_date.replace(hour=23, minute=59, second=59, microsecond=0)

        # 将开始时刻和结束时刻转换为时间戳
        start_timestamp = start_of_day.timestamp()
        end_timestamp = end_of_day.timestamp()

        print(f"日期: {current_date.strftime('%Y.%m.%d')}")
        print(f"  开始时刻时间戳: {start_timestamp}")
        print(f"  结束时刻时间戳: {end_timestamp}")

        params = {
            'symbol': 'ETH-USDT',
            'period': '1min',
            'start': int(start_timestamp),
            'end': int(end_timestamp),
        }

        response = requests.get('https://capi.websea.com/webApi/market/getKline', params=params, headers=headers)

        # 提取数据
        data = response.json()['result']['data']
        print(data)

        for i in data:
            datas.append(i)

        # 日期加一天
        current_date += datetime.timedelta(days=1)

    # 将数据转换为 DataFrame
    df = pd.DataFrame(datas)

    # 保存为 Excel 文件
    df.to_excel('kline_data.xlsx', index=False)

    print("数据已成功保存到 kline_data.xlsx 文件中。")
