-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathui.py
More file actions
193 lines (157 loc) · 6.69 KB
/
Copy pathui.py
File metadata and controls
193 lines (157 loc) · 6.69 KB
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
import sys
import os
import json
import threading
import tkinter as tk
from tkinter import scrolledtext, messagebox, ttk
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
os.chdir(SCRIPT_DIR)
sys.path.insert(0, SCRIPT_DIR)
sys.stdout.reconfigure(encoding='utf-8', errors='replace')
class StdoutRedirector:
def __init__(self, text_widget):
self.text_widget = text_widget
def write(self, text):
if text.strip():
self.text_widget.insert(tk.END, text)
self.text_widget.see(tk.END)
self.text_widget.update_idletasks()
def flush(self):
pass
class App(tk.Tk):
def __init__(self):
super().__init__()
self.title("超星学习通刷课工具")
self.geometry("900x650")
self.minsize(800, 500)
self.accounts = self.load_accounts()
self.selected_account = None
self.setup_ui()
sys.stdout = StdoutRedirector(self.log_text)
def load_accounts(self):
path = os.path.join(SCRIPT_DIR, "accounts.json")
if not os.path.exists(path):
return []
with open(path, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("accounts", [])
def setup_ui(self):
top = tk.Frame(self)
top.pack(fill=tk.X, padx=10, pady=5)
tk.Label(top, text="超星学习通刷课工具", font=("Microsoft YaHei", 14, "bold")).pack(side=tk.LEFT)
main = tk.PanedWindow(self, orient=tk.HORIZONTAL)
main.pack(fill=tk.BOTH, expand=True, padx=10, pady=5)
left = tk.Frame(main, width=280)
main.add(left, width=280, minsize=200)
tk.Label(left, text="账号列表", font=("Microsoft YaHei", 11, "bold")).pack(anchor=tk.W, padx=5, pady=2)
self.accounts_frame = tk.Frame(left)
self.accounts_frame.pack(fill=tk.BOTH, expand=True, padx=5)
self.render_accounts()
right = tk.Frame(main)
main.add(right, width=600)
self.func_frame = tk.Frame(right)
self.func_frame.pack(fill=tk.X, padx=5, pady=2)
self.render_functions()
tk.Label(right, text="日志", font=("Microsoft YaHei", 11, "bold")).pack(anchor=tk.W, padx=5)
self.log_text = scrolledtext.ScrolledText(right, height=15, font=("Consolas", 10))
self.log_text.pack(fill=tk.BOTH, expand=True, padx=5, pady=2)
bottom = tk.Frame(self)
bottom.pack(fill=tk.X, padx=10, pady=5)
tk.Button(bottom, text="退出", command=self.destroy).pack(side=tk.RIGHT, padx=2)
def render_accounts(self):
for w in self.accounts_frame.winfo_children():
w.destroy()
if not self.accounts:
tk.Label(self.accounts_frame, text="请编辑 accounts.json 添加账号", fg="red").pack()
return
for acc in self.accounts:
is_sel = self.selected_account and acc["id"] == self.selected_account["id"]
bg = "#e3f2fd" if is_sel else "white"
frame = tk.Frame(self.accounts_frame, bg=bg, bd=1, relief="ridge", cursor="hand2")
frame.pack(fill=tk.X, pady=2)
frame.bind("<Button-1>", lambda e, a=acc: self.select_account(a))
name = acc.get("name", acc["username"])
tk.Label(frame, text=name, font=("Microsoft YaHei", 10, "bold"), bg=bg).pack(anchor=tk.W, padx=5, pady=1)
tk.Label(frame, text=acc["username"], font=("Microsoft YaHei", 9), bg=bg, fg="gray").pack(anchor=tk.W, padx=5)
def render_functions(self):
for w in self.func_frame.winfo_children():
w.destroy()
if not self.selected_account:
tk.Label(self.func_frame, text="请选择一个账号", font=("Microsoft YaHei", 10), fg="gray").pack()
return
acc = self.selected_account
tk.Label(self.func_frame, text=f"当前: {acc.get('name', '')} ({acc['username']})",
font=("Microsoft YaHei", 10, "bold")).pack(anchor=tk.W, padx=5, pady=2)
btn_frame = tk.Frame(self.func_frame)
btn_frame.pack(fill=tk.X, padx=5)
for text, cmd in [
("查看课程", self.view_courses),
("开始刷课", self.start_brush),
("签到监控", self.start_sign),
("导出题目", self.export_qs),
("爬取题库", self.crawl_tiku),
]:
tk.Button(btn_frame, text=text, command=cmd, width=12).pack(side=tk.LEFT, padx=2)
def select_account(self, account):
self.selected_account = account
self.render_accounts()
self.render_functions()
print(f"\n > 已选择: {account['name']} ({account['username']})\n")
def run_thread(self, target):
t = threading.Thread(target=target, daemon=True)
t.start()
def view_courses(self):
self.run_thread(self._view_courses)
def _view_courses(self):
try:
from main import init_chaoxing, load_config
config = load_config()
chaoxing, _ = init_chaoxing(self.selected_account, config)
state = chaoxing.login(login_with_cookies=bool(self.selected_account.get("cookies", "")))
if not state["status"]:
print(f"登录失败: {state['msg']}")
return
courses = chaoxing.get_course_list()
for i, c in enumerate(courses):
print(f" [{i+1}] {c['title']}")
print(f" 共 {len(courses)} 门课程")
except Exception as e:
print(f"错误: {e}")
def start_brush(self):
self.run_thread(self._start_brush)
def _start_brush(self):
try:
from main import brush_courses, load_config
brush_courses(self.selected_account, load_config())
print("刷课完成")
except Exception as e:
print(f"错误: {e}")
def start_sign(self):
self.run_thread(self._start_sign)
def _start_sign(self):
try:
from main import start_sign_monitor, load_config
start_sign_monitor(self.selected_account, load_config())
except Exception as e:
print(f"错误: {e}")
def export_qs(self):
self.run_thread(self._export_qs)
def _export_qs(self):
try:
from main import export_questions, load_config
export_questions(self.selected_account, load_config())
except Exception as e:
print(f"错误: {e}")
def crawl_tiku(self):
self.run_thread(self._crawl_tiku)
def _crawl_tiku(self):
try:
from main import crawl_tiku, load_config
crawl_tiku(self.selected_account, load_config())
except Exception as e:
print(f"错误: {e}")
def main():
app = App()
app.mainloop()
if __name__ == "__main__":
main()