forked from prcwcy/sjtu-canvas-video-download
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sjtu_canvas_video_downloader_frame.py
143 lines (123 loc) · 5.13 KB
/
sjtu_canvas_video_downloader_frame.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
import tkinter as tk
import tkinter.ttk
import tkinter.filedialog
import tkinter.messagebox
import re
from sjtu_canvas_video_download import download_courses
from sjtu_canvas_video_helper import create_window
def remove_invalid_chars(s):
return re.sub(
r"[<>:\"/\\|?*\s]", "_", s
)
def get_course_filename_ext(course_link):
right_index = course_link.find('?')
left_index = course_link.rfind('.', 0, right_index)
course_filename_ext = course_link[left_index:right_index]
return course_filename_ext
class DownloaderFrame(tk.Frame):
def __init__(self, selected_courses, master=None):
tk.Frame.__init__(self, master)
self.grid(sticky=tk.N+tk.S+tk.W+tk.E)
num_row = 0
entry_columnspan = 8
button_columnspan = 2
for i in range(1+entry_columnspan+button_columnspan):
self.columnconfigure(i, weight=1)
for i in selected_courses:
for j in i:
j["videoPlayResponseVoList"].sort(
key=lambda t: t["cdviViewNum"]
)
self.selected_courses = selected_courses
self.video_dirname_label = tk.Label(
self,
text="保存到..."
)
self.video_dirname_label.grid(
row=num_row, column=0
)
self.video_dirname_var = tk.StringVar()
self.video_dirname_entry = tk.Entry(
self,
textvariable=self.video_dirname_var
)
self.video_dirname_entry.grid(
row=num_row, column=1, columnspan=entry_columnspan, sticky=tk.W+tk.E
)
self.set_video_dirname_button = tk.Button(
self,
command=self.set_video_dirname,
text="浏览"
)
self.set_video_dirname_button.grid(
row=num_row, column=1+entry_columnspan, columnspan=button_columnspan, sticky=tk.W+tk.E
)
num_row += 1
self.partial_download_checkbutton_var = tk.IntVar()
self.partial_download_checkbutton = tkinter.ttk.Checkbutton(
self,
variable=self.partial_download_checkbutton_var,
text="只下载录像 (不下载录屏)"
)
self.partial_download_checkbutton.grid(
row=num_row, column=0, columnspan=1+entry_columnspan+button_columnspan
)
num_row += 1
self.preview_button = tk.Button(
self,
command=self.preview,
text="预览"
)
self.preview_button.grid(
row=num_row, column=1+entry_columnspan-button_columnspan, columnspan=button_columnspan, sticky=tk.W+tk.E
)
self.initiate_download_button = tk.Button(
self,
command=self.initiate_download,
text="下载"
)
self.initiate_download_button.grid(
row=num_row, column=1+entry_columnspan, columnspan=button_columnspan, sticky=tk.W+tk.E
)
num_row += 1
for i in range(num_row):
self.rowconfigure(i, weight=1)
def get_course_links_filenames(self):
partial_download = self.partial_download_checkbutton_var.get()
course_links = []
course_filenames = []
for subject in self.selected_courses:
for course in subject:
subject_name = remove_invalid_chars(course["subjName"])
teacher_name = remove_invalid_chars(course["userName"])
course_name = remove_invalid_chars(course["courName"])
course_filename_raw = f"{subject_name}_{teacher_name}_{course_name}"
course_dirname = f"{subject_name}_{teacher_name}"
for i, video in enumerate(course["videoPlayResponseVoList"]):
if (not partial_download) or (partial_download and video["cdviViewNum"] == 0):
course_link = video["rtmpUrlHdv"]
course_filename_ext = get_course_filename_ext(
course_link
)
if partial_download:
course_filename = f"{course_dirname}/{course_filename_raw}{course_filename_ext}"
else:
course_filename = f"{course_dirname}/{course_filename_raw}_{i}{course_filename_ext}"
course_links.append(course_link)
course_filenames.append(course_filename)
return course_links, course_filenames
def preview(self):
_, course_filenames = self.get_course_links_filenames()
window = create_window(self.master)
tk.Label(window, text='\n'.join(course_filenames)).pack()
def set_video_dirname(self):
video_dirname = tkinter.filedialog.askdirectory()
if video_dirname:
self.video_dirname_var.set(video_dirname)
def initiate_download(self):
video_dirname = self.video_dirname_var.get()
if not video_dirname:
tkinter.messagebox.showerror("错误", "请指定保存路径")
return
course_links, course_filenames = self.get_course_links_filenames()
download_courses(course_links, course_filenames, video_dirname)