#!/usr/bin/python3.12
"""从Dify品牌热点工作流获取数据并写入日报（循环调用）"""
import json, os, sys, re, urllib.request, http.client
from datetime import datetime

WORKFLOW_URL = "http://43.164.190.156/v1/workflows/run"
WORKFLOW_TOKEN = "app-bFNyXVNtSEOqBnp20UcnYFS8"

URL_LIST = [
    "https://www.adquan.com",
    "https://www.digitaling.com",
    "https://socialbeta.com",
    "https://www.brandstar.com.cn/news/",
    "https://www.meihua.info/!2"
]

def call_workflow(url):
    """调用Dify workflow（streaming），返回完整SSE输出"""
    payload = json.dumps({
        "inputs": {"url": url},
        "response_mode": "streaming",
        "user": "root"
    }).encode('utf-8')

    req = urllib.request.Request(
        WORKFLOW_URL, data=payload,
        headers={'Authorization': f'Bearer {WORKFLOW_TOKEN}', 'Content-Type': 'application/json'},
        method='POST'
    )

    try:
        resp = urllib.request.urlopen(req, timeout=600)
    except Exception as e:
        raise ConnectionError(f"请求失败: {e}")

    buffer = b""
    import signal
    def timeout_handler(signum, frame):
        raise TimeoutError("Stream read timeout")
    signal.signal(signal.SIGALRM, timeout_handler)
    signal.alarm(600)  # 10 min max per URL read (2026-08-13 user request: increase wait time)
    while True:
        chunk = resp.read(65536)
        if not chunk:
            break
        buffer += chunk

    full_text = buffer.decode('utf-8', errors='replace')

    # 找 workflow_finished 事件中的 outputs
    for line in full_text.split('\n'):
        if line.startswith('data: '):
            try:
                evt = json.loads(line[6:])
                if evt.get('event') == 'workflow_finished':
                    return evt.get('data', {}).get('outputs', {})
            except:
                continue
    return None

def extract_items(outputs):
    """从workflow输出中提取brand_hotspots数组"""
    if not outputs:
        return None
    val = outputs.get('brand_hotspots')
    if val is None:
        return None
    if isinstance(val, str):
        try:
            val = json.loads(val)
        except:
            return None
    if isinstance(val, list):
        return [i for i in val if isinstance(i, dict)]
    return None

def filter_items(items):
    return [i for i in items if isinstance(i, dict) and i.get('brand', '') and i.get('brand', '') != '未提供']

def merge_to_origin(mmd_data, new_items):
    """追加到已有列表，按title去重"""
    for item in mmd_data:
        if item.get('name') == 'brand_hotspots':
            existing = item.get('list', [])
            seen_titles = {e.get('title', '') for e in existing}
            for ni in new_items:
                t = ni.get('title', '')
                if t and t not in seen_titles:
                    existing.append(ni)
                    seen_titles.add(t)
            item['list'] = existing
            return mmd_data
    mmd_data.append({"name": "brand_hotspots", "opinion": "", "list": new_items})
    return mmd_data

def main():
    mmdd = datetime.now().strftime("%m%d")
    origin_path = f"/data/news/json/origin_data/{mmdd}data.json"

    all_items = []
    seen_keys = set()

    for idx, url in enumerate(URL_LIST):
        print(f"[{datetime.now().strftime('%H:%M:%S')}] [{idx+1}/{len(URL_LIST)}] {url[:50]}...")

        outputs = call_workflow(url)
        if not outputs:
            print(f"  ⚠️ 无返回")
            continue

        items = extract_items(outputs)
        if not items:
            print(f"  ⚠️ 未提取到数据, keys={list(outputs.keys())}")
            continue

        filtered = filter_items(items)
        added = 0
        for item in filtered:
            key = f"{item.get('brand','')}|{item.get('title','')}"
            if key and key not in seen_keys:
                seen_keys.add(key)
                if 'yesorno' not in item:
                    item['yesorno'] = ''
                all_items.append(item)
                added += 1

        print(f"  新增 {added} 条（累计 {len(all_items)} 条）")
        # 每轮写入，防止后续卡死丢数据
        if all_items:
            mmd_data = []
            if os.path.exists(origin_path):
                with open(origin_path, 'r', encoding='utf-8') as f:
                    mmd_data = json.load(f)
            merged = merge_to_origin(mmd_data, all_items)
            with open(origin_path, 'w', encoding='utf-8') as f:
                json.dump(merged, f, ensure_ascii=False, indent=2)

    if not all_items:
        print("❌ 未获取到数据")
        sys.exit(1)

    # 日期过滤：只保留今天和昨天的数据
    from datetime import timedelta
    today = datetime.now().strftime('%Y-%m-%d')
    yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d')
    allowed_dates = {today, yesterday}
    before = len(all_items)
    removed = [i for i in all_items if i.get('publish_time', '') not in allowed_dates]
    all_items = [i for i in all_items if i.get('publish_time', '') in allowed_dates]
    after = len(all_items)
    if before - after > 0:
        print(f"  日期过滤: 移除 {before - after} 条非24h数据")
        for it in removed[:3]:
            print(f"    - {it.get('publish_time','')} {it.get('title','')[:30]}")

    mmd_data = []
    if os.path.exists(origin_path):
        with open(origin_path, 'r', encoding='utf-8') as f:
            mmd_data = json.load(f)

    merged = merge_to_origin(mmd_data, all_items)
    os.makedirs(os.path.dirname(origin_path), exist_ok=True)
    with open(origin_path, 'w', encoding='utf-8') as f:
        json.dump(merged, f, ensure_ascii=False, indent=2)

    print(f"\n✅ 共 {len(all_items)} 条品牌热点，已写入 {origin_path}")

if __name__ == '__main__':
    main()
