This commit is contained in:
2026-07-27 21:07:41 +08:00 Unverified
parent 7441a9ff7e
commit ad43aff9c7
5 changed files with 342 additions and 4 deletions
+30 -4
View File
@@ -1,8 +1,30 @@
import os
import time
import traceback
def get_grade_endpoint_modes():
"""从环境变量读取成绩接口模式,并返回接口尝试顺序。"""
grade_api_mode = (os.environ.get("GRADE_API_MODE") or "auto").strip().lower()
aliases = {
"dgxscj": "dg",
"xsgrcj": "xsgr",
}
grade_api_mode = aliases.get(grade_api_mode, grade_api_mode)
endpoint_modes = {
"auto": (True, False),
"dg": (True,),
"xsgr": (False,),
}
if grade_api_mode not in endpoint_modes:
raise ValueError(
"GRADE_API_MODE 配置无效,仅支持 auto、dg 或 xsgr。"
)
return endpoint_modes[grade_api_mode]
def get_grade(student_client, output_type="none"):
grade_endpoint_modes = get_grade_endpoint_modes()
try:
# 定义重试次数上限为5次
attempts = 5
@@ -19,10 +41,14 @@ def get_grade(student_client, output_type="none"):
# 使用while循环最多重试5次获取成绩信息
while attempts > 0:
# 调用student_client的get_grade方法获取成绩信息
# 学生成绩查询目前使用 cjcx_cxDgXscj 接口
grade_data = student_client.get_grade(use_personal_info=True).get("data", {})
grade = grade_data.get("courses", [])
# auto 模式优先使用 DgXscj,内容为空或接口不兼容时回退 Xsgrcj
for use_personal_info in grade_endpoint_modes:
grade_data = student_client.get_grade(
use_personal_info=use_personal_info
).get("data", {})
grade = grade_data.get("courses", [])
if grade:
break
# 如果grade不为空,跳出循环
if grade: