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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import os
|
3 |
+
from pathlib import Path
|
4 |
+
import re
|
5 |
+
import threading
|
6 |
+
import time
|
7 |
+
from tkinter import filedialog
|
8 |
+
import traceback
|
9 |
+
import gradio as gr
|
10 |
+
import requests
|
11 |
+
from tqdm import tqdm
|
12 |
+
|
13 |
+
lock = threading.Lock()
|
14 |
+
event = threading.Event()
|
15 |
+
requests.packages.urllib3.disable_warnings()
|
16 |
+
|
17 |
+
reference_subject = {
|
18 |
+
'语文': '01',
|
19 |
+
'历史': '12',
|
20 |
+
'数学': '02',
|
21 |
+
'生物': '13',
|
22 |
+
'英语': '03',
|
23 |
+
'通用技术': '102',
|
24 |
+
'信息技术': '26',
|
25 |
+
'物理': '05',
|
26 |
+
'政治': '27',
|
27 |
+
'化学': '06',
|
28 |
+
'地理':"14"
|
29 |
+
}
|
30 |
+
subject_codes={
|
31 |
+
'01': '语文',
|
32 |
+
'12': '历史',
|
33 |
+
'02': '数学',
|
34 |
+
'13': '生物',
|
35 |
+
'03': '英语',
|
36 |
+
'102': '通用技术',
|
37 |
+
'26': '信息技术',
|
38 |
+
'05': '物理',
|
39 |
+
'27': '政治',
|
40 |
+
'06': '化学',
|
41 |
+
'14':'地理'
|
42 |
+
}
|
43 |
+
headers = [
|
44 |
+
{
|
45 |
+
"Accept": "application/json, text/plain, */*",
|
46 |
+
"Accept-Encoding": "gzip, deflate, br, zstd",
|
47 |
+
"Accept-Language": "zh-CN,zh;q=0.9",
|
48 |
+
"Connection": "keep-alive",
|
49 |
+
"Host": "www.zhixue.com",
|
50 |
+
"Referer": "https://www.zhixue.com/middlehomework/web-student/views/",
|
51 |
+
"Sec-Fetch-Dest": "empty",
|
52 |
+
"Sec-Fetch-Mode": "cors",
|
53 |
+
"Sec-Fetch-Site": "same-origin",
|
54 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
|
55 |
+
"appName": "com.iflytek.zxzy.web.zx.stu",
|
56 |
+
"sec-ch-ua": '"Not A(Brand";v="8", "Chromium";v="132", "Google Chrome";v="132"',
|
57 |
+
"sec-ch-ua-mobile": "?0",
|
58 |
+
"sec-ch-ua-platform": '"Windows"'
|
59 |
+
},
|
60 |
+
{
|
61 |
+
"Host": "www.zhixue.com",
|
62 |
+
"sucOriginAppKey": "zhixue_student",
|
63 |
+
"User-Agent": "zhixue_student/1.0.2026 (iPhone; iOS 16.2; Scale/3.00)",
|
64 |
+
"appName": "com.zhixue.student",
|
65 |
+
"Connection": "keep-alive",
|
66 |
+
"Accept-Language": "zh-Hans-CN;q=1, zh-Hant-CN;q=0.9, en-CN;q=0.8",
|
67 |
+
"Accept": "*/*",
|
68 |
+
"Accept-Encoding": "gzip, deflate, br"
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"Host": "mhw.zhixue.com",
|
72 |
+
"Content-Type": "application/json",
|
73 |
+
"Accept": "application/json, text/plain, */*",
|
74 |
+
"appName": "com.zhixue.student",
|
75 |
+
"sucOriginAppKey": "zhixue_student",
|
76 |
+
"Accept-Language": "zh-CN,zh-Hans;q=0.9",
|
77 |
+
"Origin": "https://mhw.zhixue.com",
|
78 |
+
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 16_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko)",
|
79 |
+
"Referer": "https://mhw.zhixue.com/zhixuestudent/views/homeworkReport/homework-report.html",
|
80 |
+
"Accept-Encoding": "gzip, deflate, br",
|
81 |
+
"Connection": "keep-alive"
|
82 |
+
}
|
83 |
+
]
|
84 |
+
|
85 |
+
def get_token():
|
86 |
+
global token
|
87 |
+
response = requests.get("https://www.zhixue.com/middleweb/newToken", headers=headers[0], verify=False)
|
88 |
+
response.encoding = "utf-8"
|
89 |
+
response = response.json()
|
90 |
+
result = response["result"]["token"]
|
91 |
+
if result:
|
92 |
+
token = result
|
93 |
+
return True
|
94 |
+
else:
|
95 |
+
print("获取 token 失败。")
|
96 |
+
return False
|
97 |
+
|
98 |
+
def format_time(timestamp):
|
99 |
+
return time.strftime("%Y-%m-%d %H:%M", time.localtime(timestamp // 1000))
|
100 |
+
|
101 |
+
def get(url):
|
102 |
+
headers[1].update({"Host": url.split("/")[2], "sucUserToken": token})
|
103 |
+
response = requests.get(url, headers=headers[1], verify=False)
|
104 |
+
response.encoding = "utf-8"
|
105 |
+
return response.json()
|
106 |
+
|
107 |
+
def post(url, data):
|
108 |
+
headers[2].update({
|
109 |
+
"Host": url.split("/")[2],
|
110 |
+
"Origin": f'https://{url.split("/")[2]}',
|
111 |
+
"sucUserToken": token,
|
112 |
+
"Authorization": token
|
113 |
+
})
|
114 |
+
response = requests.post(url, headers=headers[2], json=data, verify=False)
|
115 |
+
response.encoding = "utf-8"
|
116 |
+
return response.json()
|
117 |
+
|
118 |
+
def parse_range(s, max_value):
|
119 |
+
result = []
|
120 |
+
for item in s.split():
|
121 |
+
if "-" in item:
|
122 |
+
l = item.split("-")
|
123 |
+
if len(l) == 2 and l[0].isdigit() and l[1].isdigit():
|
124 |
+
begin = int(l[0]) - 1
|
125 |
+
end = int(l[1]) - 1
|
126 |
+
if not (begin < 0 and end < 0 or begin >= max_value and end >= max_value):
|
127 |
+
step = -1 if begin > end else 1
|
128 |
+
for i in range(begin, end + step, step):
|
129 |
+
if 0 <= i < max_value and not i in result:
|
130 |
+
result.append(i)
|
131 |
+
elif item.isdigit():
|
132 |
+
n = int(item) - 1
|
133 |
+
if 0 <= n < max_value and not n in result:
|
134 |
+
result.append(n)
|
135 |
+
return result
|
136 |
+
|
137 |
+
def to_file(file, source_type, name=""):
|
138 |
+
result = ({"name": name or Path(file).name, "path": file, "is_text": bool(name)} if isinstance(file, str) else
|
139 |
+
{"name": name, "path": file["description"], "is_text": True} if file["fileType"] == 5 else
|
140 |
+
{"name": file.get("name", "") or Path(file["path"]).name, "path": file["path"], "is_text": False})
|
141 |
+
result["name"] = re.sub('[\\\\/:*?"<>|]', "_", result["name"])
|
142 |
+
result["type"] = source_type
|
143 |
+
return result
|
144 |
+
|
145 |
+
# 修改点1:添加uid参数
|
146 |
+
def analyze_homework(homework, include_text, uid):
|
147 |
+
hwId = homework["hwId"]
|
148 |
+
hwType = homework["hwType"]
|
149 |
+
stuHwId = homework["stuHwId"]
|
150 |
+
file_list = []
|
151 |
+
data = {"base": {"appId": "APP"}, "params": {"hwId": hwId, "stuHwId": stuHwId, "studentId": uid}} # 使用传入的uid值
|
152 |
+
|
153 |
+
if hwType == 102:
|
154 |
+
response = post("https://mhw.zhixue.com/hwreport/question/getStuReportDetail", data)
|
155 |
+
if "result" in response:
|
156 |
+
result = response["result"]
|
157 |
+
file_list.append(to_file(result["hwDescription"], "题目", result["hwTitle"] + "_说明.txt"))
|
158 |
+
for problem in result["mainTopics"]:
|
159 |
+
content = problem["content"] + problem["answerHtml"] + problem["analysisHtml"]
|
160 |
+
file_list += [to_file(path, "题目") for path in re.findall('bigger="(.+?)"', content)]
|
161 |
+
file_list += [to_file(path, "提交") for item in problem["subTopics"] for path in item["answerResList"]]
|
162 |
+
|
163 |
+
elif hwType == 105:
|
164 |
+
response = post("https://mhw.zhixue.com/hw/homework/attachment/list", data)
|
165 |
+
file_list += [to_file(item, "题目") for item in response["result"]]
|
166 |
+
response = post("https://mhw.zhixue.com/hwreport/question/getStuReportDetail", data)
|
167 |
+
if "result" in response:
|
168 |
+
result = response["result"]
|
169 |
+
file_list.append(to_file(result["hwDescription"], "题目", result["hwTitle"] + "_说明.txt"))
|
170 |
+
file_list += [to_file(item, "答案") for item in result.get("answerAttachList", [])]
|
171 |
+
for problem in result["mainTopics"]:
|
172 |
+
file_list += [to_file(path, "提交") for item in problem["subTopics"] for path in item["answerResList"]]
|
173 |
+
|
174 |
+
elif hwType == 107:
|
175 |
+
response = post("https://mhw.zhixue.com/hw/clock/answer/getClockHomeworkDetail", data)
|
176 |
+
result = response["result"]
|
177 |
+
file_list.append(to_file(result["description"], "题目", result["title"] + "_说明.txt"))
|
178 |
+
file_list += ([to_file(item, "题目") for item in result.get("hwTopicAttachments", [])]
|
179 |
+
+ [to_file(item, "答案") for item in result.get("hwAnswerAttachments", [])]
|
180 |
+
+ [to_file(item, "答案") for item in
|
181 |
+
result["hwClockRecordPreviewResponses"][0].get("teacherAnswerAttachments", [])]
|
182 |
+
+ [to_file(item, "提交", result["title"] + "_提交.txt") for item in
|
183 |
+
result["hwClockRecordPreviewResponses"][0].get("answerAttachments", [])])
|
184 |
+
|
185 |
+
file_list = [file for file in file_list if file["path"] and (include_text or not file["is_text"])]
|
186 |
+
return file_list
|
187 |
+
|
188 |
+
def query_homework(uid, tlsysSessionId, subject, status, max_count):
|
189 |
+
headers[0].update({"Cookie": f"tlsysSessionId={tlsysSessionId}"})
|
190 |
+
successful = get_token()
|
191 |
+
if not successful:
|
192 |
+
return "","","获取 token 失败"
|
193 |
+
page_size = max_count
|
194 |
+
if subject == ["语文", "数学", "英语", "物理", "化学", "生物", "地理", "历史", "政治",'通用技术', '信息技术']:
|
195 |
+
subjects = ["-1"]
|
196 |
+
else:
|
197 |
+
subjects = [reference_subject[i] for i in subject]
|
198 |
+
if status == "全部":
|
199 |
+
status = ""
|
200 |
+
elif status == "已完成":
|
201 |
+
status = "1"
|
202 |
+
else:
|
203 |
+
status = "0"
|
204 |
+
fetch_list = []
|
205 |
+
if status != "1":
|
206 |
+
fetch_list += [{"subject": subject, "status": 0} for subject in subjects]
|
207 |
+
if status != "0":
|
208 |
+
fetch_list += [{"subject": subject, "status": 1} for subject in subjects]
|
209 |
+
global homework_list
|
210 |
+
homework_list = []
|
211 |
+
timestamps = [int(time.time() * 1000)] * len(fetch_list)
|
212 |
+
finished = [False] * len(fetch_list)
|
213 |
+
while not all(finished):
|
214 |
+
print("\x9B1F\x9B0J", end="")
|
215 |
+
index = len(homework_list)
|
216 |
+
for i in tqdm(range(len(fetch_list)), unit=""):
|
217 |
+
if finished[i]:
|
218 |
+
continue
|
219 |
+
response = get("https://mhw.zhixue.com/homework_middle_service/stuapp/getStudentHomeWorkList"
|
220 |
+
f'?completeStatus={fetch_list[i]["status"]}&createTime={timestamps[i]}&pageIndex=2'
|
221 |
+
f'&pageSize={page_size}&subjectCode={fetch_list[i]["subject"]}&token={token}')
|
222 |
+
if response["code"] != 200:
|
223 |
+
raise RuntimeError("获取作业列表失败")
|
224 |
+
result_list = response["result"]["list"]
|
225 |
+
homework_list += result_list
|
226 |
+
if len(result_list) < page_size:
|
227 |
+
finished[i] = True
|
228 |
+
if result_list:
|
229 |
+
timestamps[i] = result_list[-1]["beginTime"]
|
230 |
+
|
231 |
+
print("\x9B1F\x9B0J", end="")
|
232 |
+
homework_list_temp=[]
|
233 |
+
global homework_list_oringin
|
234 |
+
homework_list_oringin = homework_list
|
235 |
+
for i in range(index, len(homework_list)):
|
236 |
+
begin_time = format_time(homework_list[i]["beginTime"])
|
237 |
+
end_time = format_time(homework_list[i]["endTime"])
|
238 |
+
homework_list_temp.append(f"[{homework_list[i]['subjectName']}]|{homework_list[i]['hwTitle']}|{begin_time}-{end_time}")
|
239 |
+
homework_list = homework_list_temp
|
240 |
+
return token, gr.update(choices=homework_list, value=[])
|
241 |
+
|
242 |
+
# 修改点2:添加uid参数
|
243 |
+
def parse_homework(token, include_text, homework_selection, uid):
|
244 |
+
result = []
|
245 |
+
for i in homework_selection:
|
246 |
+
for j in range(len(homework_list)):
|
247 |
+
if i == homework_list[j]:
|
248 |
+
result.append(j)
|
249 |
+
selected_homework = result
|
250 |
+
file_list = []
|
251 |
+
for i in tqdm(selected_homework, unit=""):
|
252 |
+
# 修改点3:传递uid参数
|
253 |
+
file_list += analyze_homework(homework_list_oringin[i], include_text, uid)
|
254 |
+
global file_list_output
|
255 |
+
file_list_output = [file["name"] for file in file_list]
|
256 |
+
global homework_downloaded_path
|
257 |
+
homework_downloaded_path = [file["path"] for file in file_list]
|
258 |
+
return gr.update(choices=file_list_output, value=[])
|
259 |
+
def download_file(token, homework_view):
|
260 |
+
download_list = []
|
261 |
+
for i in homework_view:
|
262 |
+
for j in range(len(file_list_output)):
|
263 |
+
if i == file_list_output[j]:
|
264 |
+
download_list.append(homework_downloaded_path[j])
|
265 |
+
return download_list
|
266 |
+
|
267 |
+
with gr.Blocks(title="智学网作业获取器") as demo:
|
268 |
+
gr.Markdown("# 🚀 智学网作业获取器")
|
269 |
+
gr.Markdown("## Backfront Created by Levrium,UI Design by Start_ten")
|
270 |
+
gr.Markdown("操作说明请见https://zhuanlan.zhihu.com/p/691808543")
|
271 |
+
with gr.Row():
|
272 |
+
|
273 |
+
with gr.Column():
|
274 |
+
gr.Markdown("### 📝 查询作业")
|
275 |
+
uid = gr.Textbox(
|
276 |
+
label="uid",
|
277 |
+
placeholder="请输入uid...",
|
278 |
+
)
|
279 |
+
tlsysSessionId = gr.Textbox(
|
280 |
+
label="tlsysSessionId",
|
281 |
+
placeholder="请输入tlsysSessionId...",
|
282 |
+
)
|
283 |
+
subject = gr.CheckboxGroup(
|
284 |
+
choices=["语文", "数学", "英语", "物理", "化学", "生物", "地理", "历史", "政治",'通用技术', '信息技术'],
|
285 |
+
label="具体学科",
|
286 |
+
value=["语文", "数学", "英语", "物理", "化学", "生物", "地理", "历史", "政治",'通用技术', '信息技术']
|
287 |
+
)
|
288 |
+
status = gr.Radio(
|
289 |
+
choices=["全部", "已完成", "未完成"],
|
290 |
+
label="作业状态",
|
291 |
+
value="全部"
|
292 |
+
)
|
293 |
+
max_count = gr.Slider(
|
294 |
+
label="最大请求作业数",
|
295 |
+
value=10,
|
296 |
+
minimum=1,
|
297 |
+
maximum=50,
|
298 |
+
step=1
|
299 |
+
)
|
300 |
+
with gr.Column():
|
301 |
+
token = gr.Textbox(
|
302 |
+
label="TOKEN",
|
303 |
+
interactive=False
|
304 |
+
)
|
305 |
+
|
306 |
+
homework_selection = gr.CheckboxGroup(
|
307 |
+
label="作业列表(可多选)",
|
308 |
+
choices=[],
|
309 |
+
interactive=True
|
310 |
+
)
|
311 |
+
submit_btn = gr.Button("查询作业", variant="primary")
|
312 |
+
submit_btn.click(
|
313 |
+
fn=query_homework,
|
314 |
+
inputs=[uid, tlsysSessionId, subject, status, max_count],
|
315 |
+
outputs=[token, homework_selection ]
|
316 |
+
)
|
317 |
+
gr.Markdown("---")
|
318 |
+
with gr.Row():
|
319 |
+
with gr.Column():
|
320 |
+
gr.Markdown("### 📄 解析作业并下载")
|
321 |
+
text_parse = gr.Checkbox(
|
322 |
+
label="是否解析题目、提交的文本?",
|
323 |
+
value=False
|
324 |
+
)
|
325 |
+
homework_view = gr.CheckboxGroup(
|
326 |
+
label="作业内容",
|
327 |
+
choices=[],
|
328 |
+
interactive=True
|
329 |
+
)
|
330 |
+
submit_btn = gr.Button("解析作业", variant="primary")
|
331 |
+
# 修改点4:添加uid输入
|
332 |
+
submit_btn.click(
|
333 |
+
fn=parse_homework,
|
334 |
+
inputs=[token, text_parse, homework_selection, uid], # 添加uid输入
|
335 |
+
outputs=[homework_view, ]
|
336 |
+
)
|
337 |
+
with gr.Column():
|
338 |
+
file_output = gr.File(label="作业文件", interactive=False)
|
339 |
+
download_btn = gr.Button("下载作业", variant="primary")
|
340 |
+
download_btn.click(
|
341 |
+
fn=download_file,
|
342 |
+
inputs=[token, homework_view],
|
343 |
+
outputs=[file_output]
|
344 |
+
)
|
345 |
+
|
346 |
+
if __name__ == "__main__":
|
347 |
+
demo.launch(share=True)
|