#!/usr/bin/python3.12
"""豆包重生成 social_hotspots_1.jpg（heat=94 公安机关打击网络谣言）"""
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 = "公安机关公布14起涉企网络谣言案例：编造智驾事故，AI生成假车祸视频"
summary = "公安机关公布14起涉企网络谣言案例，网民编造智驾事故、AI生成假车祸视频诋毁车企被处罚。"

# 中性描述，避开敏感触发词
prompt = "配图：公安机关开展净网行动打击网络谣言执法现场，警方网络安全管理与数字执法场景，新闻配图风格，写实，横向构图，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_1.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}")
