This repository has been archived by the owner on Aug 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgui.py
170 lines (145 loc) · 7.74 KB
/
gui.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
'''
Project :
FilePath : \OPENSOURCE\gui.py
Descripttion :
Author : GDDG08
Date : 2024-04-03 12:53:30
LastEditors : GDDG08
LastEditTime : 2024-04-04 00:55:12
'''
import re
import threading
from yanhekt import YanHeKT
import PySimpleGUI as sg
class GUI:
def __init__(self):
self.window = None
self.init_ui()
self.yanhekt = None
self.td = None
def init_ui(self):
# sg.theme('DarkAmber') # Set a theme for the GUI
sg.theme('DarkTeal7')
frame_course_info = [
[
sg.Column([
[sg.Text('Web Link:', expand_x=True), sg.Input(key='WEB_LINK',
default_text="https://www.yanhekt.cn/course/11111", enable_events=True, size=(35, 2), expand_x=True)],
[sg.Text('Course ID:', expand_x=True), sg.Input(key='COURSE_ID', enable_events=True, size=(35, 2), expand_x=True)]
]),
sg.Column([
# [sg.Button('Get Course ID', expand_x=True, disabled=True)],
[sg.Button('Get Course Info', expand_x=True, disabled=True)]
])
],
[sg.HorizontalSeparator()],
[
sg.Column([
[sg.Text('Course Name:'), sg.Text('', key='COURSE_NAME'), sg.Text('Professor:'), sg.Text('', key='PROFESSOR')],
[sg.Listbox(values=[], size=(50, 8), expand_x=True, key='LESSON_LIST',
enable_events=True, select_mode=sg.LISTBOX_SELECT_MODE_EXTENDED)],
[sg.Text('use Ctrl or Shift or Drag to select multiple')],
[sg.Text('Ctrl + A to select all')],
], pad=(25, 0), expand_x=True, element_justification='center')],
]
# frame_lesson_selection = [
# [sg.Radio('All', group_id="Selection", key='ALL', default=True)],
# [sg.Radio('List', group_id="Selection", key='LIST')],
# [sg.Checkbox('Lesson 1', key='LESSON_1'), sg.Checkbox('Lesson 2', key='LESSON_2'), sg.Checkbox('Lesson 3', key='LESSON_3')],
# [sg.Radio('Range', group_id="Selection", key='RANGE')],
# [sg.Text('from'), sg.Spin([i for i in range(1, 10)], initial_value=0, key='RANGE_START'),
# sg.Text('to'), sg.Spin([i for i in range(1, 10)], initial_value=1, key='RANGE_END')],
# ]
frame_settings = [
[sg.Text('Video Type:'), sg.Radio('Dual', group_id="RADIO_VIDEO_TYPE", key='DUAL', default=True), sg.Radio(
'VGA', group_id="RADIO_VIDEO_TYPE", key='VGA'), sg.Radio('Video', group_id="RADIO_VIDEO_TYPE", key='VIDEO')],
[sg.Text('Output Directory:'), sg.Input("./Downloads", size=(30, 2), key="OUTPUT_DIR", expand_x=True),
sg.FolderBrowse(key='OUTPUT_DIR_BROWSE', initial_folder="./")],
[sg.Checkbox('Skip Existing', default=True, key='SKIP_EXISTING')],
]
# Layout definition
layout = [
[sg.Frame('Course Information', frame_course_info, expand_x=True)],
# [sg.Frame('Lesson Selection', frame_lesson_selection)],
[sg.Frame('Settings', frame_settings, expand_x=True)],
[sg.Column([
[sg.Button('Download Lessons', disabled=True)],
[sg.Output(size=(60, 10), expand_x=True, key='OUTPUT')],
[sg.ProgressBar(100, orientation='h', size=(40, 10), expand_x=True, key='PROGRESS_BAR')]
], expand_x=True, element_justification='center')]
]
# Create the Window
self.window = sg.Window('YanHeKT Downloader', layout)
def event_loop(self):
while True:
event, values = self.window.read()
if event == sg.WIN_CLOSED: # If user closes window
if self.td:
self.td.join()
break
try:
# if event == 'Get Course ID':
# courseID = values['WEB_LINK'].split('/')[-1]
# if (courseID.isdigit()):
# self.window['COURSE_ID'].update(courseID)
# self.window['Get Course Info'].update(disabled=False)
if event == 'Get Course Info':
if not values['COURSE_ID'].isdigit():
sg.popup_error("Invalid Course ID")
continue
else:
self.window['Download Lessons'].update(disabled=True)
self.yanhekt = YanHeKT(values['COURSE_ID'])
courseInfo, lessonList = self.yanhekt.getCourseInfo()
self.window['COURSE_NAME'].update(courseInfo['name_zh'])
self.window['PROFESSOR'].update(courseInfo['professors'][0]['name'])
lessonTitles = [lesson['title'] for lesson in lessonList]
self.window['LESSON_LIST'].update(values=[tittle for tittle in lessonTitles])
elif event == 'Download Lessons':
self.window['Download Lessons'].update(disabled=True)
self.window['PROGRESS_BAR'].update(0)
self.window['OUTPUT'].update("Downloading...\n\n")
# print(values)
lessonTitles = [lesson['title'] for lesson in self.yanhekt.lessonList]
selected_lessons = [i for i, tittle in enumerate(lessonTitles) if tittle in values['LESSON_LIST']]
self.yanhekt.updateArgs(_all=False, _list=selected_lessons, _range=False,
_dual=values['DUAL'], _vga=values['VGA'], _video=values['VIDEO'], _skip=values['SKIP_EXISTING'], _dir=values['OUTPUT_DIR'])
self.do_download()
elif event == 'WEB_LINK':
# print(values['WEB_LINK'])
if values['WEB_LINK']:
# self.window['Get Course ID'].update(disabled=False)
rex = re.search(r'(?<=course/)(\d+)', values['WEB_LINK'])
if (rex):
self.window['COURSE_ID'].update(rex.group(0))
self.window['Get Course Info'].update(disabled=False)
else:
self.window['COURSE_ID'].update("")
self.window['Get Course Info'].update(disabled=True)
# else:
# self.window['Get Course ID'].update(disabled=True)
elif event == 'COURSE_ID':
if values['COURSE_ID']:
self.window['Get Course Info'].update(disabled=False)
else:
self.window['Get Course Info'].update(disabled=True)
elif event == 'LESSON_LIST':
if (len(values['LESSON_LIST']) > 0):
self.window['Download Lessons'].update(disabled=False)
else:
self.window['Download Lessons'].update(disabled=True)
except Exception as e:
sg.popup_error(f'An error occurred: {str(e)}')
self.window.close()
def do_download(self):
# new thread
self.td = threading.Thread(target=self.yanhekt.download, args=(self.callback_download, self.callback_progress))
self.td.start()
def callback_download(self, isFinished):
self.window['Download Lessons'].update(disabled=False)
# self.window['OUTPUT'].update(f"Download success? {isFinished}\n\n")
def callback_progress(self, current, total):
self.window['PROGRESS_BAR'].update(current/total*100)
if __name__ == '__main__':
gui = GUI()
gui.event_loop()