#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""罗氏诊断新闻三年回溯补采（sitemap + lastmod），并入 first 条目文件。"""
import json, re, os
import requests
from datetime import datetime, timedelta

W = '/Users/liuxinyuan/Desktop/Hermes输出-工作类'
F = os.path.join(W, '数据/market_intel/runs/20261013_items_first.jsonl')
UA = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36'
WIN = (datetime.now() - timedelta(days=1095)).strftime('%Y-%m-%d')
r = requests.get('https://diagnostics.roche.com/global/en/sitemap.xml', timeout=40, headers={'User-Agent': UA})
pairs = re.findall(r'<loc>\s*([^<\s]+)\s*</loc>\s*(?:<lastmod>\s*([^<\s]+)\s*</lastmod>)?', r.text)
news = [(l, d) for l, d in pairs if re.search(r'news-listing/20\d\d/', l) and (not d or d >= WIN)]
news.sort(key=lambda x: x[1] or '', reverse=True)
print('罗氏窗口内新闻:', len(news))
items = [json.loads(l) for l in open(F, encoding='utf-8') if l.strip()]
have = {i['链接'] for i in items}
n = 0
for loc, lm in news[:200]:
    if loc in have:
        continue
    slug = loc.rstrip('/').split('/')[-1].replace('.html', '').replace('-', ' ')
    items.append({'来源': '竞品官网 · 罗氏诊断', '主体': '罗氏诊断', '标题': slug,
                  '日期': lm or '', '链接': loc, '标签': [], '抓取时间': datetime.now().isoformat(timespec='seconds')})
    n += 1
with open(F, 'w', encoding='utf-8') as f:
    for i in items:
        f.write(json.dumps(i, ensure_ascii=False) + '\n')
print('新增罗氏条目:', n, '｜ 文件总条目:', len(items))
