66
77from config .config import model_choice
88from scripts .check_config import clear_config
9+ from client .ziantt import save_question
10+ from time import sleep
911
1012class QuizSession :
1113 def __init__ (self ):
1214 self .question_id = None
1315 self .answers = None
16+ self .question_json = None
1417 self .question_num = 0
1518 self .question = None
19+ self .current_score = 0
20+ self .category = None
21+ self .confirm = False
1622
1723 def start (self ):
1824 """开始答题会话"""
25+ print ("我们正在构建一个硬核会员题库,您是否愿意上传答题信息帮助我们构建题库?" )
26+ print ("此操作仅会上传题目和题目对应的答案信息,并不会上传您的其他信息" )
27+ confirm = input ('是否授权上传本次答题内容?[1]是 [2]否: ' )
28+ self .confirm = confirm == '1'
1929 try :
2030 while self .question_num < 100 :
2131 if not self .get_question ():
@@ -48,6 +58,13 @@ def start(self):
4858 if not self .submit_answer (result ):
4959 logger .error ("提交答案失败" )
5060 return
61+ score = question_result ().get ('score' );
62+ if self .current_score < score :
63+ logger .info ("回答正确, 当前得分:{}" .format (score ))
64+ self .current_score = score
65+ self .upload_question (answer )
66+ else :
67+ logger .info ("回答错误, 当前得分:{}" .format (score ))
5168 self .print_result ()
5269 except KeyboardInterrupt :
5370 logger .info ("答题会话已终止" )
@@ -69,6 +86,7 @@ def get_question(self):
6986 return self .handle_verification ()
7087
7188 data = question .get ('data' , {})
89+ self .question_json = data
7290 self .question = data .get ('question' )
7391 self .answers = data .get ('answers' , [])
7492 self .question_id = data .get ('id' )
@@ -96,6 +114,7 @@ def handle_verification(self):
96114 logger .info (f"ID: { cat .get ('id' )} - { cat .get ('name' )} " )
97115 logger .info ("tips: 输入多个分类ID请用 *英文逗号* 隔开,例如:1,2,3(最多三个分类)" )
98116 ids = input ('请输入分类ID: ' )
117+ self .category = ids
99118 logger .info ("获取验证码..." )
100119 captcha_res = captcha_get ()
101120 logger .info ("请打开链接查看验证码内容:{}" .format (captcha_res .get ('url' )))
@@ -109,7 +128,6 @@ def handle_verification(self):
109128 else :
110129 logger .error ("验证失败" )
111130 return False
112-
113131 except Exception as e :
114132 logger .error (f"验证过程发生错误: { str (e )} " )
115133 return False
@@ -180,6 +198,26 @@ def print_result(self):
180198 except Exception as e :
181199 logger .error (f"获取答题结果失败: { str (e )} " )
182200
201+ def upload_question (self , answer ):
202+ # 保存题目
203+ if self .confirm :
204+ try :
205+ question_submit_content = {
206+ 'qid' : self .question_json .get ('id' ),
207+ 'question' : self .question_json .get ('question' ),
208+ 'ans_1' : self .answers [0 ].get ('ans_text' ),
209+ 'ans_2' : self .answers [1 ].get ('ans_text' ),
210+ 'ans_3' : self .answers [2 ].get ('ans_text' ),
211+ 'ans_4' : self .answers [3 ].get ('ans_text' ),
212+ 'answer' : self .answers [answer - 1 ].get ('ans_text' ),
213+ 'source' : self .question_json .get ('source' ),
214+ 'author' : self .question_json .get ('author' ),
215+ 'category' : self .category and len (self .category ) == 1 and self .category [0 ] or None ,
216+ }
217+ save_question (question_submit_content )
218+ except Exception as e :
219+ logger .error (f"题库上传失败,此报错不影响正常答题: { str (e )} " )
220+
183221# 创建答题会话实例
184222quiz_session = QuizSession ()
185223
0 commit comments