import os
import sys
from pathlib import Path
from dotenv import load_dotenv
from qiniu import Auth, put_file

# 加载环境变量
env_path = Path(__file__).resolve().parent.parent / 'python-api' / '.env'
load_dotenv(env_path)

access_key = os.getenv('QINIU_ACCESS_KEY')
secret_key = os.getenv('QINIU_SECRET_KEY')
bucket = os.getenv('QINIU_VIDEO_BUCKET', 'media-liche')
domain = os.getenv('QINIU_VIDEO_DOMAIN', 'media.liche.cn')

if not access_key or not secret_key:
    print("错误: 未找到七牛云凭证，请检查 .env 文件")
    sys.exit(1)

q = Auth(access_key, secret_key)
src_dir = Path('/Users/0fun/Downloads/新增素材6.2')

total = 0
success = 0
failed = 0

files = sorted([p for p in src_dir.rglob('*.mp4') if not p.name.startswith('._') and '.DS_Store' not in str(p)])

print(f"发现 {len(files)} 个 MP4 文件，开始上传...\n")

for mp4_path in files:
    total += 1
    key = f"meijiaka-zy/materials/{mp4_path.name}"

    try:
        token = q.upload_token(bucket, key, 3600)
        ret, info = put_file(token, key, str(mp4_path))
        if ret is not None:
            url = f"https://{domain}/{key}"
            print(f"✅ [{total}/{len(files)}] {mp4_path.name}")
            success += 1
        else:
            print(f"❌ [{total}/{len(files)}] {mp4_path.name} → 失败: {info}")
            failed += 1
    except Exception as e:
        print(f"❌ [{total}/{len(files)}] {mp4_path.name} → 异常: {e}")
        failed += 1

print(f"\n{'=' * 50}")
print(f"总计: {total}, 成功: {success}, 失败: {failed}")
