Gofor5 commited on
Commit
9d63250
·
verified ·
1 Parent(s): 764d5b2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -18
app.py CHANGED
@@ -1,18 +1,12 @@
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
  import tempfile
13
 
14
- lock = threading.Lock()
15
- event = threading.Event()
16
  requests.packages.urllib3.disable_warnings()
17
 
18
  reference_subject = {
@@ -143,13 +137,12 @@ def to_file(file, source_type, name=""):
143
  result["type"] = source_type
144
  return result
145
 
146
- # 修改点1:添加uid参数
147
  def analyze_homework(homework, include_text, uid):
148
  hwId = homework["hwId"]
149
  hwType = homework["hwType"]
150
  stuHwId = homework["stuHwId"]
151
  file_list = []
152
- data = {"base": {"appId": "APP"}, "params": {"hwId": hwId, "stuHwId": stuHwId, "studentId": uid}} # 使用传入的uid值
153
 
154
  if hwType == 102:
155
  response = post("https://mhw.zhixue.com/hwreport/question/getStuReportDetail", data)
@@ -240,7 +233,6 @@ def query_homework(uid, tlsysSessionId, subject, status, max_count):
240
  homework_list = homework_list_temp
241
  return token, gr.update(choices=homework_list, value=[])
242
 
243
- # 修改点2:添加uid参数
244
  def parse_homework(token, include_text, homework_selection, uid):
245
  result = []
246
  for i in homework_selection:
@@ -250,8 +242,7 @@ def parse_homework(token, include_text, homework_selection, uid):
250
  selected_homework = result
251
  file_list = []
252
  for i in tqdm(selected_homework, unit=""):
253
- # 修改点3:传递uid参数
254
- file_list += analyze_homework(homework_list_oringin[i], include_text, uid)
255
  global file_list_output
256
  file_list_output = [file["name"] for file in file_list]
257
  global homework_downloaded_path
@@ -259,7 +250,8 @@ def parse_homework(token, include_text, homework_selection, uid):
259
  global homework_is_text
260
  homework_is_text = [file["is_text"] for file in file_list]
261
  return gr.update(choices=file_list_output, value=[])
262
- def download_file(token, homework_view):
 
263
  download_list = []
264
  for i in homework_view:
265
  for j in range(len(file_list_output)):
@@ -273,12 +265,37 @@ def download_file(token, homework_view):
273
  download_list.append(temp_path)
274
  return download_list
275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
276
  with gr.Blocks(title="智学网作业获取器") as demo:
277
  gr.Markdown("# 🚀 智学网作业获取器")
278
  gr.Markdown("## Backfront Created by Levrium,UI Design by Start_ten")
279
  gr.Markdown("操作说明请见https://zhuanlan.zhihu.com/p/691808543")
 
 
 
 
 
 
 
 
 
 
 
280
  with gr.Row():
281
-
282
  with gr.Column():
283
  gr.Markdown("### 📝 查询作业")
284
  uid = gr.Textbox(
@@ -359,20 +376,32 @@ with gr.Blocks(title="智学网作业获取器") as demo:
359
  interactive=True
360
  )
361
  submit_btn = gr.Button("解析作业", variant="primary")
362
- # 修改点4:添加uid输入
363
  submit_btn.click(
364
  fn=parse_homework,
365
- inputs=[token, text_parse, homework_selection, uid], # 添加uid输入
366
- outputs=[homework_view, ]
367
  )
368
  with gr.Column():
369
  file_output = gr.File(label="作业文件", interactive=False)
370
  download_btn = gr.Button("下载作业", variant="primary")
371
  download_btn.click(
372
  fn=download_file,
373
- inputs=[token, homework_view],
374
  outputs=[file_output]
375
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
376
 
377
  if __name__ == "__main__":
378
  demo.launch()
 
 
 
 
1
  import re
2
  import threading
3
  import time
4
+ from pathlib import Path
 
5
  import gradio as gr
6
  import requests
7
  from tqdm import tqdm
8
  import tempfile
9
 
 
 
10
  requests.packages.urllib3.disable_warnings()
11
 
12
  reference_subject = {
 
137
  result["type"] = source_type
138
  return result
139
 
 
140
  def analyze_homework(homework, include_text, uid):
141
  hwId = homework["hwId"]
142
  hwType = homework["hwType"]
143
  stuHwId = homework["stuHwId"]
144
  file_list = []
145
+ data = {"base": {"appId": "APP"}, "params": {"hwId": hwId, "stuHwId": stuHwId, "studentId": uid}}
146
 
147
  if hwType == 102:
148
  response = post("https://mhw.zhixue.com/hwreport/question/getStuReportDetail", data)
 
233
  homework_list = homework_list_temp
234
  return token, gr.update(choices=homework_list, value=[])
235
 
 
236
  def parse_homework(token, include_text, homework_selection, uid):
237
  result = []
238
  for i in homework_selection:
 
242
  selected_homework = result
243
  file_list = []
244
  for i in tqdm(selected_homework, unit=""):
245
+ file_list += analyze_homework(homework_list_oringin[i], include_text, uid)
 
246
  global file_list_output
247
  file_list_output = [file["name"] for file in file_list]
248
  global homework_downloaded_path
 
250
  global homework_is_text
251
  homework_is_text = [file["is_text"] for file in file_list]
252
  return gr.update(choices=file_list_output, value=[])
253
+
254
+ def download_file(homework_view):
255
  download_list = []
256
  for i in homework_view:
257
  for j in range(len(file_list_output)):
 
265
  download_list.append(temp_path)
266
  return download_list
267
 
268
+ def save_config(uid, tlsysSessionId):
269
+ config_content = f"uid={uid}\ntlsysSessionId={tlsysSessionId}"
270
+ with tempfile.NamedTemporaryFile(mode="w", delete=False, suffix=".txt") as f:
271
+ f.write(config_content)
272
+ return f.name
273
+
274
+ def load_config(file):
275
+ if not file:
276
+ return None, None
277
+ with open(file.name, "r") as f:
278
+ content = f.read()
279
+ uid = re.search(r"uid=(\S+)", content)
280
+ tlsysSessionId = re.search(r"tlsysSessionId=(\S+)", content)
281
+ return (uid.group(1) if uid else None, (tlsysSessionId.group(1) if tlsysSessionId else None))
282
+
283
  with gr.Blocks(title="智学网作业获取器") as demo:
284
  gr.Markdown("# 🚀 智学网作业获取器")
285
  gr.Markdown("## Backfront Created by Levrium,UI Design by Start_ten")
286
  gr.Markdown("操作说明请见https://zhuanlan.zhihu.com/p/691808543")
287
+
288
+ # 新增配置文件区域
289
+ with gr.Row():
290
+ with gr.Column(scale=2):
291
+ config_upload = gr.File(label="上传配置文件", type="filepath", file_types=[".txt"])
292
+ with gr.Column(scale=1):
293
+ load_config_btn = gr.Button("识别配置文件", variant="secondary")
294
+ save_config_btn = gr.Button("导出配置文件", variant="secondary")
295
+ with gr.Column(scale=2):
296
+ config_download = gr.File(label="下载配置文件", interactive=False)
297
+
298
  with gr.Row():
 
299
  with gr.Column():
300
  gr.Markdown("### 📝 查询作业")
301
  uid = gr.Textbox(
 
376
  interactive=True
377
  )
378
  submit_btn = gr.Button("解析作业", variant="primary")
 
379
  submit_btn.click(
380
  fn=parse_homework,
381
+ inputs=[token, text_parse, homework_selection, uid],
382
+ outputs=[homework_view]
383
  )
384
  with gr.Column():
385
  file_output = gr.File(label="作业文件", interactive=False)
386
  download_btn = gr.Button("下载作业", variant="primary")
387
  download_btn.click(
388
  fn=download_file,
389
+ inputs=[homework_view],
390
  outputs=[file_output]
391
  )
392
+
393
+ # 配置文件功能绑定
394
+ load_config_btn.click(
395
+ fn=load_config,
396
+ inputs=[config_upload],
397
+ outputs=[uid, tlsysSessionId]
398
+ )
399
+
400
+ save_config_btn.click(
401
+ fn=save_config,
402
+ inputs=[uid, tlsysSessionId],
403
+ outputs=[config_download]
404
+ )
405
 
406
  if __name__ == "__main__":
407
  demo.launch()