deason 3 лет назад
Родитель
Сommit
ef6729048d
2 измененных файлов с 145 добавлено и 130 удалено
  1. 34 30
      exportformarking/exportExamRecord.py
  2. 111 100
      exportformarking/exportjsonfile.py

+ 34 - 30
exportformarking/exportExamRecord.py

@@ -11,26 +11,28 @@ import logger
 import htmlhandler
 import csv
 
-questionlist=[]
-solvelist=[]
-examstudentlist=[]
-mediaimportlist=[]
-question_cookie={}
+questionlist = []
+solvelist = []
+examstudentlist = []
+mediaimportlist = []
+question_cookie = {}
 output = config.outputpath + "/" + str(config.examId)
 if not os.path.exists(output):
     os.makedirs(output)
 savepath = output + "/" + "question.csv"
-student_savepath=output + "/" + "examstudent.csv"
-mediaimport_savepath=output + "/" + "mediaimport.csv"
+student_savepath = output + "/" + "examstudent.csv"
+mediaimport_savepath = output + "/" + "mediaimport.csv"
+
+
 def main():
-    result=cloudservice.queryExamMarkingInfo()
+    result = cloudservice.queryExamMarkingInfo()
     if result:
         examRecordForMarkingBeanList = result['examRecordForMarkingBeanList']
         for examRecordForMarkingBean in examRecordForMarkingBeanList:
-               handleRecordData(examRecordForMarkingBean["courseId"],1)
+            handleRecordData(examRecordForMarkingBean["courseId"], 1)
         with open(savepath, 'w', encoding='gbk', newline='') as f:
             writer = csv.writer(f)
-            writer.writerow(['科目代码','科目名称','大题名称','main_number','sub_number','分值'])
+            writer.writerow(['科目代码', '科目名称', '大题名称', 'main_number', 'sub_number', '分值'])
             for row in questionlist:
                 writer.writerow(row)
         with open(mediaimport_savepath, 'w', encoding='gbk', newline='') as f:
@@ -45,10 +47,10 @@ def main():
                 writer.writerow(row)
 
 
-def handleRecordData(courseId,next):
-    result=cloudservice.queryExamValidRecordData(courseId,next);
+def handleRecordData(courseId, next):
+    result = cloudservice.queryExamValidRecordData(courseId, next);
     if result:
-        examRecordForMarkingBeanList=result["examRecordForMarkingBeanList"]
+        examRecordForMarkingBeanList = result["examRecordForMarkingBeanList"]
         if len(examRecordForMarkingBeanList) > 0:
             for examRecordForMarking in examRecordForMarkingBeanList:
                 # 取试卷题目
@@ -66,11 +68,12 @@ def handleRecordData(courseId,next):
                     os.makedirs(output)
                 savepath = output + "/" + str(examRecordForMarking["examRecordDataId"]) + ".json"
                 if course["code"] in solvelist:
-                    logger.info("已包含%s"%(course["code"]))
+                    logger.info("已包含%s" % (course["code"]))
                 else:
                     logger.info("开始处理" + str(course["code"]))
                     solvelist.append(course["code"])
-                    handlePaperStruct(course["code"],examRecordForMarking["paperType"],examRecordForMarking["basePaperId"],course["name"])
+                    handlePaperStruct(course["code"], examRecordForMarking["paperType"],
+                                      examRecordForMarking["basePaperId"], course["name"])
                 if os.path.exists(savepath):
                     logger.info("已有%s记录不再次进行处理" % str(examRecordForMarking["examRecordDataId"]))
                     continue
@@ -86,8 +89,7 @@ def handleRecordData(courseId,next):
             handleRecordData(examRecordForMarking['courseId'], next)
 
 
-
-def handleStudentPaperDetail(examRecordid,subjectdetail,courseCode,paperType):
+def handleStudentPaperDetail(examRecordid, subjectdetail, courseCode, paperType):
     questionBeanList = []
     querySubjectiveAnswerBeanList = subjectdetail["querySubjectiveAnswerBeanList"]
     question_unit_number = 1
@@ -104,10 +106,10 @@ def handleStudentPaperDetail(examRecordid,subjectdetail,courseCode,paperType):
             lastquestionId = questionId
             question_unit_number = 1
         if questionId in question_cookie:
-            questiondata=question_cookie[questionId]
+            questiondata = question_cookie[questionId]
         else:
             questiondata = cloudservice.getQuestion(questionId, courseCode, paperType)
