#!/usr/bin/env python3.12
# -*- coding: utf-8 -*-
"""豆包重生成 social[0][1] 配图（用户要求）"""
import json, urllib.request, os, re

API_KEY = "ark-563a4210-d804-47fa-a37a-40962192119b-ce6db"
API_URL = "https://ark.cn-beijing.volces.com/api/v3/images/generations"
MODEL = "doubao-seedream-4-0-250828"
IMG_DIR = '/data/news/images/0822'

targets = [
    (0, '社会热点：国家级自驾游路线，中国地图上的"三环四横五纵"精品自驾公路，壮美山河公路风景，汽车行驶在风景公路，新闻配图风格，写实，横向构图，16:9，高清'),
    (1, '中国财政部大楼，宏观经济政策发布会场景，财政政策支持消费与民生，新闻配图风格，写实，横向构图，16:9，高清'),
]

def gen_img(prompt, fname):
    payload = json.dumps({
        "model": MODEL, "prompt": prompt, "size": "1280x720", "n": 1,
        "response_format": "url"
    }).encode()
    req = urllib.request.Request(API_URL, data=payload,
        headers={'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json'})
    resp = urllib.request.urlopen(req, timeout=120)
    result = json.loads(resp.read().decode('utf-8'))
    img_url = result.get('data', [{}])[0].get('url', '')
    if not img_url:
        raise Exception('豆包未返回图片URL')
    local_path = os.path.join(IMG_DIR, fname)
    img_req = urllib.request.Request(img_url, headers={'User-Agent': 'Mozilla/5.0'})
    img_resp = urllib.request.urlopen(img_req, timeout=60)
    with open(local_path, 'wb') as f:
        f.write(img_resp.read())
    return local_path

for idx, prompt in targets:
    fname = f'social_hotspots_{idx}.jpg'
    try:
        local = gen_img(prompt, fname)
        from PIL import Image
        im = Image.open(local)
        print(f'  ✅ social[{idx}] {fname} {im.size} {os.path.getsize(local)//1024}KB')
    except Exception as e:
        print(f'  ❌ social[{idx}] 失败: {e}')
print('✅ 完成')
