2024-11-23 17:50:32 +08:00
|
|
|
|
# flask_app/routes/little_zbparse.py
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
2024-12-03 09:58:50 +08:00
|
|
|
|
from flask import Blueprint, jsonify, g
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
2024-11-25 16:04:53 +08:00
|
|
|
|
from flask_app.ConnectionLimiter import require_connection_limit
|
2024-12-05 15:01:37 +08:00
|
|
|
|
from flask_app.general.format_change import download_file
|
2024-12-16 13:56:28 +08:00
|
|
|
|
from flask_app.routes.小解析main import little_parse_main
|
2024-12-02 11:10:49 +08:00
|
|
|
|
from flask_app.routes.utils import validate_and_setup_logger
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
little_zbparse_bp = Blueprint('little_zbparse', __name__)
|
2024-11-25 16:24:38 +08:00
|
|
|
|
|
2025-01-09 15:17:58 +08:00
|
|
|
|
def create_response(message, status, data=''):
|
2025-01-09 14:59:28 +08:00
|
|
|
|
"""
|
|
|
|
|
创建统一格式的 JSON 响应。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
message (str): 响应消息。
|
|
|
|
|
status (str): 状态标记,'success' 或 'error'。
|
|
|
|
|
data (str, optional): 响应数据。默认为空字符串。
|
|
|
|
|
status_code (int, optional): HTTP 状态码。默认为 200。
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
Response: Flask 响应对象。
|
|
|
|
|
"""
|
|
|
|
|
response = jsonify({
|
|
|
|
|
'message': message,
|
|
|
|
|
'status': status,
|
|
|
|
|
'data': data
|
|
|
|
|
})
|
|
|
|
|
return response
|
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
@little_zbparse_bp.route('/little_zbparse', methods=['POST'])
|
2024-11-26 11:32:24 +08:00
|
|
|
|
@validate_and_setup_logger
|
2024-12-02 11:10:49 +08:00
|
|
|
|
@require_connection_limit(timeout=300)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
def little_zbparse():
|
|
|
|
|
logger = g.logger
|
2024-12-02 11:10:49 +08:00
|
|
|
|
file_url = g.file_url
|
|
|
|
|
zb_type = g.zb_type
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
|
|
|
|
# 检查是否为错误响应(假设装饰器返回的是一个响应元组)
|
|
|
|
|
if isinstance(file_url, tuple):
|
2024-11-23 17:50:32 +08:00
|
|
|
|
return file_url
|
|
|
|
|
|
|
|
|
|
try:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.info(f"Starting parsing URL: {file_url}")
|
2024-11-23 17:50:32 +08:00
|
|
|
|
final_json_path = download_and_process_file(file_url, zb_type)
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
if not final_json_path:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
return create_response(
|
|
|
|
|
message='上传的文件非招标文件或文件内容不完整!',
|
|
|
|
|
status='error',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=''
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
response = generate_response(final_json_path)
|
|
|
|
|
return response
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
except Exception as e:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.error(f'Exception occurred: {e}')
|
|
|
|
|
return create_response(
|
|
|
|
|
message='解析遇到不知名错误!',
|
|
|
|
|
status='error',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=''
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
def download_and_process_file(file_url, zb_type):
|
|
|
|
|
"""
|
2025-01-09 14:59:28 +08:00
|
|
|
|
下载并处理文件,根据 zb_type 选择处理函数。
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
参数:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
file_url (str): 文件的 URL 地址。
|
|
|
|
|
zb_type (int): 标的类型,1 表示工程标,2 表示货物标。
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
返回:
|
2025-01-09 14:59:28 +08:00
|
|
|
|
str or None: 处理后的文件路径,或在失败时返回 None。
|
2024-11-23 17:50:32 +08:00
|
|
|
|
"""
|
|
|
|
|
logger = g.logger
|
|
|
|
|
output_folder = g.output_folder
|
|
|
|
|
filename = "ztbfile"
|
|
|
|
|
downloaded_filename = os.path.join(output_folder, filename)
|
|
|
|
|
|
|
|
|
|
downloaded_filepath, file_type = download_file(file_url, downloaded_filename)
|
|
|
|
|
|
|
|
|
|
if downloaded_filepath is None or file_type == 4:
|
|
|
|
|
logger.error("Unsupported file type or failed to download file")
|
|
|
|
|
return None
|
|
|
|
|
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.info(f"Local file path: {downloaded_filepath}")
|
2024-11-23 17:50:32 +08:00
|
|
|
|
processed_file_path = little_parse_main(output_folder, downloaded_filepath, file_type, zb_type, g.unique_id)
|
|
|
|
|
return processed_file_path
|
|
|
|
|
|
|
|
|
|
def generate_response(final_json_path):
|
2025-01-09 14:59:28 +08:00
|
|
|
|
"""
|
|
|
|
|
生成最终的成功响应或错误响应。
|
|
|
|
|
|
|
|
|
|
参数:
|
|
|
|
|
final_json_path (str): 处理后的 JSON 文件路径。
|
|
|
|
|
|
|
|
|
|
返回:
|
|
|
|
|
tuple: Flask 响应对象和状态码。
|
|
|
|
|
"""
|
2024-11-23 17:50:32 +08:00
|
|
|
|
logger = g.logger
|
2025-01-09 14:59:28 +08:00
|
|
|
|
|
2024-11-23 17:50:32 +08:00
|
|
|
|
if not os.path.exists(final_json_path):
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.error(f'final_json 未找到!: {final_json_path}')
|
|
|
|
|
return create_response(
|
|
|
|
|
message='final_json not found',
|
|
|
|
|
status='error',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=''
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(final_json_path, 'r', encoding='utf-8') as f:
|
|
|
|
|
zbparse_data = json.load(f)
|
|
|
|
|
json_str = json.dumps(zbparse_data, ensure_ascii=False)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f'Error reading or parsing final_json: {e}')
|
|
|
|
|
return create_response(
|
|
|
|
|
message='Error processing final_json.',
|
|
|
|
|
status='error',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=''
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return create_response(
|
|
|
|
|
message='Little Parse processed successfully',
|
|
|
|
|
status='success',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=json_str
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|