release: v2.2.0 - 博查新闻搜索功能

新增功能:
- 集成博查Web Search API,自动获取网站相关新闻
- News模型添加source_name和source_icon字段
- 新闻管理后台界面优化
- 网站详情页新闻展示(标题、摘要、来源、链接)
- 定期任务脚本支持批量获取新闻
- 完整的API路由和测试脚本

技术实现:
- NewsSearcher工具类封装博查API
- 智能新闻搜索和去重机制
- 数据库迁移脚本migrate_news_fields.py
- API路由:/api/fetch-site-news 和 /api/fetch-all-news
- Cron任务脚本:fetch_news_cron.py

修改文件:
- config.py: 添加博查API配置
- models.py: News模型扩展
- app.py: 新闻获取路由和NewsAdmin优化
- templates/detail_new.html: 新闻展示UI

新增文件:
- utils/news_searcher.py (271行)
- migrate_news_fields.py (99行)
- fetch_news_cron.py (167行)
- test_news_feature.py (142行)
- NEWS_FEATURE_v2.2.md (408行)

统计:9个文件,1348行新增

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Jowe
2025-12-30 22:04:35 +08:00
parent 00397a63b8
commit d7d21e19c9
9 changed files with 1348 additions and 8 deletions

142
test_news_feature.py Normal file
View File

@@ -0,0 +1,142 @@
"""
测试v2.2新闻功能 - 完整流程测试
"""
import os
import sys
from dotenv import load_dotenv
# 加载环境变量
load_dotenv()
# 添加项目路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from app import create_app
from models import db, Site, News
from utils.news_searcher import NewsSearcher
def test_news_feature():
"""测试新闻功能"""
print("=" * 60)
print("v2.2 新闻功能测试")
print("=" * 60)
# 创建应用上下文
app = create_app('development')
with app.app_context():
# 1. 测试API配置
print("\n[1/4] 检查API配置...")
api_key = app.config.get('BOCHA_API_KEY')
if not api_key:
print(">>> 错误BOCHA_API_KEY未配置")
return False
print(f">>> API Key: {api_key[:20]}...")
# 2. 测试数据库连接
print("\n[2/4] 检查数据库...")
try:
site_count = Site.query.filter_by(is_active=True).count()
print(f">>> 找到 {site_count} 个启用的网站")
if site_count == 0:
print(">>> 警告:没有可用的网站")
return False
except Exception as e:
print(f">>> 数据库错误:{e}")
return False
# 3. 测试新闻搜索
print("\n[3/4] 测试新闻搜索...")
searcher = NewsSearcher(api_key)
# 获取第一个网站
site = Site.query.filter_by(is_active=True).first()
print(f">>> 测试网站:{site.name}")
try:
news_items = searcher.search_site_news(
site_name=site.name,
site_url=site.url,
count=3,
freshness='oneWeek'
)
print(f">>> 找到 {len(news_items)} 条新闻")
if news_items:
print("\n新闻列表:")
for i, item in enumerate(news_items, 1):
print(f" {i}. {item['title'][:50]}...")
print(f" 来源:{item.get('site_name', '未知')}")
print(f" URL{item['url'][:60]}...")
except Exception as e:
print(f">>> 搜索失败:{e}")
return False
# 4. 测试保存到数据库
print(f"\n[4/4] 测试保存到数据库...")
if not news_items:
print(">>> 没有新闻可保存")
return True
try:
saved_count = 0
for item in news_items[:2]: # 只保存前2条作为测试
# 检查是否已存在
existing = News.query.filter_by(
site_id=site.id,
url=item['url']
).first()
if not existing:
news = News(
site_id=site.id,
title=item['title'],
content=item.get('summary') or item.get('snippet', ''),
url=item['url'],
source_name=item.get('site_name', ''),
source_icon=item.get('site_icon', ''),
published_at=item.get('published_at'),
news_type='Search Result',
is_active=True
)
db.session.add(news)
saved_count += 1
db.session.commit()
print(f">>> 成功保存 {saved_count} 条新闻")
# 验证保存
total_news = News.query.filter_by(site_id=site.id).count()
print(f">>> 该网站共有 {total_news} 条新闻记录")
except Exception as e:
db.session.rollback()
print(f">>> 保存失败:{e}")
return False
print("\n" + "=" * 60)
print(">>> 所有测试通过!")
print("=" * 60)
# 提供下一步建议
print("\n下一步操作:")
print(f"1. 访问网站详情页查看新闻http://localhost:5000/site/{site.code}")
print(f"2. 访问后台新闻管理http://localhost:5000/admin/newsadmin/")
print(f"3. 运行定期任务脚本python fetch_news_cron.py --limit 5")
return True
if __name__ == '__main__':
try:
success = test_news_feature()
sys.exit(0 if success else 1)
except Exception as e:
print(f"\n严重错误:{e}")
import traceback
traceback.print_exc()
sys.exit(1)