#!/usr/bin/env python3
"""
独立图片生成脚本 - 供定时任务调用
用法: python3 generate_images.py <article_title> [platform]
示例: python3 generate_images.py \"上海阳台种菜一年\" zhihu
"""

import sys
from pathlib import Path

PROJECT_ROOT = Path(__file__).parent.parent
sys.path.insert(0, str(PROJECT_ROOT))

from scripts.image_generator import ImageGenerator

def main():
    if len(sys.argv) < 2:
        print("用法: python3 generate_images.py <article_title> [platform=zhihu]")
        sys.exit(1)
    
    title = sys.argv[1]
    platform = sys.argv[2] if len(sys.argv) > 2 else "zhihu"
    
    generator = ImageGenerator()
    
    print(f"开始生成图片...")
    print(f"文章标题: {title}")
    print(f"目标平台: {platform}")
    print(f"输出目录: {generator.output_dir}")
    
    try:
        files = generator.generate_all_placeholders(title, platform)
        
        print(f"\n✅ 成功生成 {len(files)} 张图片:")
        for name, path in files.items():
            size_kb = path.stat().st_size // 1024
            print(f"  - {name}: {path.name} ({size_kb}KB)")
        
        print(f"\n📁 图片保存在: {generator.output_dir}")
        return 0
    except Exception as e:
        print(f"\n❌ 图片生成失败: {e}")
        import traceback
        traceback.print_exc()
        return 1

if __name__ == "__main__":
    sys.exit(main())