#!/usr/bin/python3.12
"""豆包重生成 social_hotspots_2.jpg（heat=93 人工智能安全）"""
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/0819/"

title = "人工智能安全：下一阶段人工智能产业竞争的新赛道"
summary = "专家称AI进入物理世界后安全成产业底线，美欧已布局风险框架与立法，安全测试等新兴产业加速形成。"

# 清理标题末尾热度值
ct = re.sub(r'\s*\d[\d,.]*\s*万(?:热度)?\s*$', '', title).strip()
prompt = f"配图：{ct}，{summary[:60]}，新闻配图风格，写实，横向构图，16:9，高清"
print("prompt:", prompt)

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")

fname = "social_hotspots_2.jpg"
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())
print(f"✅ 已生成: {local_path} ({os.path.getsize(local_path)//1024}KB)")

# 验证
from PIL import Image
im = Image.open(local_path)
print(f"尺寸: {im.size} {im.format}")
