import json
from pathlib import Path

# 读取产品数据
json_path = Path(r'D:\ai_project\openclaw_project\test_page\research\2026-07-04_亚马逊热销苹果表带\products_verified.json')
with open(json_path, 'r', encoding='utf-8') as f:
    products = json.load(f)

# 生成飞书文档 XML 内容
xml_lines = ['<title>亚马逊热销Apple Watch表带汇总 (2026-07-04)</title>', '']
xml_lines.append('<h1>数据概览</h1>')
xml_lines.append('<p>本文档汇总了亚马逊美国站 Best Sellers 中前14款热销 Apple Watch 表带的产品信息。数据采集时间：2026年7月4日。</p>')
xml_lines.append('')

xml_lines.append('<h1>产品列表</h1>')

for p in products:
    rank = p['rank']
    name = p['name'].replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;')
    brand = p.get('brand', 'Unknown').replace('&', '&amp;')
    price = p['price']
    rating = p.get('rating', 'N/A')
    reviews = p.get('reviews', 'N/A')
    url = p['url']
    
    xml_lines.append(f'<h2>#{rank} {brand}</h2>')
    xml_lines.append(f'<p><b>产品名称：</b>{name}</p>')
    xml_lines.append(f'<p><b>价格：</b>{price}</p>')
    xml_lines.append(f'<p><b>评分：</b>{rating} ⭐ ({reviews} 评价)</p>')
    xml_lines.append(f'<p><a href="{url}">查看产品详情 →</a></p>')
    xml_lines.append('<hr/>')
    xml_lines.append('')

xml_lines.append('<h1>数据说明</h1>')
xml_lines.append('<ul>')
xml_lines.append('<li>数据来源：Amazon US Best Sellers - Apple Watch Bands</li>')
xml_lines.append('<li>采集时间：2026年7月4日</li>')
xml_lines.append('<li>产品数量：14款</li>')
xml_lines.append('<li>价格区间：$7.99 - $22.99</li>')
xml_lines.append('</ul>')

xml_content = '\n'.join(xml_lines)

# 保存到临时文件
output_path = Path(r'D:\ai_project\openclaw_project\test_page\research\2026-07-04_亚马逊热销苹果表带\feishu_doc_content.xml')
with open(output_path, 'w', encoding='utf-8') as f:
    f.write(xml_content)

print('✓ XML content generated')
print(f'Saved to: {output_path}')
print(f'Total products: {len(products)}')
