2024-11-23 17:50:32 +08:00
|
|
|
|
# flask_app/routes/little_zbparse.py
|
|
|
|
|
|
|
|
|
|
import json
|
|
|
|
|
import os
|
2025-01-10 14:30:35 +08:00
|
|
|
|
from flask import Blueprint, 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
|
2025-01-14 17:10:38 +08:00
|
|
|
|
from flask_app.routes.utils import validate_and_setup_logger, create_response_normal, log_error_unique_id
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
little_zbparse_bp = Blueprint('little_zbparse', __name__)
|
|
|
|
|
@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)
|
2025-02-07 15:27:24 +08:00
|
|
|
|
def little_zbparse(): #小解析
|
2024-11-23 17:50:32 +08:00
|
|
|
|
logger = g.logger
|
2024-12-02 11:10:49 +08:00
|
|
|
|
file_url = g.file_url
|
|
|
|
|
zb_type = g.zb_type
|
2025-01-14 17:10:38 +08:00
|
|
|
|
unique_id = g.unique_id
|
2024-11-23 17:50:32 +08:00
|
|
|
|
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)
|
|
|
|
|
if not final_json_path:
|
2025-01-14 17:10:38 +08:00
|
|
|
|
logger.error("错误的招标文件类型或者文件类型或者下载地址不存在!")
|
|
|
|
|
log_error_unique_id(unique_id,2)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
2025-01-14 17:10:38 +08:00
|
|
|
|
message='错误的招标文件类型或者文件类型或者下载地址不存在!',
|
2025-01-09 14:59:28 +08:00
|
|
|
|
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-14 17:10:38 +08:00
|
|
|
|
logger.error(f'内部服务器错误: {e}')
|
|
|
|
|
log_error_unique_id(unique_id,2)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
2025-01-14 17:10:38 +08:00
|
|
|
|
message='内部服务器错误',
|
2025-01-09 14:59:28 +08:00
|
|
|
|
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
|
2025-01-14 17:10:38 +08:00
|
|
|
|
unique_id = g.unique_id
|
2024-11-23 17:50:32 +08:00
|
|
|
|
filename = "ztbfile"
|
|
|
|
|
downloaded_filename = os.path.join(output_folder, filename)
|
2025-01-14 17:10:38 +08:00
|
|
|
|
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
|
2025-02-13 15:42:52 +08:00
|
|
|
|
downloaded_filepath, file_type = download_file(file_url, downloaded_filename,True)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
|
|
|
|
|
if downloaded_filepath is None or file_type == 4:
|
|
|
|
|
return None
|
|
|
|
|
|
2025-01-09 14:59:28 +08:00
|
|
|
|
logger.info(f"Local file path: {downloaded_filepath}")
|
2025-01-14 17:10:38 +08:00
|
|
|
|
processed_file_path = little_parse_main(output_folder, downloaded_filepath, file_type, mapped_zb_type, g.unique_id)
|
2024-11-23 17:50:32 +08:00
|
|
|
|
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-14 17:10:38 +08:00
|
|
|
|
unique_id = g.unique_id # 获取 unique_id
|
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}')
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 2)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
2025-01-09 14:59:28 +08:00
|
|
|
|
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}')
|
2025-01-14 17:10:38 +08:00
|
|
|
|
log_error_unique_id(unique_id, 2)
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
2025-01-09 14:59:28 +08:00
|
|
|
|
message='Error processing final_json.',
|
|
|
|
|
status='error',
|
2025-01-09 15:17:58 +08:00
|
|
|
|
data=''
|
2025-01-09 14:59:28 +08:00
|
|
|
|
)
|
|
|
|
|
|
2025-01-10 14:30:35 +08:00
|
|
|
|
return create_response_normal(
|
2025-01-09 14:59:28 +08:00
|
|
|
|
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
|
|
|
|
)
|