-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBilibiliCrawlerGUI.py
289 lines (250 loc) · 12.5 KB
/
BilibiliCrawlerGUI.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
from tkinter import *
from tkinter.ttk import *
from typing import Dict
import HelpFunctions
import BilibiliCrawler
import YoutubeCrawler
import os
class WinGUI(Toplevel):
widget_dic: Dict[str, Widget] = {}
def __init__(self):
super().__init__()
self.__win()
self.widget_dic["tk_label_BVNumber"] = self.__tk_label_BVNumber(self)
self.widget_dic["tk_input_BVNumberInput"] = self.__tk_input_BVNumberInput(self)
self.widget_dic["tk_button_BVNumberHelp"] = self.__tk_button_BVNumberHelp(self)
self.widget_dic["tk_label_BVNumberHelpNote"] = self.__tk_label_BVNumberHelpNote(self)
self.widget_dic["tk_label_OutputPath"] = self.__tk_label_OutputPath(self)
self.widget_dic["tk_text_AudioOutputPathInput"] = self.__tk_text_AudioOutputPathInput(self)
self.widget_dic["tk_label_AudioOutputNote"] = self.__tk_label_AudioOutputNote(self)
self.widget_dic["tk_button_ShowDefaultPath"] = self.__tk_button_ShowDefaultPath(self)
self.widget_dic["tk_label_PNumberLabel"] = self.__tk_label_PNumberLabel(self)
self.widget_dic["tk_input_StartPInput"] = self.__tk_input_StartPInput(self)
self.widget_dic["tk_label_MiddleLabel"] = self.__tk_label_MiddleLabel(self)
self.widget_dic["tk_input_EndPInput"] = self.__tk_input_EndPInput(self)
self.widget_dic["tk_label_RightLabel"] = self.__tk_label_RightLabel(self)
self.widget_dic["tk_label_AudioOutputFormat"] = self.__tk_label_AudioOutputFormat(self)
self.widget_dic["tk_select_box_lhoqevnz"] = self.__tk_select_box_lhoqevnz(self) # select output format
self.widget_dic["tk_label_PNumberNote"] = self.__tk_label_PNumberNote(self)
self.widget_dic["tk_label_ActionsLabel"] = self.__tk_label_ActionsLabel(self)
self.widget_dic["tk_button_DownloadButton"] = self.__tk_button_DownloadButton(self)
self.widget_dic["tk_label_LogNote"] = self.__tk_label_LogNote(self)
self.widget_dic["tk_text_Log"] = self.__tk_text_Log(self)
with open('./Resources/info.txt', 'r') as f:
open_help_var = int(f.read())
if open_help_var == 1:
help_instance = HelpFunctions.BilibiliCrawlerHelp(self)
def __win(self):
self.title("Bilibili & YouTube Audio Crawler")
# 设置窗口大小、居中
width = 600
height = 400
screenwidth = self.winfo_screenwidth()
screenheight = self.winfo_screenheight()
geometry = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
self.geometry(geometry)
self.resizable(width=False, height=False)
# 自动隐藏滚动条
def scrollbar_autohide(self,bar,widget):
self.__scrollbar_hide(bar,widget)
widget.bind("<Enter>", lambda e: self.__scrollbar_show(bar,widget))
bar.bind("<Enter>", lambda e: self.__scrollbar_show(bar,widget))
widget.bind("<Leave>", lambda e: self.__scrollbar_hide(bar,widget))
bar.bind("<Leave>", lambda e: self.__scrollbar_hide(bar,widget))
def __scrollbar_show(self,bar,widget):
bar.lift(widget)
def __scrollbar_hide(self,bar,widget):
bar.lower(widget)
def __tk_label_BVNumber(self,parent):
label = Label(parent,text="BV / V / List number:",anchor="center")
label.place(x=30, y=20, width=126, height=30)
return label
def __tk_input_BVNumberInput(self,parent):
ipt = Entry(parent)
ipt.place(x=170, y=20, width=268, height=30)
return ipt
def __tk_button_BVNumberHelp(self,parent):
btn = Button(parent, text="❓")
btn.place(x=460, y=20, width=30, height=30)
return btn
def __tk_label_BVNumberHelpNote(self,parent):
label = Label(parent,text="How to find \nThe Numbers",anchor="center",font=('Arial',8))
label.place(x=500, y=10, width=80, height=54)
return label
def __tk_label_OutputPath(self,parent):
label = Label(parent,text="Audio Output Path:",anchor="center")
label.place(x=30, y=60, width=116, height=30)
return label
def __tk_text_AudioOutputPathInput(self,parent):
text = Text(parent)
text.place(x=170, y=60, width=326, height=61)
vbar = Scrollbar(parent)
text.configure(yscrollcommand=vbar.set)
vbar.config(command=text.yview)
vbar.place(x=481, y=60, width=15, height=61)
self.scrollbar_autohide(vbar,text)
return text
def __tk_label_AudioOutputNote(self,parent):
label = Label(parent,text="Leave it blank if you want to have it output in the default directory",anchor="center")
label.place(x=170, y=117, width=397, height=30)
return label
def __tk_button_ShowDefaultPath(self,parent):
btn = Button(parent, text="❓")
btn.place(x=510, y=80, width=30, height=30)
return btn
def __tk_label_PNumberLabel(self,parent):
label = Label(parent,text="P number:",anchor="center")
label.place(x=30, y=160, width=116, height=30)
return label
def __tk_input_StartPInput(self,parent):
ipt = Entry(parent)
ipt.place(x=170, y=160, width=62, height=30)
return ipt
def __tk_label_MiddleLabel(self,parent):
label = Label(parent,text="P ~ ",anchor="center")
label.place(x=224, y=160, width=50, height=30)
return label
def __tk_input_EndPInput(self,parent):
ipt = Entry(parent)
ipt.place(x=270, y=160, width=62, height=30)
return ipt
def __tk_label_RightLabel(self,parent):
label = Label(parent,text="P",anchor="center")
label.place(x=320, y=160, width=15, height=30)
return label
def __tk_label_AudioOutputFormat(self,parent):
label = Label(parent,text="Audio Output Format: ",anchor="center")
label.place(x=367, y=160, width=142, height=30)
return label
def __tk_select_box_lhoqevnz(self,parent):
cb = Combobox(parent, state="readonly")
cb['values'] = (".mp3",".wav",".ogg")
cb.place(x=510, y=160, width=83, height=30)
return cb
def __tk_label_PNumberNote(self,parent):
label = Label(parent,text="*If you are only crawling one video's audio or only crawling the first P, you can leave these two blank\n"
"*If you are crawling a Youtube List, you can also use this! Treat the P as video episodes in the list.",anchor="center", font=('Arial',8))
label.place(x=10, y=190, width=581, height=30)
return label
def __tk_label_ActionsLabel(self,parent):
label = Label(parent,text="Actions:",anchor="center")
label.place(x=30, y=230, width=116, height=30)
return label
def __tk_button_DownloadButton(self,parent):
btn = Button(parent, text="Download!")
btn.place(x=170, y=230, width=102, height=30)
return btn
def __tk_label_LogNote(self,parent):
label = Label(parent,text="Log:",anchor="center")
label.place(x=0, y=270, width=56, height=30)
return label
def __tk_text_Log(self,parent):
text = Text(parent)
text.place(x=0, y=300, width=600, height=100)
vbar = Scrollbar(parent)
text.configure(yscrollcommand=vbar.set)
vbar.config(command=text.yview)
vbar.place(x=585, y=300, width=15, height=100)
self.scrollbar_autohide(vbar,text)
return text
class Win(WinGUI):
def __init__(self):
super().__init__()
self.__event_bind()
self.config(menu=self.create_menu())
def create_menu(self):
menu = Menu(self,tearoff=False)
menu.add_command(label="Help", command=self.BilibiliAudioCrawlerHelp)
return menu
def BilibiliAudioCrawlerHelp(self): # TODO
help_instance = HelpFunctions.BilibiliCrawlerHelp(self)
def BVNumberHelp(self,evt):
HelpFunctions.BVNumberHelp()
def ShowDefaultPathBilibili(self,evt):
HelpFunctions.Show_Default_Path_Bilibili()
def Download(self,evt):
mode = 0
BV_num = self.widget_dic["tk_input_BVNumberInput"].get().strip()
output_path = self.widget_dic["tk_text_AudioOutputPathInput"].get("1.0", 'end').strip()
output_format = self.widget_dic["tk_select_box_lhoqevnz"].get().lstrip('.')
p_start = self.widget_dic["tk_input_StartPInput"].get().strip()
if BV_num[:2] == 'BV':
mode = 1 # Bilibili
if p_start == '':
p_start = 1
else:
p_start = int(p_start)
p_end = self.widget_dic["tk_input_EndPInput"].get().strip()
if p_end == '':
p_end = 1
else:
p_end = int(p_end)
log = self.widget_dic["tk_text_Log"]
##################################
if output_format != '' and mode == 1:
try:
BilibiliCrawler.BilibiliAudioDownload(bv=BV_num, p_start=p_start, p_end=p_end, log=log, output_path=output_path, output_format=output_format)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
# BilibiliCrawler.BilibiliAudioDownload(bv=BV_num, p_start=p_start, p_end=p_end, log=log,
# output_path=output_path, output_format=output_format)
elif output_format == '' and mode == 1:
try:
BilibiliCrawler.BilibiliAudioDownload(bv=BV_num, p_start=p_start, p_end=p_end, log=log, output_path=output_path)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
# BilibiliCrawler.BilibiliAudioDownload(bv=BV_num, p_start=p_start, p_end=p_end, log=log,
# output_path=output_path)
elif mode == 0:
if len(BV_num) == 11:
if output_format != '':
try:
YoutubeCrawler.YoutubeAudioDownload(v=BV_num, log=log, output_path=output_path, output_format=output_format)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
elif output_format == '':
try:
YoutubeCrawler.YoutubeAudioDownload(v=BV_num, log=log, output_path=output_path)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
elif len(BV_num) == 34:
if output_format != '':
try:
YoutubeCrawler.YoutubeAudioDownloadList(listnum=BV_num, p_start=p_start, p_end=p_end, log=log, output_path=output_path, output_format=output_format)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
elif output_format == '':
try:
YoutubeCrawler.YoutubeAudioDownloadList(listnum=BV_num, p_start=p_start, p_end=p_end, log=log, output_path=output_path)
except Exception as e:
# Log the error message
log.insert('end', f"Error: {e}\n")
log.see('end')
print('An Error has occurred. Please check the log.')
else:
log.insert('end', "A RANDOM ERROR HAS OCCURRED. This shouldn't happen though. Weird. Check your inputs")
log.see('end')
print('An Error has occurred. Please check the log.')
############################################
def __event_bind(self):
self.widget_dic["tk_button_BVNumberHelp"].bind('<Button-1>',self.BVNumberHelp)
self.widget_dic["tk_button_ShowDefaultPath"].bind('<Button-1>',self.ShowDefaultPathBilibili)
self.widget_dic["tk_button_DownloadButton"].bind('<Button-1>',self.Download)
if __name__ == "__main__":
win = Win()
win.mainloop()