import requests
import json
import time
import os

# 配置
BASE_URL = "https://mainnet.zklighter.elliot.ai/api/v1/account"
HEADERS = {"accept": "application/json"}
OUTPUT_FILE = "accounts.json"
SAVE_INTERVAL = 100  # 每100个保存一次
REQUEST_INTERVAL = 1  # 每1秒请求一个

def fetch_account(index):
    """获取单个账户信息"""
    try:
        params = {"by": "index", "value": index}
        response = requests.get(BASE_URL, headers=HEADERS, params=params)
        response.raise_for_status()
        return response.json()
    except Exception as e:
        print(f"请求索引 {index} 失败: {e}")
        return None

def load_existing_data():
    """加载已有的数据"""
    if os.path.exists(OUTPUT_FILE):
        try:
            with open(OUTPUT_FILE, 'r', encoding='utf-8') as f:
                return json.load(f)
        except Exception as e:
            print(f"加载已有数据失败: {e}")
            return []
    return []

def save_to_file(data):
    """保存数据到JSON文件"""
    try:
        with open(OUTPUT_FILE, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        print(f"✓ 已保存 {len(data)} 条记录到 {OUTPUT_FILE}")
    except Exception as e:
        print(f"保存文件失败: {e}")

def get_already_fetched_indices(data):
    """获取已经爬取过的索引"""
    return set(item['index'] for item in data)

def main():
    # 获取用户输入
    try:
        start_index = int(input("请输入起始 index (默认 0): ") or "0")
        end_index = int(input("请输入终止 index (默认 310000): ") or "310000")
    except ValueError:
        print("输入格式错误，使用默认值 0-310000")
        start_index = 0
        end_index = 310000
    
    if start_index > end_index:
        print("错误：起始index不能大于终止index")
        return
    
    total_count = end_index - start_index + 1
    print(f"\n开始遍历账户...")
    print(f"范围: {start_index} - {end_index}")
    print(f"总共需要请求: {total_count} 个账户\n")
    
    # 加载已有数据
    all_data = load_existing_data()
    already_fetched = get_already_fetched_indices(all_data)
    
    if already_fetched:
        print(f"已存在 {len(already_fetched)} 条记录")
        print(f"最小索引: {min(already_fetched)}, 最大索引: {max(already_fetched)}\n")
    
    # 本次新增计数器
    new_count = 0
    
    try:
        for index in range(start_index, end_index + 1):
            # 如果已经爬取过，跳过
            if index in already_fetched:
                print(f"跳过 index={index} (已存在)")
                continue
            
            # 发送请求
            response = fetch_account(index)
            
            # 构建数据对象
            data_item = {
                "index": index,
                "response": response
            }
            all_data.append(data_item)
            new_count += 1
            
            # 当前进度
            current_index = index
            processed = current_index - start_index + 1
            
            print(f"已处理: index={current_index}, 进度: {processed}/{total_count}, 本次新增: {new_count}, 总计: {len(all_data)}")
            
            # 每SAVE_INTERVAL个保存一次
            if new_count % SAVE_INTERVAL == 0:
                save_to_file(all_data)
                print(f"=" * 60)
            
            # 等待指定时间
            time.sleep(REQUEST_INTERVAL)
    
    except KeyboardInterrupt:
        print("\n\n⚠️  检测到中断 (Ctrl+C)")
        print(f"当前进度: index={index}")
        print(f"本次新增: {new_count} 条")
        print(f"总计: {len(all_data)} 条记录")
        # 最后保存一次
        save_to_file(all_data)
        print(f"\n下次继续运行请输入:")
        print(f"  起始 index: {index + 1}")
        print(f"  终止 index: {end_index}")
        return
    
    # 最后保存一次
    save_to_file(all_data)
    print(f"\n✅ 完成！")
    print(f"本次新增: {new_count} 条")
    print(f"总计: {len(all_data)} 个账户")

if __name__ == "__main__":
    main()