-            question_cookie[questionId]=questiondata
+            question_cookie[questionId] = questiondata
         if questiondata["defaultQuestion"]["masterVersion"]["body"]:
             # 套题处理
             mainquestionbody = questiondata["defaultQuestion"]["masterVersion"]["body"]
@@ -138,16 +140,20 @@ def handleStudentPaperDetail(examRecordid,subjectdetail,courseCode,paperType):
                         questionBean["body"]["sections"] = mainbodyblocklist + quesbodyblocklist
                     else:
                         questionBean["body"]["sections"] = quesbodyblocklist
-                    questionBean["studentAnswer"]["sections"] =htmlhandler.splitQuesitonStudentAnswer(examRecordid,studentanswer,
-                                                                                           questionUnit["answerType"],
-                                                                                           questionUnit["questionType"])
+                    questionBean["studentAnswer"]["sections"] = htmlhandler.splitQuesitonStudentAnswer(examRecordid,
+                                                                                                       studentanswer,
+                                                                                                       questionUnit[
+                                                                                                           "answerType"],
+                                                                                                       questionUnit[
+                                                                                                           "questionType"])
                     questionBean["answer"]["sections"] = htmlhandler.splitQuesitonAnswer(questionUnit["rightAnswer"][0])
                     questionBeanList.append(questionBean)
                 current_subjective_number = current_subjective_number + 1
     return questionBeanList
 
-def handlePaperStruct(courseCode,groupCode,paperId,courseName):
-    result=cloudservice.queryPaperStruct(courseCode,groupCode,paperId,courseName)
+
+def handlePaperStruct(courseCode, groupCode, paperId, courseName):
+    result = cloudservice.queryPaperStruct(courseCode, groupCode, paperId, courseName)
     if result:
         questionGroupList = result["defaultPaper"]["questionGroupList"]
         main_number = 1
@@ -158,7 +164,8 @@ def handlePaperStruct(courseCode,groupCode,paperId,courseName):
                 questionUnitWrapperList = questionWrapper["questionUnitWrapperList"]
                 for questionUnitWrapper in questionUnitWrapperList:
                     questionbean = []
-                    if questionUnitWrapper["questionType"] == "ESSAY" or questionUnitWrapper["questionType"] == "FILL_UP":
+                    if questionUnitWrapper["questionType"] == "ESSAY" or questionUnitWrapper[
+                        "questionType"] == "FILL_UP":
                         questionbean.append(courseCode)
                         questionbean.append(courseName)
                         questionbean.append("第%d大题" % (main_number))
@@ -173,8 +180,5 @@ def handlePaperStruct(courseCode,groupCode,paperId,courseName):
         solvelist.remove(courseCode)
 
 
-
-
-
-if __name__=="__main__":
-    main()
+if __name__ == "__main__":
+    main()

+ 111 - 100
exportformarking/exportjsonfile.py

@@ -12,6 +12,8 @@ import os
 from bs4 import BeautifulSoup
 from bs4 import element
 import re
+
+
 def main():
     queryExamMarkingInfo()
     # questionlist=querySubjectInfo("8887593","A","B149")
@@ -24,41 +26,41 @@ def queryExamMarkingInfo():
     """
     获取阅卷信息
     """
-    url=config.oedomain+"api/core/oe/admin/examRecordForMarking/findExamRecordForMarkingInfo"
+    url = config.oedomain + "api/core/oe/admin/examRecordForMarking/findExamRecordForMarkingInfo"
     data = {
         "examId": config.examId,
         "courseId": "",
         "batchNum": ""
     }
-    headers=createRpcheader("4","OE")
+    headers = createRpcheader("4", "OE")
     result = requests.post(
         url, headers=headers,
         json=data)
-    if result.status_code==200:
-        examRecordForMarkingBeanList=result.json()['examRecordForMarkingBeanList']
+    if result.status_code == 200:
+        examRecordForMarkingBeanList = result.json()['examRecordForMarkingBeanList']
         for examRecordForMarkingBean in examRecordForMarkingBeanList:
-            queryExamValidRecordData(examRecordForMarkingBean['courseId'],1)
+            queryExamValidRecordData(examRecordForMarkingBean['courseId'], 1)
     else:
         print("查询阅卷信息失败")
 
 
-def queryExamValidRecordData(courseId,pageNo):
+def queryExamValidRecordData(courseId, pageNo):
     """
      获取全部考生的阅卷信息
     """
-    url=config.oedomain+"api/core/oe/admin/examRecordForMarking/queryValidExamRecordInfoPage"
+    url = config.oedomain + "api/core/oe/admin/examRecordForMarking/queryValidExamRecordInfoPage"
     data = {
         "examId": config.examId,
         "courseId": courseId,
         "start": pageNo,
-        "size":200
+        "size": 200
     }
