-
Notifications
You must be signed in to change notification settings - Fork 6
/
maingui.py
270 lines (212 loc) · 8.69 KB
/
maingui.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
from tkinter import *
from tkinter import ttk
from tkinter.filedialog import askdirectory
import subprocess
import os
import pickle
from varname import nameof
import threading
import general
class main:
def __init__(self):
# window
self.window = Tk()
self.window.title("Coursera Downloader")
self.window.resizable(False, False)
# @variables
u = StringVar()
p = StringVar()
cauth = StringVar()
classname = StringVar()
path = StringVar()
vidres = StringVar()
sllangs = StringVar() # subtitle languages
self.shouldResume = False
# @
self.inputvardict = {'u': u, 'p': p,
'cauth': cauth, 'classname': classname,
'path': path, 'video_resolution': vidres,
'sl': sllangs}
# load argument's value
self.argdict = self.loadargdict()
# set input variable's values
for key, value in self.inputvardict.items():
value.set(self.argdict[key])
# Load UI
self.loadUI()
# start mainloop
self.window.mainloop()
def loadUI(self):
# frame
frame1 = Frame(self.window)
frame1.pack()
LW = 20 # LABEL WIDTH
EW = 50 # ENTRY WIDTH
# info message frame
infoMsgFrame = Frame(frame1, padx=0, pady=6)
infoMsgFrame.grid(row=1, column=1, columnspan=2)
msg = "You must be logged in in coursera.org in Chrome or Firefox.\
Browser don't have to be opened though.\
\nYou can only download courses that you are enrolled in"
infoMsg = Message(infoMsgFrame, text=msg, width=400)
infoMsg.grid(row=1)
# infoMsgFrame['text'] = "You must be logged in in coursera.org in Chrome or Firefox.(Browser don't have to be opene though)"
Label(frame1, text="Coursera Username/Email: ",
width=LW, anchor='w').grid(row=2, column=1)
emailentry = Entry(
frame1, textvariable=self.inputvardict['u'], width=EW)
emailentry.grid(row=2, column=2)
emailentry.focus_set()
Label(frame1, text="Coursera Password: ",
width=LW, anchor='w').grid(row=3, column=1)
Entry(
frame1, textvariable=self.inputvardict['p'], show='*', width=EW).grid(row=3, column=2)
# Label(frame1, text="Cookie Auth Code: ", width=LW, anchor='w').grid(row=3, column = 1)
# Entry(frame1, textvariable=self.inputvardict['cauth'], width=EW).grid(row=3, column=2)
# course name row
Label(frame1,
text="Course Home Page URL: ",
width=LW,
anchor='w').grid(row=4, column=1)
Entry(frame1,
textvariable=self.inputvardict['classname'],
width=EW).grid(row=4, column=2)
# download folder row
Label(frame1, text="Download Folder: ", width=LW,
anchor='w').grid(row=5, column=1, sticky='W')
Button(frame1, text="Select Folder", command=self.getPath).grid(
row=5, column=2, sticky='W')
Message(frame1, textvariable=self.inputvardict['path'], width=300, anchor='w').grid(
row=6, column=2, sticky='w')
# video resolution row
Label(frame1,
text="Video Resolution: ",
width=LW,
anchor='w').grid(row=7, column=1, sticky='W')
innerframe = Frame(frame1)
innerframe.grid(row=7, column=2, sticky='W')
Radiobutton(innerframe,
text="720p",
variable=self.inputvardict['video_resolution'],
value='720p').grid(row=1, column=1)
Radiobutton(innerframe,
text="540p",
variable=self.inputvardict['video_resolution'],
value='540p').grid(row=1, column=2)
Radiobutton(innerframe,
text="360p",
variable=self.inputvardict['video_resolution'],
value='360p').grid(row=1, column=3)
self.inputvardict['video_resolution'].set('720p')
# subtitle language row
Label(frame1,
text='Subtitle Language: ',
width=LW,
anchor='w').grid(row=10, column=1)
self.sllangschoices = general.LANG_NAME_TO_CODE_MAPPING
self.inputvardict['sl'].set('English')
ttk.Combobox(frame1,
textvariable=self.inputvardict['sl'],
values=sorted(list(self.sllangschoices.keys())),
state='readonly').grid(row=10, column=2, sticky='W')
# transcript row
# download and resume button row
btnFrame = Frame(frame1)
btnFrame.grid(row=11, column=2, sticky='E')
downloadBtn = Button(btnFrame,
text="Download",
command=self.downloadBtnHandler)
downloadBtn.grid(row=11, column=2, sticky='E')
resumeBtn = Button(btnFrame,
text='Resume',
command=self.resumeBtnHandler)
resumeBtn.grid(row=11, column=1, sticky='E')
def downloadBtnHandler(self):
# load cauth code automatically and store it in inputvardict
self.inputvardict['cauth'].set(general.loadcauth('coursera.org'))
# make argdict from inputvarlist
self.argdict = {}
for key, value in self.inputvardict.items():
# do necessary processing
# process classname variable which actually stores course home page url
# convert the home page url to classname
if key == 'classname':
courseurl = self.inputvardict['classname'].get()
cname = general.urltoclassname(courseurl)
self.argdict[key] = cname
continue
if key == 'sl':
langcode = self.sllangschoices[self.inputvardict['sl'].get()]
if langcode == '':
self.argdict['ignore-formats'] = "srt"
self.argdict[key] = 'en'
continue
else:
self.argdict[key] = langcode
continue
self.argdict[key] = value.get()
# save the argdict to data.bin
self.saveargdic()
# create command from argumentdict
cmd = ["coursera_dl.exe"]
cmd.append('--download-quizzes')
cmd.append('--download-notebooks')
cmd.append('--disable-url-skipping')
cmd.append('--unrestricted-filenames')
cmd.append('--combined-section-lectures-nums')
cmd.append('--jobs')
cmd.append('1')
if self.shouldResume == True:
cmd.append("--resume")
for item in self.argdict.items():
# convert cauth to --cauth and u to -u
if len(item[0]) <= 2:
flag = '-' + item[0]
else:
flag = '--' + item[0]
# convert video_resolution to video-resoltion
flag = flag.replace('_', '-')
# now append to cmd
# @ FILTER ARGUMENTS THAT DON'T NEED FLAG LIKE, classname, resume etc.
if not 'classname' in flag:
cmd.append(flag)
cmd.append(item[1])
# run cmd
print(cmd)
# subprocess.call(cmd)
t = threading.Thread(target=self.subprocesscall, args=[cmd])
t.daemon = True
t.start()
def resumeBtnHandler(self):
self.shouldResume = True
self.downloadBtnHandler()
def subprocesscall(self, command):
# command is a list of the command line commands
subprocess.call(command)
def loadargdict(self):
# dic = {'username':'', 'password':'', 'cauth': '', 'path':''}
dic = {}
for i in self.inputvardict.keys():
dic[i] = ''
# if data.bin doesn't exist make it return the empty dic
if not os.path.isfile("data.bin"):
f = open("data.bin", 'wb')
pickle.dump(dic, f)
f.close()
return dic
# else load dic from data.bin
else:
f = open("data.bin", 'rb')
dic = pickle.load(f)
return dic
def saveargdic(self):
# dic = {'username': self.argdict['username'],
# 'password': self.argdict['password'],
# 'cauth': self.argdict['cauth'],
# 'path': self.argdict['path']} #tkinter variable are saved. get method has to be used to get their value
f = open("data.bin", 'wb')
pickle.dump(self.argdict, f)
f.close()
def getPath(self):
self.inputvardict['path'].set(askdirectory())
main()