#!/usr/bin/env python3 """ 测试加密货币新闻获取(多源聚合) """ import asyncio import sys import os # 确保路径正确 script_dir = os.path.dirname(os.path.abspath(__file__)) project_root = os.path.dirname(script_dir) backend_dir = os.path.join(project_root, 'backend') sys.path.insert(0, backend_dir) from app.services.news_service import get_news_service async def main(): print("=" * 60) print("📰 测试加密货币新闻获取(多源聚合)") print("=" * 60) news_service = get_news_service() # 获取最新新闻 print("\n🔍 获取最新加密货币新闻...") news_list = await news_service.get_latest_news(limit=30) print(f"\n✅ 获取到 {len(news_list)} 条新闻\n") # 按来源分组统计 sources = {} for news in news_list: source = news.get('source', 'Unknown') sources[source] = sources.get(source, 0) + 1 print("📊 新闻来源统计:") for source, count in sources.items(): print(f" {source}: {count} 条") # 显示最新10条新闻 print("\n" + "=" * 60) print("📰 最新 10 条新闻") print("=" * 60) for i, news in enumerate(news_list[:10], 1): time_str = news.get('time_str', '') title = news.get('title', '') source = news.get('source', '') desc = news.get('description', '')[:100] print(f"\n{i}. [{time_str}] {source}") print(f" {title}") if desc: print(f" {desc}...") # 测试格式化给 LLM print("\n" + "=" * 60) print("🤖 格式化给 LLM 的新闻") print("=" * 60) formatted_news = news_service.format_news_for_llm(news_list[:5], max_items=5) print(formatted_news) print("\n" + "=" * 60) print("✅ 测试完成") print("=" * 60) if __name__ == "__main__": asyncio.run(main())