#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import csv
import os
import shutil

# CSV文件路径
csv_file_path = '/Users/v_sat/Documents/trae_projects/bingqi/data/map_data.csv'
# 目标目录
target_dir = '/Users/v_sat/Documents/trae_projects/bingqi/pic/map'

# 确保目标目录存在
os.makedirs(target_dir, exist_ok=True)

# 读取CSV文件
with open(csv_file_path, 'r', encoding='utf-8-sig') as csvfile:
    reader = csv.DictReader(csvfile)
    
    for row in reader:
        province = row['省']
        img_url = row['地图图片地址']
        
        # 构建源文件路径
        if img_url.startswith('./'):
            # 去掉 ./ 前缀，构建完整路径
            relative_path = img_url[2:]
            source_file = os.path.join('/Users/v_sat/Desktop', relative_path)
            
            # 构建目标文件路径
            target_file = os.path.join(target_dir, f'{province}.jpg')
            
            # 检查源文件是否存在
            if os.path.exists(source_file):
                # 复制文件
                shutil.copy2(source_file, target_file)
                print(f'已复制 {province} 图片')
            else:
                print(f'找不到 {province} 的源图片: {source_file}')
        else:
            print(f'无法处理 {province} 的图片URL: {img_url}')

print('图片复制完成')
