#!/usr/bin/env python3
"""读取所有飞书链接并生成汇总报告"""

import json
import requests
from bs4 import BeautifulSoup
import time
from datetime import datetime

def extract_content(url):
    """从飞书链接提取内容"""
    try:
        resp = requests.get(url, timeout=10, headers={'User-Agent': 'Mozilla/5.0'})
        soup = BeautifulSoup(resp.text, 'html.parser')
        
        # 获取标题
        title = soup.find('title')
        title_text = title.text.strip() if title else ''
        
        # 移除脚本和样式
        for script in soup(['script', 'style']):
            script.decompose()
        
        # 获取文本内容
        text = soup.get_text(strip=True)
        
        # 提取关键信息
        # 查找日报内容（通常在特定区域）
        content_start = text.find('日报')
        if content_start == -1:
            content_start = text.find('工作')
        if content_start == -1:
            content_start = 0
        
        content = text[content_start:content_start + 800]
        
        return {
            'title': title_text,
            'content': content,
            'success': True
        }
    except Exception as e:
        return {
            'title': '',
            'content': str(e),
            'success': False
        }

def main():
    # 读取链接
    with open('D:/desktop/feishu_docs_summary/all_feishu_links.json', 'r', encoding='utf-8') as f:
        data = json.load(f)
    
    links = data.get('links', [])
    print(f'共 {len(links)} 个链接')
    
    # 按组分类
    groups = {}
    for link in links:
        group = link['group']
        if group not in groups:
            groups[group] = []
        groups[group].append(link)
    
    # 读取每个组的文档
    all_docs = []
    for group_name, group_links in groups.items():
        print(f'\n=== {group_name} ({len(group_links)} 个链接) ===')
        
        for i, link in enumerate(group_links[:5], 1):  # 每个组读取前5个
            print(f'  [{i}/5] {link["sender"]} - {link["url"][:50]}...')
            result = extract_content(link['url'])
            result['sender'] = link['sender']
            result['time'] = link['time']
            result['group'] = group_name
            result['url'] = link['url']
            all_docs.append(result)
            time.sleep(0.3)
    
    # 保存结果
    output = {
        'date': datetime.now().strftime('%Y-%m-%d'),
        'total_links': len(links),
        'docs_read': len(all_docs),
        'documents': all_docs
    }
    
    with open('D:/desktop/feishu_docs_summary/feishu_docs_content.json', 'w', encoding='utf-8') as f:
        json.dump(output, f, ensure_ascii=False, indent=2)
    
    # 生成 Markdown 报告
    report = f'# 飞书文档汇总报告\n\n'
    report += f'**日期**: {datetime.now().strftime("%Y-%m-%d")}\n'
    report += f'**链接总数**: {len(links)}\n'
    report += f'**已读取**: {len(all_docs)}\n\n'
    
    for group_name in groups.keys():
        group_docs = [d for d in all_docs if d['group'] == group_name]
        if group_docs:
            report += f'## {group_name}\n\n'
            for doc in group_docs:
                report += f'### {doc["sender"]} ({doc["time"]})\n'
                report += f'**链接**: {doc["url"]}\n\n'
                report += f'{doc["content"][:300]}\n\n'
                report += '---\n\n'
    
    with open('D:/desktop/feishu_docs_summary/feishu_summary_report.md', 'w', encoding='utf-8') as f:
        f.write(report)
    
    print(f'\n已保存到:')
    print(f'  - feishu_docs_content.json')
    print(f'  - feishu_summary_report.md')

if __name__ == '__main__':
    main()
