PDF-service/process_reports.py

76 lines
2.5 KiB
Python
Raw Normal View History

import json
import requests
import time
from datetime import datetime
# 读取JSON文件
with open('修复.json', 'r', encoding='utf-8') as f:
patients = json.load(f)
# 创建日志文件
log_filename = f'api_requests_{datetime.now().strftime("%Y%m%d_%H%M%S")}.log'
success_count = 0
fail_count = 0
def log_result(medical_sn, p_name, success, response):
global success_count, fail_count
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
status = "成功" if success else "失败"
if success:
success_count += 1
else:
fail_count += 1
with open(log_filename, 'a', encoding='utf-8') as f:
f.write(f"[{timestamp}] 体检编号: {medical_sn}, 姓名: {p_name}, 状态: {status}\n")
f.write(f"响应内容: {json.dumps(response, ensure_ascii=False)}\n")
f.write("-" * 80 + "\n")
# API请求函数
def request_report(medical_sn, p_name):
url = f"https://pacs.gw12320.com/adminInspect/admin-api/inspect/patient/generateReport"
params = {"medicalSn": medical_sn}
# 添加请求头完全匹配Postman的设置
headers = {
'Content-Type': 'application/json',
'User-Agent': 'PostmanRuntime/7.43.3',
'Accept': '*/*',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive',
'Host': 'pacs.gw12320.com'
}
try:
print(f"正在请求 {p_name} 的报告,请等待接口返回...")
response = requests.get(url, params=params, headers=headers, verify=False)
response.raise_for_status() # 检查HTTP错误
result = response.json()
# 修改成功判断条件code为0表示成功
success = result.get("code", -1) == 0
log_result(medical_sn, p_name, success, result)
return success
except Exception as e:
error_msg = str(e)
print(f"请求失败: {error_msg}")
log_result(medical_sn, p_name, False, {"error": error_msg})
return False
# 禁用SSL警告
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# 主处理循环
print("开始处理请求...")
for patient in patients:
medical_sn = patient["medicalSn"]
p_name = patient["pName"]
print(f"\n开始处理: {p_name} (体检编号: {medical_sn})")
success = request_report(medical_sn, p_name)
print(f"处理完成: {'成功' if success else '失败'}")
print("\n所有请求处理完成!")
print(f"成功请求数: {success_count}")
print(f"失败请求数: {fail_count}")
print(f"详细日志已保存到: {log_filename}")