# flask_app/routes/little_zbparse.py import json import os from flask import Blueprint, g from flask_app.ConnectionLimiter import require_connection_limit, require_execution_timeout from flask_app.general.format_change import download_file from flask_app.routes.小解析main import little_parse_main from flask_app.routes.utils import validate_and_setup_logger, create_response_normal, log_error_unique_id little_zbparse_bp = Blueprint('little_zbparse', __name__) @little_zbparse_bp.route('/little_zbparse', methods=['POST']) @validate_and_setup_logger # @require_connection_limit(timeout=300) @require_execution_timeout(timeout=300) def little_zbparse(): #小解析 logger = g.logger file_url = g.file_url zb_type = g.zb_type unique_id = g.unique_id try: logger.info(f"Starting parsing URL: {file_url}") final_json_path = download_and_process_file(file_url, zb_type) if not final_json_path: logger.error("错误的招标文件类型或者文件类型或者下载地址不存在!") log_error_unique_id(unique_id,2) return create_response_normal( message='错误的招标文件类型或者文件类型或者下载地址不存在!', status='error', data='' ) response = generate_response(final_json_path) return response except Exception as e: logger.error(f'内部服务器错误: {e}') log_error_unique_id(unique_id,2) return create_response_normal( message='内部服务器错误', status='error', data='' ) def download_and_process_file(file_url, zb_type): """ 下载并处理文件,根据 zb_type 选择处理函数。 参数: file_url (str): 文件的 URL 地址。 zb_type (int): 标的类型,1 表示工程标,2 表示货物标。 返回: str or None: 处理后的文件路径,或在失败时返回 None。 """ logger = g.logger output_folder = g.output_folder unique_id = g.unique_id filename = "ztbfile" downloaded_filename = os.path.join(output_folder, filename) if zb_type not in [1, 2, 3]: logger.error(f"无效的 zb_type: {zb_type}. 期望 zb_type: 1、2 或 3") log_error_unique_id(unique_id, 2) # 假设这是一个用于记录错误的函数 return None # 返回 None 以指示失败 # 映射 zb_type,如果是 3 则映射为 2 mapped_zb_type = 2 if zb_type == 3 else zb_type downloaded_filepath, file_type = download_file(file_url, downloaded_filename,True) if downloaded_filepath is None or file_type == 4: return None logger.info(f"Local file path: {downloaded_filepath}") processed_file_path = little_parse_main(output_folder, downloaded_filepath, file_type, mapped_zb_type, g.unique_id) return processed_file_path def generate_response(final_json_path): """ 生成最终的成功响应或错误响应。 参数: final_json_path (str): 处理后的 JSON 文件路径。 返回: tuple: Flask 响应对象和状态码。 """ logger = g.logger unique_id = g.unique_id # 获取 unique_id if not os.path.exists(final_json_path): logger.error(f'final_json 未找到!: {final_json_path}') log_error_unique_id(unique_id, 2) return create_response_normal( message='final_json not found', status='error', data='' ) 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}') log_error_unique_id(unique_id, 2) return create_response_normal( message='Error processing final_json.', status='error', data='' ) return create_response_normal( message='Little Parse processed successfully', status='success', data=json_str )