初始化参数数据自动化管理系统
功能: - 文章内容库管理 - 待处理产品列表管理 - 自动处理流程 - 智能搜索和数据提取 - ParamHub API集成 - 定时任务调度 部署端口: 16043
This commit is contained in:
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,135 @@
|
||||
"""
|
||||
文章内容库管理 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
|
||||
@@ -0,0 +1,216 @@
|
||||
"""
|
||||
产品处理 API
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.database import db
|
||||
from services.process_service import process_service
|
||||
|
||||
bp = Blueprint('products', __name__, url_prefix='/api/products')
|
||||
|
||||
@bp.route('/pending', methods=['GET'])
|
||||
def list_pending():
|
||||
"""获取待处理产品列表"""
|
||||
limit = request.args.get('limit', 20, type=int)
|
||||
order_by = request.args.get('order_by', 'priority')
|
||||
|
||||
products = db.get_pending_products(limit=limit, order_by=order_by)
|
||||
count = db.get_pending_count()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'products': products,
|
||||
'count': count
|
||||
})
|
||||
|
||||
@bp.route('/pending', methods=['POST'])
|
||||
def add_pending():
|
||||
"""添加待处理产品"""
|
||||
data = request.get_json()
|
||||
|
||||
if isinstance(data, dict):
|
||||
products = [data]
|
||||
elif isinstance(data, list):
|
||||
products = data
|
||||
else:
|
||||
return jsonify({'error': '无效的数据格式'}), 400
|
||||
|
||||
added_count = 0
|
||||
for item in products:
|
||||
if 'product_name' not in item:
|
||||
continue
|
||||
|
||||
result = db.add_pending_product(
|
||||
product_name=item['product_name'],
|
||||
category=item.get('category'),
|
||||
subcategory=item.get('subcategory'),
|
||||
priority=item.get('priority', 0),
|
||||
source=item.get('source', 'manual')
|
||||
)
|
||||
if result:
|
||||
added_count += 1
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'added_count': added_count,
|
||||
'message': f'成功添加 {added_count} 个产品到待处理列表'
|
||||
})
|
||||
|
||||
@bp.route('/pending/<product_name>', methods=['DELETE'])
|
||||
def remove_pending(product_name):
|
||||
"""从待处理列表移除产品"""
|
||||
success = db.remove_pending_product(product_name)
|
||||
|
||||
if success:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': '产品已从待处理列表移除'
|
||||
})
|
||||
else:
|
||||
return jsonify({'error': '产品不存在'}), 404
|
||||
|
||||
@bp.route('/processing', methods=['GET'])
|
||||
def list_processing():
|
||||
"""获取正在处理的产品列表"""
|
||||
products = db.get_processing_products()
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'products': products,
|
||||
'count': len(products)
|
||||
})
|
||||
|
||||
@bp.route('/history', methods=['GET'])
|
||||
def list_history():
|
||||
"""获取处理历史"""
|
||||
limit = request.args.get('limit', 100, type=int)
|
||||
history = db.get_process_history(limit=limit)
|
||||
|
||||
# 解析JSON字段
|
||||
for item in history:
|
||||
if item.get('details'):
|
||||
item['details'] = __import__('json').loads(item['details'])
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'history': history,
|
||||
'count': len(history)
|
||||
})
|
||||
|
||||
@bp.route('/history/<product_name>', methods=['GET'])
|
||||
def get_product_history(product_name):
|
||||
"""获取指定产品的处理历史"""
|
||||
history = db.get_history_by_product(product_name)
|
||||
|
||||
for item in history:
|
||||
if item.get('details'):
|
||||
item['details'] = __import__('json').loads(item['details'])
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'product_name': product_name,
|
||||
'history': history,
|
||||
'count': len(history)
|
||||
})
|
||||
|
||||
@bp.route('/process', methods=['POST'])
|
||||
def process_single():
|
||||
"""处理单个产品"""
|
||||
data = request.get_json()
|
||||
|
||||
if 'product_name' not in data:
|
||||
return jsonify({'error': '请提供产品名称'}), 400
|
||||
|
||||
product_info = {
|
||||
'product_name': data['product_name'],
|
||||
'category': data.get('category'),
|
||||
'subcategory': data.get('subcategory')
|
||||
}
|
||||
|
||||
# 检查是否正在处理
|
||||
processing = db.get_processing_products()
|
||||
if any(p['product_name'] == product_info['product_name'] for p in processing):
|
||||
return jsonify({'error': '该产品正在处理中'}), 400
|
||||
|
||||
# 添加到处理中列表
|
||||
db.start_processing(
|
||||
product_name=product_info['product_name'],
|
||||
category=product_info['category'],
|
||||
subcategory=product_info['subcategory']
|
||||
)
|
||||
|
||||
try:
|
||||
# 执行处理
|
||||
result = process_service.process_product(product_info)
|
||||
|
||||
# 从待处理列表移除
|
||||
db.remove_pending_product(product_info['product_name'])
|
||||
|
||||
# 如果发现新产品,已在process_service中添加到待处理列表
|
||||
|
||||
return jsonify({
|
||||
'success': result['success'],
|
||||
'message': result['message'],
|
||||
'review_id': result.get('review_id'),
|
||||
'new_products': result.get('new_products', [])
|
||||
})
|
||||
finally:
|
||||
# 完成处理,从处理中列表移除
|
||||
db.finish_processing(product_info['product_name'])
|
||||
|
||||
@bp.route('/process/batch', methods=['POST'])
|
||||
def process_batch():
|
||||
"""批量处理产品"""
|
||||
data = request.get_json()
|
||||
limit = data.get('limit', 5)
|
||||
|
||||
# 获取待处理产品
|
||||
products = db.get_pending_products(limit=limit)
|
||||
|
||||
if not products:
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': '没有待处理的产品',
|
||||
'processed': 0
|
||||
})
|
||||
|
||||
results = []
|
||||
for product in products:
|
||||
# 检查是否正在处理
|
||||
processing = db.get_processing_products()
|
||||
if any(p['product_name'] == product['product_name'] for p in processing):
|
||||
results.append({
|
||||
'product_name': product['product_name'],
|
||||
'success': False,
|
||||
'message': '正在处理中'
|
||||
})
|
||||
continue
|
||||
|
||||
# 添加到处理中列表
|
||||
db.start_processing(
|
||||
product_name=product['product_name'],
|
||||
category=product.get('category'),
|
||||
subcategory=product.get('subcategory')
|
||||
)
|
||||
|
||||
try:
|
||||
# 执行处理
|
||||
result = process_service.process_product(product)
|
||||
|
||||
# 从待处理列表移除
|
||||
db.remove_pending_product(product['product_name'])
|
||||
|
||||
results.append({
|
||||
'product_name': product['product_name'],
|
||||
'success': result['success'],
|
||||
'message': result['message'],
|
||||
'review_id': result.get('review_id')
|
||||
})
|
||||
finally:
|
||||
# 完成处理
|
||||
db.finish_processing(product['product_name'])
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'processed': len(results),
|
||||
'results': results
|
||||
})
|
||||
@@ -0,0 +1,68 @@
|
||||
"""
|
||||
系统管理 API
|
||||
"""
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.database import db
|
||||
|
||||
bp = Blueprint('system', __name__, url_prefix='/api/system')
|
||||
|
||||
@bp.route('/config', methods=['GET'])
|
||||
def get_config():
|
||||
"""获取系统配置"""
|
||||
configs = {
|
||||
'auto_process_enabled': db.get_system_config('auto_process_enabled', 'true'),
|
||||
'process_interval': db.get_system_config('process_interval', '300'),
|
||||
'batch_size': db.get_system_config('batch_size', '5')
|
||||
}
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'config': configs
|
||||
})
|
||||
|
||||
@bp.route('/config', methods=['PUT'])
|
||||
def update_config():
|
||||
"""更新系统配置"""
|
||||
data = request.get_json()
|
||||
|
||||
for key, value in data.items():
|
||||
db.set_system_config(key, str(value))
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'message': '配置已更新'
|
||||
})
|
||||
|
||||
@bp.route('/stats', methods=['GET'])
|
||||
def get_stats():
|
||||
"""获取系统统计信息"""
|
||||
pending_count = db.get_pending_count()
|
||||
processing_count = len(db.get_processing_products())
|
||||
|
||||
# 获取最近处理历史
|
||||
recent_history = db.get_process_history(limit=10)
|
||||
success_count = len([h for h in recent_history if h['status'] == 'submitted'])
|
||||
failed_count = len([h for h in recent_history if h['status'] in ['failed', 'error']])
|
||||
|
||||
# 获取内容库统计
|
||||
articles = db.get_all_articles(limit=1000)
|
||||
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'stats': {
|
||||
'pending_products': pending_count,
|
||||
'processing_products': processing_count,
|
||||
'recent_success': success_count,
|
||||
'recent_failed': failed_count,
|
||||
'total_articles': len(articles)
|
||||
}
|
||||
})
|
||||
|
||||
@bp.route('/health', methods=['GET'])
|
||||
def health_check():
|
||||
"""健康检查"""
|
||||
return jsonify({
|
||||
'success': True,
|
||||
'status': 'healthy',
|
||||
'message': '系统运行正常'
|
||||
})
|
||||
Reference in New Issue
Block a user