deason 3 年 前
コミット
a67f3571f9
2 ファイル変更83 行追加51 行削除
  1. 3 1
      exportexamdetail/common.py
  2. 80 50
      exportexamdetail/examInf.py

+ 3 - 1
exportexamdetail/common.py

@@ -35,7 +35,9 @@ def convert_to_pdf(sourceHtml,outpdffile):
     try:
        pdfkit.from_file(sourceHtml, outpdffile, configuration=configuration, options=options)
     except Exception as e:
-        print('ignore exception..')
+        {
+            # print(str(e))
+        }
 
 
 #生成资源文件目录访问路径

+ 80 - 50
exportexamdetail/examInf.py

@@ -2,41 +2,52 @@
    接口
 
 """
-import requests
-import config
-import bs4
 import os
 import time
+
+import bs4
+import requests
+
 import common
+import config
+
 
 def calctime(m_func):
-    def decorter(*args,**kwargs):
-        start=time.time()
-        res=m_func(*args,**kwargs)
-        end=time.time()
+    def decorter(*args, **kwargs):
+        start = time.time()
+        res = m_func(*args, **kwargs)
+        end = time.time()
         # print(m_func.__name__+" spend time is "+str(end-start))
         return res
+
     return decorter
 
 
 @calctime
 def login():
-    url="https://"+config.domain+"/api/ecs_core/auth/login"
-    data={"rootOrgId":None,"domain":config.domain.replace("exam-cloud.cn","ecs.qmth.com.cn"),"accountType":"COMMON_LOGIN_NAME","accountValue":config.username,"password":config.password,"smsCode":None}
-    result=requests.post(url,json=data)
-    if result.status_code==200:
-        config.header={
-            "key":result.json()["key"],
-            "token":result.json()['token']
+    url = "https://" + config.domain + "/api/ecs_core/auth/login"
+    data = {"rootOrgId": None, "domain": config.domain.replace("exam-cloud.cn", "ecs.qmth.com.cn"),
+            "accountType": "COMMON_LOGIN_NAME", "accountValue": config.username, "password": config.password,
+            "smsCode": None}
+    result = requests.post(url, json=data)
+    if result.status_code == 200:
+        config.header = {
+            "key": result.json()["key"],
+            "token": result.json()['token']
         }
-        config.rootOrgId=result.json()["rootOrgId"]
+        config.rootOrgId = result.json()["rootOrgId"]
     else:
         raise Exception("登录失败")
+
+
 @calctime
 def querydetail(curpage):
+    print('')
     login()
-    url = "https://"+config.domain+"/api/ecs_oe_admin/exam/record/detail/list"
-    params = {"rootOrgId": config.rootOrgId, "examRecordDataId": None, "hasStranger": None, "courseId": None, "courseLevel": None,
+
+    url = "https://" + config.domain + "/api/ecs_oe_admin/exam/record/detail/list"
+    params = {"rootOrgId": config.rootOrgId, "examRecordDataId": None, "hasStranger": None, "courseId": None,
+              "courseLevel": None,
               "examId": config.examId, "faceSuccessPercentLower": None, "faceSuccessPercentUpper": None,
               "identityNumber": None,
               "orgId": None, "studentCode": None, "studentName": None, "isWarn": None, "pageNo": curpage,
@@ -47,20 +58,26 @@ def querydetail(curpage):
     result = requests.post(url, json=params, headers=config.header)
     if result.status_code == 200:
         contents = result.json()["content"]
+        totalPages = result.json()["totalPages"]
+        totalElements = result.json()["totalElements"]
+        print(
+            "---> totalElements=" + str(totalElements) + " totalPages=" + str(totalPages) + " curPage=" + str(curpage))
+
         for content in contents:
             solveexamRecord(content)
-        totalPages = result.json()["totalPages"]
+
         if curpage < totalPages:
             querydetail(curpage + 1)
 
+
 @calctime
 def findExamRecordDataEntity(examRecordId):
-    url = "https://"+config.domain+"/api/ecs_oe_admin/exam/record/data/findExamRecordDataEntity"
+    url = "https://" + config.domain + "/api/ecs_oe_admin/exam/record/data/findExamRecordDataEntity"
     params = {
         "examRecordDataId": examRecordId
     }
     result = requests.get(url, params=params, headers=config.header)
-    print(result.json())
+    # print(result.json())
     if result.status_code == 200:
         return result.json()
     else:
@@ -68,9 +85,10 @@ def findExamRecordDataEntity(examRecordId):
         # print(result.json())
         return None
 
+
 @calctime
 def getExamRecordPaperStruct(examRecordDataId):
-    url = "https://"+config.domain+"/api/ecs_oe_admin/examRecordPaperStruct/getExamRecordPaperStruct"
+    url = "https://" + config.domain + "/api/ecs_oe_admin/examRecordPaperStruct/getExamRecordPaperStruct"
     params = {
         "examRecordDataId": examRecordDataId
     }
@@ -80,9 +98,10 @@ def getExamRecordPaperStruct(examRecordDataId):
     else:
         return None
 
+
 @calctime
 def getExamRecordQuestions(examRecordDataId):
-    url = "https://"+config.domain+"/api/ecs_oe_admin/examRecordQuestions/getExamRecordQuestions"
+    url = "https://" + config.domain + "/api/ecs_oe_admin/examRecordQuestions/getExamRecordQuestions"
     params = {
         "examRecordDataId": examRecordDataId
     }
@@ -92,9 +111,10 @@ def getExamRecordQuestions(examRecordDataId):
     else:
         return None
 
+
 @calctime
 def getQuesitons(courseCode, groupCode, questionId):
-    url = "https://"+config.domain+"/api/ecs_ques/default_question/question"
+    url = "https://" + config.domain + "/api/ecs_ques/default_question/question"
     data = {"examId": config.examId, "courseCode": courseCode, "groupCode": groupCode, "questionId": questionId}
     result = requests.post(url, json=data, headers=config.header)
     if result.status_code == 200:
@@ -103,21 +123,31 @@ def getQuesitons(courseCode, groupCode, questionId):
         return None
 
 
-
 def solveexamRecord(examRecorddetail):
-    record = bs4.BeautifulSoup(open(common.resource_path(os.path.join("template","template.html")), "r", encoding="utf-8"), features="html.parser")
     identityNumber = examRecorddetail["identityNumber"]
     studentName = examRecorddetail["studentName"]
     examrRecordDataId = examRecorddetail["id"]
-    # print("Start examRecordDataId" + str(examrRecordDataId))
     courseCode = examRecorddetail["courseCode"]
     courseName = examRecorddetail["courseName"]
     studentCode = examRecorddetail["studentCode"]
-    result=findExamRecordDataEntity(examrRecordDataId)
-    #print(result)
+
+    record = bs4.BeautifulSoup(
+        open(common.resource_path(os.path.join("template", "template.html")), "r", encoding="utf-8"),
+        features="html.parser")
+
+    filepath = config.outputpath + os.sep + str(
+        config.examId) + os.sep + courseCode + "_" + identityNumber + "_" + studentName + ".pdf"
+    if os.path.exists(filepath):
+        print("已处理,跳过! " + courseCode + "_" + identityNumber + "_" + studentName + ".pdf")
+        return None
+
+    result = findExamRecordDataEntity(examrRecordDataId)
+    # print(result)
+
     groupCode = result["examRecord"]["paperType"]
     defaultPaper = getExamRecordPaperStruct(examrRecordDataId)["defaultPaper"]
     papername = defaultPaper["name"]
+
     record.select_one("#paperName").string = str(papername)
     record.select_one("#studentInfoTable > tbody > tr > td:nth-of-type(1)").string = str(examrRecordDataId)
     record.select_one("#studentInfoTable > tbody > tr > td:nth-of-type(2)").string = str(studentCode)
@@ -175,7 +205,7 @@ def solveexamRecord(examRecorddetail):
                 content_div.append(soup)
                 rightAnswer = questionUnit["rightAnswer"]
                 quesitonEntity = quesitonEntites[sub_number - 1]
-                optionorder=quesitonEntity["optionPermutation"]
+                optionorder = quesitonEntity["optionPermutation"]
                 questionScore = quesitonEntity["questionScore"]
                 studentAnswer = quesitonEntity["studentAnswer"]
                 questionOptionList = questionUnit["questionOptionList"]
@@ -183,16 +213,17 @@ def solveexamRecord(examRecorddetail):
                 if questionOptionList:
                     option_idex = 0
                     for option in optionorder:
-                        optiondetail=questionOptionList[option]
+                        optiondetail = questionOptionList[option]
                         optiondiv = record.new_tag("div", attrs={"data-v-3575fe23": "", "style": "clear:both"})
                         datadiv1.append(optiondiv)
-                        optiondiv1 = record.new_tag("div", attrs={"data-v-3575fe23": "",                                                               "style": "display: flex; flex-direction: row;"})
+                        optiondiv1 = record.new_tag("div", attrs={"data-v-3575fe23": "",
+                                                                  "style": "display: flex; flex-direction: row;"})
                         optiondiv.append(optiondiv1)
                         optionIndexdiv = record.new_tag("div", attrs={"data-v-3575fe23": "", "style": "float:left"})
                         optiondiv1.append(optionIndexdiv)
                         optionContentdiv = record.new_tag("div", attrs={"data-v-3575fe23": "", "style": "float:left"})
                         optiondiv1.append(optionContentdiv)
-                        optionIndexdiv.string = chr(65+int(option_idex)) + "、"
+                        optionIndexdiv.string = chr(65 + int(option_idex)) + "、"
                         soup = bs4.BeautifulSoup(optiondetail["body"], features="html.parser")
                         optionContentdiv.append(soup)
                         option_idex = option_idex + 1
@@ -215,15 +246,15 @@ def solveexamRecord(examRecorddetail):
                 studentAnswerdiv.append(studentAnswercontentspan)
 
                 if questionType == "SINGLE_CHOICE" or questionType == "MULTIPLE_CHOICE":
-                    correctAnswer=quesitonEntity["correctAnswer"]
+                    correctAnswer = quesitonEntity["correctAnswer"]
                     # print(correctAnswer,studentAnswer)
 
                     rightAnswerdivcontent.append(
-                        selectorder(correctAnswer,optionorder))
+                        selectorder(correctAnswer, optionorder))
                     studentAnswercontentspan.append(
-                       selectorder(studentAnswer,optionorder))
-                elif questionType=="TRUE_OR_FALSE":
-                    f=lambda x: "" if not x else "正确" if x=="true" else "错误"
+                        selectorder(studentAnswer, optionorder))
+                elif questionType == "TRUE_OR_FALSE":
+                    f = lambda x: "" if not x else "正确" if x == "true" else "错误"
                     # print(f(rightAnswer[0]))
                     rightAnswerdivcontent.append(f(rightAnswer[0]))
                     studentAnswercontentspan.append(f(studentAnswer))
@@ -233,30 +264,29 @@ def solveexamRecord(examRecorddetail):
                     studentAnswercontentspan.append(
                         bs4.BeautifulSoup(studentAnswer if studentAnswer else '', features="html.parser"))
 
-
                 # print(sub_number, " ", body, " ", studentAnswer, rightAnswer, questionScore)
                 sub_number = sub_number + 1
 
         main_number = main_number + 1
 
-    with open(common.resource_path(os.path.join("template","temp.html")), 'w', encoding="utf-8") as fp:
-        # write the current soup content
+    with open(common.resource_path(os.path.join("template", "temp.html")), 'w', encoding="utf-8") as fp:
         fp.write(record.prettify())
-    if not os.path.exists(config.outputpath+os.sep+str(config.examId)):
-         os.makedirs(config.outputpath+os.sep+str(config.examId))
-    filepath=config.outputpath+os.sep+str(config.examId)+os.sep+courseCode+'_'+identityNumber+'_'+studentName+".pdf"
-    common.convert_to_pdf(common.resource_path(os.path.join("template","temp.html")),filepath)
-    print("Finish examRecordDataId " + str(examrRecordDataId))
-    print('')
+
+    if not os.path.exists(config.outputpath + os.sep + str(config.examId)):
+        os.makedirs(config.outputpath + os.sep + str(config.examId))
+
+    common.convert_to_pdf(common.resource_path(os.path.join("template", "temp.html")), filepath)
+    print("已完成! " + courseCode + "_" + identityNumber + "_" + studentName + ".pdf examrRecordDataId=" + str(
+        examrRecordDataId))
 
 
-def selectorder(answer,optionpremutation):
+def selectorder(answer, optionpremutation):
     if answer:
-        answerstr=[]
+        answerstr = []
         for answerorder in answer:
             # print(optionpremutation)
-            index=optionpremutation.index(int(answerorder))
-            answerstr.append(chr(65+index))
+            index = optionpremutation.index(int(answerorder))
+            answerstr.append(chr(65 + index))
         answerstr.sort()
         return ",".join(answerstr)
     else: