#!/usr/bin/env python3
"""基于正文重新生成社会热点摘要（替换刚生成的标题改写版）"""
import json, os, urllib.request, re

import yaml

cfg = yaml.safe_load(open(os.path.expanduser('~/.hermes/config.yaml')))
API_KEY = cfg['providers']['deepseek']['api_key']
API_URL = "https://api.deepseek.com/v1/chat/completions"

FINAL = '/data/news/json/0809data.json'
YES = '/data/news/json/yes_data/0808data.json'

body = (
    "人民锐评：AI虚拟人'方桃子'一句'戴了一天都很舒服'，让AI代言的行业隐患暴露。"
    "无实体、无真实感知的数字虚拟人模拟消费者使用体验进行产品推荐，是技术演绎的虚假宣传。"
    "AI广告翻车事件屡见不鲜：苏泊尔AI广告因低俗内容下架、babysheep安睡裤AI广告渲染月经羞耻引发争议、"
    "美妆品牌借助AI虚假美颜误导消费者。不少品牌重流量轻合规，将广告全流程外包给MCN机构与AI工具，"
    "一旦违规便以'内容由AI生成'为借口推卸责任，将AI当作规避处罚、转嫁风险的'免责盾牌'。"
    "根据广告法，广告代言人需具备法律主体责任资格，AI虚拟人只是算法生成的数字产物，不具备法律主体身份，"
    "不属于法定代言范畴。规范AI广告生态需破除'AI免责'误区、明确追责主体、坚守宣传真实性底线、落实强制标识。"
)

prompt = (
    "你是汽车行业营销日报的摘要撰写者。根据以下新闻正文提炼一条摘要。\n\n"
    f"正文：{body}\n\n"
    "要求：\n"
    "1. 摘要必须提炼正文核心事实，包含具体信息（谁/发生了什么/影响），不能只是标题扩写\n"
    "2. 长度不超过40字，以句号结尾\n"
    "3. 直接输出摘要文本，不要加引号、前缀或'媒体：'等字样"
)

payload = json.dumps({
    "model": "deepseek-chat",
    "messages": [
        {"role": "system", "content": "你是严谨的新闻摘要撰写助手，输出简洁准确的中文摘要。"},
        {"role": "user", "content": prompt}
    ],
    "temperature": 0.3,
    "max_tokens": 200
}).encode()
req = urllib.request.Request(API_URL, data=payload, headers={
    'Content-Type': 'application/json',
    'Authorization': f'Bearer {API_KEY}'
})
with urllib.request.urlopen(req, timeout=180) as r:
    new_summary = json.loads(r.read())['choices'][0]['message']['content'].strip()

print("新摘要:", new_summary)

TARGET_TITLE = "媒体：'内容由AI生成'不是免责盾牌"

with open(FINAL) as f:
    final = json.load(f)
for item in final.get('social_hotspots', []):
    if item.get('title') == TARGET_TITLE:
        print(f"[最终文件] 旧摘要: {item['summary']}")
        item['summary'] = new_summary
with open(FINAL, 'w', encoding='utf-8') as f:
    json.dump(final, f, ensure_ascii=False, indent=2)

with open(YES) as f:
    yes = json.load(f)
for sec in yes:
    if sec.get('name') == 'social_hotspots':
        for item in sec.get('list', []):
            if item.get('title') == TARGET_TITLE:
                print(f"[yes_data] 旧摘要: {item['summary']}")
                item['summary'] = new_summary
with open(YES, 'w', encoding='utf-8') as f:
    json.dump(yes, f, ensure_ascii=False, indent=2)

print("\n完成。")