-    headers = createRpcheader("4","OE")
+    headers = createRpcheader("4", "OE")
     result = requests.post(
         url, headers=headers,
         json=data)
-    if result.status_code==200:
-        examRecordForMarkingBeanList=result.json()['examRecordForMarkingBeanList']
+    if result.status_code == 200:
+        examRecordForMarkingBeanList = result.json()['examRecordForMarkingBeanList']
         if len(examRecordForMarkingBeanList) > 0:
             for examRecordForMarkingBean in examRecordForMarkingBeanList:
                 # 取试卷题目
@@ -69,62 +71,63 @@ def queryExamValidRecordData(courseId,pageNo):
                     os.makedirs(output)
                 savepath = output + "/" + str(examRecordForMarkingBean["examRecordDataId"]) + ".json"
                 if os.path.exists(savepath):
-                    print("已有%s记录不再次进行处理"%str(examRecordForMarkingBean["examRecordDataId"]))
+                    print("已有%s记录不再次进行处理" % str(examRecordForMarkingBean["examRecordDataId"]))
                     continue
-                questionbeanlist=querySubjectInfo(examRecordForMarkingBean["examRecordDataId"],examRecordForMarkingBean["paperType"],course["code"])
-                #存储到本地
+                questionbeanlist = querySubjectInfo(examRecordForMarkingBean["examRecordDataId"],
+                                                    examRecordForMarkingBean["paperType"], course["code"])
+                # 存储到本地
                 time.sleep(1)
-                with open(savepath,"w",encoding="utf-8") as file:
-                    json_str=json.dumps(questionbeanlist,indent=4,ensure_ascii=False)
+                with open(savepath, "w", encoding="utf-8") as file:
+                    json_str = json.dumps(questionbeanlist, indent=4, ensure_ascii=False)
                     file.write(json_str)
             next = result.json()["next"]
-            queryExamValidRecordData(courseId,next)
+            queryExamValidRecordData(courseId, next)
         else:
             print("没有剩余课程")
 
 
-def querySubjectInfo(examRecordDataId,paperType,courseCode):
+def querySubjectInfo(examRecordDataId, paperType, courseCode):
     """
       paperId:
       paperType:
       courseCode:
     """
