- 新增搜索引擎选择下拉框 - 支持 Bing 中国(默认)、Bing 国际、Google、百度 - 后端 API 支持 engine 参数 - 添加百度搜索结果解析方法
192 lines
5.7 KiB
Python
192 lines
5.7 KiB
Python
"""
|
|
文章内容库管理 API
|
|
"""
|
|
from flask import Blueprint, request, jsonify
|
|
from models.database import db
|
|
from services.search_service import search_service
|
|
|
|
bp = Blueprint('articles', __name__, url_prefix='/api/articles')
|
|
|
|
@bp.route('', methods=['GET'])
|
|
def list_articles():
|
|
"""获取文章列表"""
|
|
limit = request.args.get('limit', 100, type=int)
|
|
offset = request.args.get('offset', 0, type=int)
|
|
|
|
articles = db.get_all_articles(limit=limit, offset=offset)
|
|
|
|
# 解析JSON字段
|
|
for article in articles:
|
|
article['product_names'] = __import__('json').loads(article.get('product_names', '[]'))
|
|
article['keywords'] = __import__('json').loads(article.get('keywords', '[]'))
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'articles': articles,
|
|
'count': len(articles)
|
|
})
|
|
|
|
@bp.route('/search', methods=['GET'])
|
|
def search_articles():
|
|
"""搜索文章"""
|
|
keyword = request.args.get('q', '')
|
|
category = request.args.get('category')
|
|
|
|
if not keyword:
|
|
return jsonify({'error': '请提供搜索关键词'}), 400
|
|
|
|
articles = db.search_articles(keyword, category)
|
|
|
|
# 解析JSON字段
|
|
for article in articles:
|
|
article['product_names'] = __import__('json').loads(article.get('product_names', '[]'))
|
|
article['keywords'] = __import__('json').loads(article.get('keywords', '[]'))
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'articles': articles,
|
|
'count': len(articles)
|
|
})
|
|
|
|
@bp.route('/<int:article_id>', methods=['GET'])
|
|
def get_article(article_id):
|
|
"""获取文章详情"""
|
|
article = db.get_article_by_id(article_id)
|
|
|
|
if not article:
|
|
return jsonify({'error': '文章不存在'}), 404
|
|
|
|
article['product_names'] = __import__('json').loads(article.get('product_names', '[]'))
|
|
article['keywords'] = __import__('json').loads(article.get('keywords', '[]'))
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'article': article
|
|
})
|
|
|
|
@bp.route('', methods=['POST'])
|
|
def create_article():
|
|
"""创建文章(手动添加)"""
|
|
data = request.get_json()
|
|
|
|
required_fields = ['product_names', 'summary', 'content', 'source']
|
|
for field in required_fields:
|
|
if field not in data:
|
|
return jsonify({'error': f'缺少必填字段: {field}'}), 400
|
|
|
|
article_id = search_service.save_to_articles(
|
|
product_names=data['product_names'],
|
|
category=data.get('category'),
|
|
keywords=data.get('keywords', []),
|
|
summary=data['summary'],
|
|
content=data['content'],
|
|
source=data['source'],
|
|
url=data.get('url')
|
|
)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'article_id': article_id,
|
|
'message': '文章创建成功'
|
|
})
|
|
|
|
@bp.route('/<int:article_id>', methods=['DELETE'])
|
|
def delete_article(article_id):
|
|
"""删除文章"""
|
|
success = db.delete_article(article_id)
|
|
|
|
if success:
|
|
return jsonify({
|
|
'success': True,
|
|
'message': '文章已删除'
|
|
})
|
|
else:
|
|
return jsonify({'error': '文章不存在或删除失败'}), 404
|
|
|
|
@bp.route('/fetch', methods=['POST'])
|
|
def fetch_article():
|
|
"""从URL抓取文章"""
|
|
data = request.get_json()
|
|
url = data.get('url')
|
|
|
|
if not url:
|
|
return jsonify({'error': '请提供URL'}), 400
|
|
|
|
result = search_service.fetch_url_content(url)
|
|
|
|
if result:
|
|
# 自动保存到内容库
|
|
article_id = search_service.save_to_articles(
|
|
product_names=data.get('product_names', [result['title']]),
|
|
category=data.get('category'),
|
|
keywords=data.get('keywords', []),
|
|
summary=result.get('description', ''),
|
|
content=result['content'],
|
|
source=url,
|
|
url=url
|
|
)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'article_id': article_id,
|
|
'data': result
|
|
})
|
|
else:
|
|
return jsonify({'error': '抓取失败'}), 500
|
|
|
|
@bp.route('/internet-search', methods=['POST'])
|
|
def internet_search():
|
|
"""互联网搜索(浏览器方式)"""
|
|
data = request.get_json()
|
|
keyword = data.get('keyword', '')
|
|
max_results = data.get('max_results', 10)
|
|
engine = data.get('engine', 'bing_cn') # 默认 Bing 中国
|
|
|
|
if not keyword:
|
|
return jsonify({'error': '请提供搜索关键词'}), 400
|
|
|
|
# 执行互联网搜索
|
|
results = search_service.search_internet(keyword, max_results, engine)
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'keyword': keyword,
|
|
'engine': engine,
|
|
'results': results,
|
|
'count': len(results)
|
|
})
|
|
|
|
@bp.route('/internet-search-and-fetch', methods=['POST'])
|
|
def internet_search_and_fetch():
|
|
"""互联网搜索并抓取内容"""
|
|
data = request.get_json()
|
|
keyword = data.get('keyword', '')
|
|
max_results = data.get('max_results', 5)
|
|
category = data.get('category')
|
|
|
|
if not keyword:
|
|
return jsonify({'error': '请提供搜索关键词'}), 400
|
|
|
|
# 执行互联网搜索
|
|
results = search_service.search_internet(keyword, max_results)
|
|
|
|
# 抓取每个结果的详细内容
|
|
fetched_results = []
|
|
for r in results:
|
|
url = r.get('url')
|
|
if url:
|
|
content = search_service.fetch_url_content(url)
|
|
if content:
|
|
fetched_results.append({
|
|
'title': r['title'],
|
|
'url': url,
|
|
'source': r['source'],
|
|
'fetched_content': content
|
|
})
|
|
|
|
return jsonify({
|
|
'success': True,
|
|
'keyword': keyword,
|
|
'results': fetched_results,
|
|
'count': len(fetched_results)
|
|
}) |