import os
import requests

BASE_URL = "https://wargame.ia.ac.cn/static/map-images/model-type"
OUTPUT_DIR = "/Users/v_sat/Documents/trae_projects/bingqi/pic/model"

def download_icons():
    # 创建输出目录
    os.makedirs(OUTPUT_DIR, exist_ok=True)
    
    for type_num in [0, 1]:
        type_dir = os.path.join(OUTPUT_DIR, str(type_num))
        os.makedirs(type_dir, exist_ok=True)
        
        print(f"开始下载 type={type_num} 的图标...")
        
        for index in range(0, 1001):
            url = f"{BASE_URL}/png/{type_num}/{index}-0.png"
            output_path = os.path.join(type_dir, f"{index}-0.png")
            
            # 如果文件已存在，跳过
            if os.path.exists(output_path):
                print(f"文件已存在，跳过: {output_path}")
                continue
            
            try:
                response = requests.get(url, timeout=10)
                
                if response.status_code == 404:
                    print(f"遇到404，停止下载 type={type_num}")
                    break
                
                if response.status_code == 200:
                    with open(output_path, 'wb') as f:
                        f.write(response.content)
                    print(f"下载成功: {url} -> {output_path}")
                else:
                    print(f"下载失败，状态码: {response.status_code}, URL: {url}")
                    continue
                    
            except requests.exceptions.RequestException as e:
                print(f"下载异常: {e}, URL: {url}")
                continue

if __name__ == "__main__":
    download_icons()
    print("下载完成！")