-    print("开始处理%s"%(examRecordDataId))
-    questionBeanList=[]
-    url=config.oedomain+"api/core/oe/admin/examRecordQuestions/querySubjectiveAnswerList"
-    headers=createRpcheader("4","OE")
-    data={
-        "examRecordDataId":examRecordDataId
+    print("开始处理%s" % (examRecordDataId))
+    questionBeanList = []
+    url = config.oedomain + "api/core/oe/admin/examRecordQuestions/querySubjectiveAnswerList"
+    headers = createRpcheader("4", "OE")
+    data = {
+        "examRecordDataId": examRecordDataId
     }
     result = requests.post(
         url, headers=headers,
         json=data)
-    if result.status_code==200:
-        querySubjectiveAnswerBeanList=result.json()["querySubjectiveAnswerBeanList"]
-        question_unit_number=1
-        lastquestionId=None
+    if result.status_code == 200:
+        querySubjectiveAnswerBeanList = result.json()["querySubjectiveAnswerBeanList"]
+        question_unit_number = 1
+        lastquestionId = None
         for querySubjectiveAnswerBean in querySubjectiveAnswerBeanList:
-            questionId=querySubjectiveAnswerBean["questionId"]
-            main_number=querySubjectiveAnswerBean["mainNumber"]
-            sub_number=querySubjectiveAnswerBean["order"]
-            studentanswer=querySubjectiveAnswerBean["studentAnswer"]
-            print("studentanswer:",studentanswer)
-            questiondata=getQuestion(questionId,courseCode,paperType)
-            mainbodyblocklist=None
+            questionId = querySubjectiveAnswerBean["questionId"]
+            main_number = querySubjectiveAnswerBean["mainNumber"]
+            sub_number = querySubjectiveAnswerBean["order"]
+            studentanswer = querySubjectiveAnswerBean["studentAnswer"]
+            print("studentanswer:", studentanswer)
+            questiondata = getQuestion(questionId, courseCode, paperType)
+            mainbodyblocklist = None
             if lastquestionId == questionId:
                 question_unit_number = question_unit_number + 1  # 下一道主观题
             else:
                 lastquestionId = questionId
                 question_unit_number = 1
             if questiondata["defaultQuestion"]["masterVersion"]["body"]:
-                #套题处理
-                mainquestionbody=questiondata["defaultQuestion"]["masterVersion"]["body"]
-                mainbodyblocklist=splitQuestionBody(mainquestionbody)
-            questionUnitList=questiondata["defaultQuestion"]["masterVersion"]["questionUnitList"]
-            current_subjective_number=1
+                # 套题处理
+                mainquestionbody = questiondata["defaultQuestion"]["masterVersion"]["body"]
+                mainbodyblocklist = splitQuestionBody(mainquestionbody)
+            questionUnitList = questiondata["defaultQuestion"]["masterVersion"]["questionUnitList"]
+            current_subjective_number = 1
             for questionUnit in questionUnitList:
-                sub_ques_body=questionUnit["body"]
-                if questionUnit["questionType"]=="FILL_UP" or questionUnit["questionType"]=="ESSAY":
+                sub_ques_body = questionUnit["body"]
+                if questionUnit["questionType"] == "FILL_UP" or questionUnit["questionType"] == "ESSAY":
                     if current_subjective_number == question_unit_number:
                         questionBean = {
                             "mainNumber": main_number,
@@ -142,16 +145,21 @@ def querySubjectInfo(examRecordDataId,paperType,courseCode):
                         }
                         quesbodyblocklist = splitQuestionBody(sub_ques_body)
                         if mainbodyblocklist:
-                           questionBean["body"]["sections"] = mainbodyblocklist + quesbodyblocklist
+                            questionBean["body"]["sections"] = mainbodyblocklist + quesbodyblocklist
                         else:
                             questionBean["body"]["sections"] = quesbodyblocklist
-                        questionBean["studentAnswer"]["sections"]=splitQuesitonStudentAnswer(studentanswer,questionUnit["answerType"],questionUnit["questionType"])
-                        questionBean["answer"]["sections"]=splitQuesitonAnswer(questionUnit["rightAnswer"][0])
+                        questionBean["studentAnswer"]["sections"] = splitQuesitonStudentAnswer(studentanswer,
+                                                                                               questionUnit[
+                                                                                                   "answerType"],
+                                                                                               questionUnit[
+                                                                                                   "questionType"])
+                        questionBean["answer"]["sections"] = splitQuesitonAnswer(questionUnit["rightAnswer"][0])
                         questionBeanList.append(questionBean)
-                    current_subjective_number=current_subjective_number+1
+                    current_subjective_number = current_subjective_number + 1
     return questionBeanList
 
-def splittagElement(blocklist,childrenelement):
+
+def splittagElement(blocklist, childrenelement):
     if type(childrenelement) == element.NavigableString:
         block = {
             "type": "text",
@@ -174,15 +182,15 @@ def splittagElement(blocklist,childrenelement):
                 }
                 blocklist.append(block)
             elif child.name == "img":
-                width=None
-                height=None
+                width = None
+                height = None
                 if "width" in child.attrs and "height" in child.attrs:
-                    width=child.attrs["width"]
-                    height=child.attrs["height"]
+                    width = child.attrs["width"]
+                    height = child.attrs["height"]
                 else:
-                    style=child.attrs["style"]
-                    width=re.search("width:([^;]*)",style).group(1)
-                    height=re.search("height:([^;]*)",style).group(1)
+                    style = child.attrs["style"]
+                    width = re.search("width:([^;]*)", style).group(1)
+                    height = re.search("height:([^;]*)", style).group(1)
                 block = {
                     "type": "image",
                     "value": child.attrs["src"],
@@ -194,33 +202,32 @@ def splittagElement(blocklist,childrenelement):
                 }
                 blocklist.append(block)
             else:
-                splittagElement(blocklist,child)
-
-
+                splittagElement(blocklist, child)
 
 
 def splitQuestionBody(questionBody):
     """
       切分quesitonBody
     """
-    sectionlist=[]
-    bodyhtml=BeautifulSoup(questionBody,features="html.parser")
-    plist=bodyhtml.find_all("p")
+    sectionlist = []
+    bodyhtml = BeautifulSoup(questionBody, features="html.parser")
+    plist = bodyhtml.find_all("p")
     for p in plist:
-        #遍历p标签
-        section={
-            "blocks":[]
+        # 遍历p标签
+        section = {
+            "blocks": []
         }
-        splittagElement(section["blocks"],p)
+        splittagElement(section["blocks"], p)
         sectionlist.append(section)
     return sectionlist
 
+
 def splitQuesitonAnswer(quesitonanswer):
     """
      切分问题标答
     """
     sectionlist = []
-    #print(quesitonanswer)
+    # print(quesitonanswer)
     if quesitonanswer:
         bodyhtml = BeautifulSoup(quesitonanswer, features="html.parser")
         plist = bodyhtml.find_all("p")
@@ -232,32 +239,33 @@ def splitQuesitonAnswer(quesitonanswer):
             splittagElement(section["blocks"], p)
             sectionlist.append(section)
     else:
-        sectionlist=None
+        sectionlist = None
 
     return sectionlist
 
-def splitQuesitonStudentAnswer(studentanswer,answerType,questionType):
+
+def splitQuesitonStudentAnswer(studentanswer, answerType, questionType):
     """
     切分学生答案
     """
-    print(answerType,questionType)
+    print(answerType, questionType)
     if studentanswer:
         sectionlist = []
-        if questionType=="ESSAY":
-            if answerType=="SINGLE_AUDIO":
-                answer={
-                    "blocks":[{
-                    "type": "audio",
-                    "value": studentanswer,
-                    "playTime": None,
-                    "param": None
-                }]}
+        if questionType == "ESSAY":
+            if answerType == "SINGLE_AUDIO":
+                answer = {
+                    "blocks": [{
+                        "type": "audio",
+                        "value": studentanswer,
+                        "playTime": None,
+                        "param": None
+                    }]}
                 sectionlist.append(answer)
             else:
-                #取图片
+                # 取图片
                 bodyhtml = BeautifulSoup(studentanswer, features="html.parser")
-                blocks={
-                    "blocks":[]
+                blocks = {
+                    "blocks": []
                 }
                 for body in bodyhtml:
                     if type(body) == element.NavigableString:
@@ -270,16 +278,16 @@ def splitQuesitonStudentAnswer(studentanswer,answerType,questionType):
                         blocks["blocks"].append(textanswer)
                     else:
                         alist = body.find_all("a")
-                        if len(alist)>0:
+                        if len(alist) > 0:
                             for atag in alist:
-                                imageanswer={
+                                imageanswer = {
                                     "type": "image",
                                     "value": atag.attrs["href"],
                                     "playTime": None,
                                     "param": {
                                         "width": 200,
                                         "height": 200
-                                     }
+                                    }
                                 }
                                 blocks["blocks"].append(imageanswer)
                         else:
@@ -294,12 +302,12 @@ def splitQuesitonStudentAnswer(studentanswer,answerType,questionType):
 
         else:
             answer = {
-                "blocks":[{
-                "type": "text",
-                "value": studentanswer,
-                "playTime": None,
-                "param": None
-            }]
+                "blocks": [{
+                    "type": "text",
+                    "value": studentanswer,
+                    "playTime": None,
+                    "param": None
+                }]
             }
             sectionlist.append(answer)
         print(sectionlist)
@@ -308,7 +316,8 @@ def splitQuesitonStudentAnswer(studentanswer,answerType,questionType):
         sectionlist = None
     return sectionlist
 
-def getQuestion(questionId,courseCode,paperType):
+
+def getQuestion(questionId, courseCode, paperType):
     url = config.questiondomain + "api/core/questions/extract_config/getQuestion"
     headers = createRpcheader("3", "Q")
     data = {
@@ -320,22 +329,23 @@ def getQuestion(questionId,courseCode,paperType):
     result = requests.post(
         url, headers=headers,
         json=data)
-    if result.status_code==200:
+    if result.status_code == 200:
         return result.json()
 
 
 def getCourse(courseId):
-    url=config.basedomain+"api/core/basic/course/getCoursesByIdList"
-    headers =createRpcheader("1","B")
-    data={
-        "courseIdList":[courseId]
+    url = config.basedomain + "api/core/basic/course/getCoursesByIdList"
+    headers = createRpcheader("1", "B")
+    data = {
+        "courseIdList": [courseId]
     }
     result = requests.post(
-        url, headers=headers,json=data)
-    if result.status_code==200:
+        url, headers=headers, json=data)
+    if result.status_code == 200:
         return result.json()["courseList"][0]
 
-def createRpcheader(appid,appcode):
+
+def createRpcheader(appid, appcode):
     secretkey = "123456"
     t = time.time()
     secretstr = ''.join([appid, appcode, str(int(round(t * 1000))), secretkey])
@@ -349,5 +359,6 @@ def createRpcheader(appid,appcode):
     }
     return headers
 
-if __name__=="__main__":
-    main()
+
+if __name__ == "__main__":
+    main()