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

"""
Instagram视频上传示例脚本
"""

import asyncio
from pathlib import Path
from datetime import datetime

# 添加项目根目录到路径
import sys
current_dir = Path(__file__).parent
project_root = current_dir.parent
sys.path.append(str(project_root))

from uploader.ins_uploder.main import InsVideo, ins_setup
from utils.log import ins_logger


async def upload_video_to_instagram():
    """上传视频到Instagram"""
    
    # 配置文件路径
    account_file = project_root / "cookies" / "ins_uploader" / "account.json"
    account_file.parent.mkdir(exist_ok=True, parents=True)
    
    # 视频文件路径
    video_path = project_root / "videos" / "demo.mp4"
    
    if not video_path.exists():
        ins_logger.error(f"视频文件不存在: {video_path}")
        ins_logger.info("请将视频文件放在 videos/demo.mp4 路径下")
        return False
    
    # 首先确保cookie有效
    ins_logger.info("检查Instagram账户登录状态...")
    if not await ins_setup(str(account_file), handle=True):
        ins_logger.error("Instagram账户登录失败")
        return False
    
    # 创建视频上传对象
    ins_video = InsVideo(
        title="我的Instagram视频",
        file_path=str(video_path),
        tags=["生活", "分享", "视频"],
        publish_date=datetime.now(),
        account_file=str(account_file)
    )
    
    # 开始上传
    ins_logger.info("开始上传视频到Instagram...")
    try:
        from playwright.async_api import async_playwright
        async with async_playwright() as playwright:
            await ins_video.upload(playwright)
        ins_logger.success("视频上传完成！")
        return True
    except Exception as e:
        ins_logger.error(f"上传失败: {e}")
        return False


if __name__ == '__main__':
    success = asyncio.run(upload_video_to_instagram())
    if success:
        print("✅ Instagram视频上传成功!")
    else:
        print("❌ Instagram视频上传失败!")




