-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
338 lines (309 loc) · 11.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import time
from api import *
INTRO_S = R"""
Yangcong-tools: A tool to Complete home work on Onion School.
Copyright (C) 2023 Imken Luo
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
This program is a part of Yangcong-tools.
"""
print(INTRO_S)
def bug_report(msg, **kwargs):
print("\n\n\n")
print("Oops! 程序出现了一些问题:\n\t%s" % msg)
print("正在准备生成日志文件和调试信息,请稍后。")
filename = "YCT-Error-%d.json.log" % time.time_ns()
logfile = open(filename, "w", encoding="utf-8")
logfile.write(json.dumps(kwargs))
logfile.close()
print("日志文件已生成,位于当前工作目录下的 %s" % filename)
print(
"请前往我们的GitHub仓库提出issue: https://github.com/immccn123/Yangcong-tools/issues/new/choose 。"
)
exit(3)
def complete_topic(homework_id, topic):
if topic["state"] != "unfinished":
return
topic_id = topic["id"]
print("\t获取话题", topic["name"], "的详细信息...", end="")
topic_detail = get_topic_detail(topic_id)
print(" 完成")
if topic["videoState"] == "unfinished":
print("\t\t正在完成视频...", end="")
submit_video(
topic_id,
homework_id,
topic_detail["video"]["id"],
int(topic_detail["video"]["duration"]),
)
print(" 完成")
if topic["practiceState"] != "unfinished":
return
if topic_detail["practices"] == None:
print("\t无习题")
return
problem_count = len(topic_detail["practices"])
done_count = 0
for pc in topic_detail["practices"]:
problem = pc[0]
problem_id = problem["problemId"]
problem_type = problem["type"]
if problem_type in ["multi_choice", "single_choice"]:
ans = []
for choice in problem["choices"][0]:
if choice["correct"] == True:
ans.append(choice["body"])
submit_video_problem(problem_id, homework_id, topic_id, ans)
elif problem_type in ["multi_blank", "single_blank", "hybrid"]:
submit_video_problem(problem_id, homework_id, topic_id, problem["blanks"])
elif problem_type in ["exam"]:
submit_video_problem(problem_id, homework_id, topic_id, ["我答对了", "答:"])
else:
bug_report(
"未知问题类型 10001",
break_point="complete_topic",
problem=problem,
topic_name=topic["name"],
ptype=problem_type,
)
done_count += 1
print(f"\t完成习题 ({done_count}/{problem_count})")
def complete_practice(homework_id, problem_list):
for index, problem in enumerate(problem_list):
ans = []
problem_type = problem["type"]
if problem_type in ["single_choice", "multi_choice"]:
for choice in problem["choices"][0]:
if choice["correct"] == True:
ans.append({"body": choice["body"], "no": 0})
elif problem_type in ["multi_blank", "single_blank", "hybrid"]:
for s in problem["blanks"]:
ans.append({"body": s, "no": 0})
elif problem_type in ["exam"]:
ans.append({"body": "我答对了", "no": 0})
ans.append({"body": "答:", "no": 0})
else:
bug_report(
"未知问题类型 10001",
break_point="complete_practice",
problem=problem,
ptype=problem_type,
)
submit_practice_problem(
homework_id,
[
{
"problemId": problem["id"],
"answer": ans,
"duration": randint(1, 12),
}
],
"finished" if index == len(problem_list) - 1 else "unfinished",
)
def complete_exam(group_id, problem_list):
problem_count = len(problem_list)
for index, problem in enumerate(problem_list):
ans = []
problem_type = problem["type"]
if problem_type in ["single_choice", "multi_choice"]:
for choice in problem["choices"][0]:
if choice["correct"] == True:
ans.append({"body": choice["body"], "no": 0})
elif problem_type in ["multi_blank", "single_blank", "hybrid"]:
for s in problem["blanks"]:
ans.append({"body": s, "no": 0})
elif problem_type in ["exam"]:
ans.append({"body": "我答对了", "no": 0})
ans.append({"body": "答:", "no": 0})
else:
bug_report(
"未知问题类型 10001",
break_point="complete_exam",
problem=problem,
ptype=problem_type,
)
commit_problem_progress(
group_id,
index == len(problem_list) - 1,
[
{
"problemId": problem["id"],
"answer": ans,
"type": problem_type,
"duration": randint(1, 120),
}
],
)
print(f"\t\t已完成 ({index + 1}/{problem_count})")
def complete_homework(hw):
homework_id = hw["id"]
if hw["state"] == 2:
return
if hw["type"] == 0:
print("[Video] 正在完成", hw["name"], homework_id)
topics: list = hw["topics"]
print("\t共", len(topics), "个话题")
for topic in topics:
complete_topic(homework_id, topic)
print("\t已完成", topic["name"], topic["id"])
elif hw["type"] == 1:
print("[Practice] 正在完成", hw["name"], homework_id)
print("\t正在获取试题列表...", end="")
problems = get_practice_problems(homework_id)
print(" 共", len(problems), "题")
complete_practice(homework_id, problems)
elif hw["type"] == 3:
print("[Exam] 正在完成", hw["name"], homework_id)
print("\t正在获取试题列表...", end="")
ctx = get_task_problem(homework_id)
problems = ctx["problems"]
group_id = ctx["groupDetailId"]
print(" 共", len(problems), "题")
complete_exam(group_id, problems)
print("已完成", hw["name"], homework_id)
def complete_vacation_task_video(t, tl):
topic = get_vacation_video_detail(t["topicId"], t["id"])
if t["videoState"] == 0:
print("\t正在完成视频...", end="")
submit_vacation_video(
topic["id"],
topic["video"]["id"],
t["id"],
int(topic["video"]["duration"]) + 1,
)
print(" 完成")
if t["problemState"] != 0:
return
ans = []
practices_count = len(topic["practices"])
for index, problem in enumerate(topic["practices"]):
problem = problem[0]
if problem["type"] in ["single_choice", "multi_choice"]:
for choice in problem["choices"][0]:
if choice["correct"] == True:
ans.append(choice["body"])
elif problem["type"] in ["multi_blank", "single_blank", "hybrid"]:
ans = problem["blanks"]
elif problem["type"] in ["exam"]:
ans = ["我答对了", "答:"]
else:
bug_report(
"未知问题类型 10001",
timeline_name=tl["name"],
break_point="complete_vacation",
problem=problem,
ptype=problem["type"],
)
submit_vacation_practice(
problem["problemId"],
topic["id"],
t["id"],
problem["pool"],
index == len(topic["practices"]) - 1,
ans,
)
print(f"\t完成练习 ({index + 1}/{practices_count})")
def complete_vacation_task_stage(t, vc, subject_id, stage_id):
problems = get_vacation_stage_problem(
t["id"],
stage_id,
subject_id,
vc["id"],
)
problem_details = []
problem_count = len(problems)
for index, problem in enumerate(problems):
problem_type = problem["type"]
ans = []
if problem_type in ["single_choice", "multi_choice"]:
for choice in problem["choices"][0]:
if choice["correct"] == True:
ans.append(choice["body"])
elif problem_type in ["multi_blank", "single_blank", "hybrid"]:
for s in problem["blanks"]:
ans.append(s)
elif problem["type"] in ["exam"]:
ans = ["我答对了", "答:"]
else:
bug_report(
"未知问题类型 10001",
break_point="vacation",
problem=problem,
ptype=problem_type,
)
problem_details.append(
submit_vacation_stage_problem(
task_id=t["id"],
stage_id=stage_id,
subject_id=subject_id,
homework_id=vc["id"],
problem_id=problem["id"],
problem_type=problem_type,
answer=ans,
is_complete=(index == len(problems) - 1),
)
)
if index == len(problems) - 1:
finalsubmit_vacation_stage_problem(t["id"], problem_details)
print(f"\t\t完成练习 ({index + 1}/{problem_count})")
def complete_vacation(vc):
print("正在完成", vc["name"])
vacation = get_vacation_details(vc["id"])
timelines = vacation["timeLines"]
stage_id = vacation["stageId"]
subject_id = vacation["subjectId"]
print("\t共", len(timelines), "个时间节点")
for tl in timelines:
if not (tl["state"] == 0 and tl["unlock"]):
continue
if len(tl["tasks"]) == 0:
continue
print("\t节点", tl["name"], "已解锁但未完成")
tasks = tl["tasks"]
for t in tasks:
if t["type"] == 1:
complete_vacation_task_video(t, tl)
elif t["type"] in (2, 3):
complete_vacation_task_stage(
t, vc, subject_id=subject_id, stage_id=stage_id
)
def run():
unfinished_homework = get_unfinished_homework()
expired_homework = get_expired_homework()
homework: list = (
unfinished_homework["homeworkList"] + expired_homework["homeworkList"]
)
vacations = get_vacations()
print("共", len(homework), "个作业")
for homework in homework:
complete_homework(homework)
print("共", len(vacations), "个假期课程")
for vacation in vacations:
complete_vacation(vacation)
if __name__ == "__main__":
userinfo = []
try:
with open("users.json", "r", encoding="utf-8") as f:
userinfo = json.load(f)
for user in userinfo:
if login(user["username"], user["password"]) is not None:
print("[Multi User] 正在处理用户", user["username"])
run()
else:
print("Warning:", user["username"], "的账号或密码错误!")
except FileNotFoundError:
un = input("用户名/手机号:")
pw = input("密码:")
login(un, pw)
run()
except json.decoder.JSONDecodeError as E:
print(E)
print("多用户配置文件格式有误。")