# -*- coding: utf-8 -*-
"""临时发布脚本"""
import asyncio
import json
import sys
import os

# 设置控制台编码
if sys.platform == "win32":
    os.system("chcp 65001 >nul 2>&1")

sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))

from publisher import XiaohongshuPublisher

async def main():
    # 读取内容
    with open(os.path.join(os.path.dirname(__file__), 'publish_content.json'), 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    publisher = XiaohongshuPublisher(headless=False)
    await publisher.start()
    
    try:
        # 检查登录
        if not await publisher.check_login():
            print("Need login, waiting...")
            if not await publisher.wait_for_login():
                print("Login timeout")
                return
        
        # 发布
        await publisher.publish_note(
            title=data['title'],
            content=data['content'],
            tags=data['tags']
        )
        
        # 等待用户确认
        print("\nBrowser will stay open. Press Enter to close...")
        await asyncio.sleep(300)  # 保持5分钟
        
    finally:
        await publisher.close()

if __name__ == "__main__":
    asyncio.run(main())
