-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwidget_26.py
1605 lines (1511 loc) · 87.9 KB
/
mainwidget_26.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'test_mainwidget_2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
# from shutil import copyfile
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from LogUtilClass import LogUtilClass, QMessageLogUtilClass
from UtilClass import UtilClass
from DownloadThreadClass import *
import time
import threading
import os
# import json
# import datetime
abs_dir = os.path.dirname(os.path.abspath(__file__))
class Ui_Form(QWidget):
receive_signal_dict = {}
send_signal_dict = {}
# send_signal_dict['pause_download_signal'] = pyqtSignal(dict) #这里适配一下用dict控制暂停按钮,通过int index_in_download_list来区分
pause_download_signal = pyqtSignal(dict) #这里适配一下用dict控制暂停按钮
print(pause_download_signal)
def __init__(self) -> None:
super(Ui_Form, self).__init__()
self.log = LogUtilClass()
self.save_dir = ""
self.thread_manager_dict = {} #用于存放线程,避免线程成为局部变量而报错
self.receive_signal_dict['get_refresh_finish_signal_func'] = self.get_refresh_finish_signal_func
self.receive_signal_dict['get_download_thread_finish_signal'] = self.get_download_thread_finish_signal
self.receive_signal_dict['get_collection_info_finish_signal'] = self.get_collection_info_finish_signal
self.receive_signal_dict['get_single_url_info_finish_signal'] = self.get_single_url_info_finish_signal
self.receive_signal_dict['get_update_progressbar_signal'] = self.get_update_progressbar_signal
self.download_list = []
self.url = "" #QLineEdit的内容
self.collection_info_list = []
self.is_multi_thread_download = False
self.dw_index = 0
def setupUi(self, Form):
Form.setObjectName("Form")
Form.setWindowIcon(QtGui.QIcon(os.path.join(abs_dir, "icon.ico")))
Form.resize(800, 400)
self.gridLayout = QtWidgets.QGridLayout(Form)
self.gridLayout.setObjectName("gridLayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.input_url_line = QtWidgets.QLineEdit(Form)
self.input_url_line.setInputMask("")
self.input_url_line.setText("")
self.input_url_line.setObjectName("input_url_line")
self.horizontalLayout_2.addWidget(self.input_url_line)
self.single_url_parse_button = QtWidgets.QPushButton(Form)
self.single_url_parse_button.setObjectName("single_url_parse_button")
self.horizontalLayout_2.addWidget(self.single_url_parse_button)
self.collection_url_parse_button = QtWidgets.QPushButton(Form)
self.collection_url_parse_button.setObjectName("collection_url_parse_button")
self.horizontalLayout_2.addWidget(self.collection_url_parse_button)
self.gridLayout.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.show_save_location_label = QtWidgets.QLabel(Form)
self.show_save_location_label.setStyleSheet("background-color: rgb(255, 255, 255);")
self.show_save_location_label.setText("")
self.show_save_location_label.setObjectName("show_save_location_label")
self.horizontalLayout_3.addWidget(self.show_save_location_label)
self.select_save_location_button = QtWidgets.QPushButton(Form)
self.select_save_location_button.setObjectName("select_save_location_button")
self.horizontalLayout_3.addWidget(self.select_save_location_button)
self.horizontalLayout_3.setStretch(0, 1)
self.gridLayout.addLayout(self.horizontalLayout_3, 1, 0, 1, 1)
self.scrollArea = QtWidgets.QScrollArea(Form)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
self.scrollAreaWidgetContents = QtWidgets.QWidget()
self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 776, 302))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
self.verticalLayout = QtWidgets.QVBoxLayout(self.scrollAreaWidgetContents)
self.verticalLayout.setObjectName("verticalLayout")
self.verticalLayout_2 = QtWidgets.QVBoxLayout()
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.downlist_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.downlist_label.setStyleSheet("")
self.downlist_label.setObjectName("downlist_label")
self.horizontalLayout_4.addWidget(self.downlist_label)
self.download_all_button = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.download_all_button.setObjectName("download_all_button")
self.horizontalLayout_4.addWidget(self.download_all_button)
self.delete_all_button = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.delete_all_button.setObjectName("delete_all_button")
self.horizontalLayout_4.addWidget(self.delete_all_button)
self.horizontalLayout_4.setStretch(0, 5)
self.verticalLayout_2.addLayout(self.horizontalLayout_4)
self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
self.horizontalLayout_5.setObjectName("horizontalLayout_5")
self.total_progress_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.total_progress_label.setObjectName("total_progress_label")
self.horizontalLayout_5.addWidget(self.total_progress_label)
self.total_progressbar = QtWidgets.QProgressBar(self.scrollAreaWidgetContents)
self.total_progressbar.setProperty("value", 0)
self.total_progressbar.setObjectName("total_progressbar")
self.horizontalLayout_5.addWidget(self.total_progressbar)
self.total_size_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
self.total_size_label.setObjectName("total_size_label")
self.horizontalLayout_5.addWidget(self.total_size_label)
self.verticalLayout_2.addLayout(self.horizontalLayout_5)
self.verticalLayout.addLayout(self.verticalLayout_2)
self.spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Expanding)
self.verticalLayout.addItem(self.spacerItem)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
self.gridLayout.addWidget(self.scrollArea, 2, 0, 1, 1)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "B站视频下载"))
self.input_url_line.setPlaceholderText(_translate("Form", "请输入URL"))
self.single_url_parse_button.setText(_translate("Form", "单视频解析"))
self.collection_url_parse_button.setText(_translate("Form", "合集解析"))
self.select_save_location_button.setText(_translate("Form", "选择保存位置"))
self.downlist_label.setText(_translate("Form", "下载列表"))
self.download_all_button.setText(_translate("Form", "下载全部"))
self.delete_all_button.setText(_translate("Form", "删除全部"))
self.total_progress_label.setText(_translate("Form", "总进度"))
self.total_size_label.setText(_translate("Form", "1.0K/5.0M"))
#==============
self.total_size_label.setVisible(False)
self.total_progress_label.setVisible(False)
self.total_progressbar.setVisible(False)
#===============
#链接按钮
self.single_url_parse_button.clicked.connect(self.parse_single_url)
self.select_save_location_button.clicked.connect(self.select_save_folder)
self.collection_url_parse_button.clicked.connect(self.parse_collection_url)
self.download_all_button.clicked.connect(self.download_all_url)
self.delete_all_button.clicked.connect(self.delete_all_info)
def parse_single_url(self):
self.url = self.input_url_line.text()
print(self.url)
if self.url.isspace() or self.url=="" or len(self.url)==0:
QMessageLogUtilClass().show("提示","URL不能为空")
return
# if not self.save_dir or self.save_dir.isspace() :
# QMessageLogUtilClass().show("提示","请选择保存地址")
# return
#先检查一下时候有重复的下载解析结果
for index, horizontallayout_dict in enumerate(self.download_list):
parse_single_url = horizontallayout_dict['parse_single_url']
parse_collection_url = horizontallayout_dict['parse_collection_url']
title = horizontallayout_dict['prime_download_info_dict']['title']
if self.url == parse_single_url or self.url == parse_collection_url:
user_choice = QMessageLogUtilClass(set_cancel_button=True).show("提示","该链接已经解析过,是否删除原解析结果?")
if user_choice == QMessageBox.Ok:
# self.download_list.pop(index)
# del horizontallayout_dict
#同时删除页面布局本身的布局控件
# self.delete_horizonlayout(index) #先删除再pop,因为该方法需要self.downlod_lsit中的信息。
self.download_list.pop(index)
horizontallayout = horizontallayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
self.delete_horizonlayout(horizontallayout)
del horizontallayout_dict
# self.delete_record
# print(horizontallayout_dict)
# return
break
else:
return
#刷新下载列表
#先禁用按钮
self.single_url_parse_button.setEnabled(False) #后面获取结束信号在恢复
self.refresh_download_list()
# #将解析工作放到线程中进行
# timestamp = time.time()
# downloadthread = DownloadThread(click_index=timestamp, url=self.url, options="get_single_url_info")
# downloadthread_dict = {}
# downloadthread_dict['options'] = "get_single_url_info"
# downloadthread_dict['index'] = len(self.download_list)
# downloadthread_dict['thread'] = downloadthread
# downloadthread_dict['url'] = self.url
# downloadthread_dict['timestamp'] = timestamp #唯一标识
# self.thread_manager_dict[downloadthread_dict['timestamp']] = downloadthread_dict #用字典方便查找,后续删除也方便。
# #链接信号
# downloadthread.send_single_url_info_finish_signal.connect(self.get_single_url_info_finish_signal)
# #开启线程
# self.thread_manager_dict[timestamp]['thread'].start()
#结束,等信号
return
def parse_collection_url(self):
#首先清除download_list
self.delete_all_info()
self.log.info("开始解析合集")
input_collection_url = self.input_url_line.text()
time_stamp = int(time.time())
parse_collection_thread = DownloadThread(time_stamp, collection_url=input_collection_url,\
download_file_type="collection_parse")
#链接解析完成信号
parse_collection_thread.parse_collection_finish_signal.connect(self.get_parse_collection_finish_signal)
#禁用所有按钮,等待解析完毕
self.disable_or_enable_widgets(False)
#将线程加入manager
downloadthread_dict = {}
downloadthread_dict['options'] = "collection_parse"
downloadthread_dict['index'] = len(self.download_list)
downloadthread_dict['thread'] = parse_collection_thread
downloadthread_dict['url'] = self.url
downloadthread_dict['timestamp'] = time_stamp #唯一标识
self.thread_manager_dict[downloadthread_dict['timestamp']] = downloadthread_dict #用字典方便查找,后续删除也方便。
#链接信号
parse_collection_thread.send_single_url_info_finish_signal.connect(self.get_single_url_info_finish_signal)
#开启线程
self.thread_manager_dict[time_stamp]['thread'].start()
return
def download_all_url1(self): #测试用
pass
print(len(self.thread_manager_dict))
print(len(self.download_list))
def download_all_url2(self):
#先检查文件是否又savev_path
if self.save_dir == "":
self.log.error("请先设置保存路径")
QMessageLogUtilClass().show("提示","请选择保存地址")
return
pass
print(len(self.thread_manager_dict))
print(len(self.download_list))
self.multi_dw_keys = []
for key in self.thread_manager_dict.keys():
if len(str(key).split('-')) ==2:
if str(key).split('-')[1] == "download_thread":
self.multi_dw_keys.append(key)
if len(self.multi_dw_keys) == 0:
self.log.error("当前没有下载线程")
return
self.is_multi_thread_download = True
self.log.debug(f"keys: {self.multi_dw_keys}")
# return
len1 = 2 if len(self.multi_dw_keys)>=2 else 1
for i in range(len1):
# self.thread_manager_dict[self.multi_dw_keys[i]]['thread'].start()
# self.pause_download_signal.emit({})
self.log.info(f"开始下载第{i}个线程")
index_in_download_list = self.thread_manager_dict[self.multi_dw_keys[i]]['index']
self.start_or_pause_download_when_multi_thread(index_in_download_list)
def download_all_url(self):
#先检查文件是否又savev_path
if self.save_dir == "":
self.log.error("请先设置保存路径")
QMessageLogUtilClass().show("提示","请选择保存地址")
return
pass
print(len(self.thread_manager_dict))
print(len(self.download_list))
self.multi_dw_keys = []
# return
self.max_thread = 3 if len(self.download_list)>=3 else 1
self.dw_index = 0
self.is_multi_thread_download = True
len1 = 2 if len(self.download_list)>=2 else 1
for i in range(self.max_thread):
# self.dw_index+=1
self.start_or_pause_download_when_multi_thread(self.dw_index)
self.dw_index+=1
def delete_all_info(self):
#直接删除所有layout,清空self.thread_manager_dict
for index, horizontallayout_dict in enumerate(self.download_list):
horizontallayout = horizontallayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
self.delete_horizonlayout(horizontallayout)
# self.download_list.remove(horizontallayout_dict)
self.download_list[index] = None
del horizontallayout_dict
#清空self.thread_manager_dict
keys = list(self.thread_manager_dict.keys())
for key in keys:
thread_to_destroy = self.thread_manager_dict.get(key)['thread']
# del thread_to_destroy
del self.thread_manager_dict[key]
# 终止线程
thread_to_destroy.terminate()
# 检查线程的状态
if thread_to_destroy.isFinished() or True: #没结束也删除
self.log.error(f"线程{thread_to_destroy}已经结束")
# 销毁线程对象
thread_to_destroy.deleteLater()
# break
self.download_list=[]
self.log.info("删除所有下载信息")
# print(self.thread_manager_dict)
print(self.download_list)
def parse_per_url_in_collection_list(self):
pass
# for index, per_url_info_dict in enumerate(self.collection_info_list):
# url = per_url_info_dict['url']
# print(f"当前url : {url}")
# time.sleep(1)
#先检查一下时候有重复的下载解析结果
for index, horizontallayout_dict in enumerate(self.download_list):
parse_single_url = horizontallayout_dict['parse_single_url']
parse_collection_url = horizontallayout_dict['parse_collection_url']
if self.url == parse_single_url or self.url == parse_collection_url:
# user_choice = QMessageLogUtilClass(set_cancel_button=True).show("提示","该链接已经解析过,是否删除原解析结果?")
if True:
self.download_list.pop(index)
horizontallayout = horizontallayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
self.delete_horizonlayout(horizontallayout)
del horizontallayout_dict
break
else:
return
#刷新下载列表
#先禁用按钮
self.disable_or_enable_widgets(False) #后面获取结束信号在恢复
self.refresh_download_list_when_parse_collection(index_in_collection=0)
def add_progress_layout(self, single_url_info_dict ={}):
self.log.info("开始添加progress布局")
# 先移除 弹簧
self.verticalLayout.removeItem(self.spacerItem)
horizontalLayout = QtWidgets.QHBoxLayout()
horizontalLayout.setObjectName("horizontalLayout")
title_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(title_label.sizePolicy().hasHeightForWidth())
title_label.setSizePolicy(sizePolicy)
title_label.setObjectName("title_label")
horizontalLayout.addWidget(title_label)
progressBar = QtWidgets.QProgressBar(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(progressBar.sizePolicy().hasHeightForWidth())
progressBar.setSizePolicy(sizePolicy)
progressBar.setProperty("value", 0)
progressBar.setObjectName("progressBar")
horizontalLayout.addWidget(progressBar)
progress_label = QtWidgets.QLabel(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(progress_label.sizePolicy().hasHeightForWidth())
progress_label.setSizePolicy(sizePolicy)
font = QtGui.QFont()
font.setPointSize(9)
progress_label.setFont(font)
progress_label.setTextFormat(QtCore.Qt.AutoText)
progress_label.setScaledContents(False)
progress_label.setObjectName("progress_label")
horizontalLayout.addWidget(progress_label)
start_or_pause_button = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(start_or_pause_button.sizePolicy().hasHeightForWidth())
start_or_pause_button.setSizePolicy(sizePolicy)
start_or_pause_button.setObjectName("start_or_pause_button")
horizontalLayout.addWidget(start_or_pause_button)
comboBox = QtWidgets.QComboBox(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(comboBox.sizePolicy().hasHeightForWidth())
comboBox.setSizePolicy(sizePolicy)
comboBox.setEditable(True)
comboBox.setObjectName("comboBox")
comboBox.addItem("")
horizontalLayout.addWidget(comboBox)
delete_button = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Ignored, QtWidgets.QSizePolicy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(delete_button.sizePolicy().hasHeightForWidth())
delete_button.setSizePolicy(sizePolicy)
delete_button.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
delete_button.setObjectName("delete_button")
horizontalLayout.addWidget(delete_button)
horizontalLayout.setStretch(0, 4)
horizontalLayout.setStretch(1, 10)
horizontalLayout.setStretch(2, 3)
horizontalLayout.setStretch(3, 3)
horizontalLayout.setStretch(4, 4)
horizontalLayout.setStretch(5, 3)
comboBox.setCurrentIndex(0)
self.verticalLayout.addLayout(horizontalLayout)
#设置控件样式
_translate = QtCore.QCoreApplication.translate
title_label.setText(_translate("Form", "TextLabel"))
progress_label.setText(_translate("Form", "1.0K/5.0M"))
start_or_pause_button.setText(_translate("Form", "开始下载"))
comboBox.setCurrentText(_translate("Form", "视频质量"))
comboBox.setItemText(0, _translate("Form", "视频质量选择"))
delete_button.setText(_translate("Form", "删除记录"))
#再添加弹簧
self.verticalLayout.addItem(self.spacerItem)
#设置一个字典来存储这个水平布局
#获取时间戳作为线程唯一标识
time_stamp = int(time.time())
this_horizontalLayout_dict = {}
this_horizontalLayout_layout_dict = {} #用于存放所有布局和控件
this_horizontalLayout_layout_dict['horizontalLayout'] = horizontalLayout
this_horizontalLayout_layout_dict['title_label'] = title_label
this_horizontalLayout_layout_dict['progressBar'] = progressBar
this_horizontalLayout_layout_dict['progress_label'] = progress_label
this_horizontalLayout_layout_dict['start_or_pause_button'] = start_or_pause_button
this_horizontalLayout_layout_dict['comboBox'] = comboBox
this_horizontalLayout_layout_dict['delete_button'] = delete_button
this_horizontalLayout_dict['this_horizontalLayout_layout_dict'] = this_horizontalLayout_layout_dict
this_horizontalLayout_dict['download_info_dict'] = single_url_info_dict #完整的下载信息
this_horizontalLayout_dict['download_thread'] = None #下载线程
this_horizontalLayout_dict['prime_download_info_dict'] = None #优先选择的下载信息
this_horizontalLayout_dict['download_status_info_dict'] = None #用于存储下载状态的字典
this_horizontalLayout_dict['this_horizontalLayout_create_time'] = time_stamp #用时间戳来唯一标识这个水平布局
this_horizontalLayout_dict['index_in_download_list'] = None #用于表示再下载列表中的位置
this_horizontalLayout_dict['receive_signal_handler_func_dict'] = self.receive_signal_dict #用于存储信号处理函数的字典
this_horizontalLayout_dict['send_signal_dict'] = self.send_signal_dict #用于存储信号的字典
this_horizontalLayout_dict['save_dir'] = self.save_dir #用于存储错误信息
this_horizontalLayout_dict['parse_single_url'] = single_url_info_dict['parse_single_url'] #记录用于解析的url
this_horizontalLayout_dict['parse_collection_url'] = single_url_info_dict['parse_collection_url'] #用于解析合集的url
this_horizontalLayout_dict['err_dict'] = None #用于存储错误信息
time.sleep(0.01) #这里确保时间戳的唯一性
#然后进行按钮的链接
start_or_pause_button.clicked.connect(self.start_or_pause_download)
delete_button.clicked.connect(self.delete_record)
comboBox.currentIndexChanged.connect(self.select_video_quality)
comboBox.setEditable(False)
#然后将this_horizontalLayout_dict添加到self.download_list中
index_in_download_list = len(self.download_list)
this_horizontalLayout_dict['index_in_download_list'] = index_in_download_list
self.download_list.append(this_horizontalLayout_dict)
self.log.info("progress布局加入到了self.dowload_list")
# self.download_list[0].hide()
#先隐藏布局控件
for index in range(0, horizontalLayout.count()):
item = horizontalLayout.itemAt(index)
widget_item = item.widget()
widget_item.setVisible(False)
# #获取时间戳作为线程唯一标识
# time_stamp = time.time()
options="arrange_horizontallayout_in_downloadlist"
#然后开一个线程对horizontalLayout的dict进行完善,完善后再显示它
utilclass_thread = UtilClass(index=time_stamp, download_list=self.download_list, horizontaldict=self.download_list[-1],\
options=options, thread_manager_dict=self.thread_manager_dict, \
pause_download_signal=self.pause_download_signal, save_dir=self.save_dir)
#链接线程结束信号
utilclass_thread.ARRANGE_HORIZONTALLAYOUT_IN_DOWNLOADLIST.connect(self.get_arrage_finish_signal)
# self.pause_download_signal.connect(utilclass_thread.temp_pause_download_signal) #这里单独设置与downloaad_theread的连接了
#加入线程manager
utilclass_thread_dict = {}
utilclass_thread_dict['options'] = "get_single_url_info"
utilclass_thread_dict['index'] = len(self.download_list)
utilclass_thread_dict['thread'] = utilclass_thread
utilclass_thread_dict['url'] = self.url
utilclass_thread_dict['timestamp'] = time_stamp #唯一标识
self.thread_manager_dict[time_stamp] = utilclass_thread_dict
#然后开启线程
self.thread_manager_dict[time_stamp]['thread'].start()
# self.log.debug(f"self.thread_manager_dict为: {self.thread_manager_dict}")
self.log.debug(f"self.thread_manager_dict长度为: {len(self.thread_manager_dict)}")
return this_horizontalLayout_dict
def start_or_pause_download_when_multi_thread(self, index_in_download_list):
self.log.error(index_in_download_list)
# #先检查文件是否又savev_path
# if self.save_dir == "":
# self.log.error("请先设置保存路径")
# QMessageLogUtilClass().show("提示","请选择保存地址")
# return
try:
click_button_index_in_download_list = index_in_download_list
this_horizontalLayout_dict = self.download_list[click_button_index_in_download_list]
# self.log.error(this_horizontalLayout_dict['send_signal_dict'])
this_horizontalLayout_layout_dict = this_horizontalLayout_dict['this_horizontalLayout_layout_dict']
this_horizontalLayout = this_horizontalLayout_layout_dict['horizontalLayout']
this_button = this_horizontalLayout_layout_dict['start_or_pause_button']
click_button_time_stamp = int(this_button.objectName().split("-")[1])
if this_button.text() == "下载完成":
# self.get_update_progressbar_signal({"end":0},click_button_time_stamp)
self.dw_index+=1
if self.dw_index == len(self.download_list):
QMessageLogUtilClass().show("提示","没有可下载的")
return
this_horizontalLayout_dict = self.download_list[self.dw_index]
# self.log.error(this_horizontalLayout_dict['send_signal_dict'])
this_horizontalLayout_layout_dict = this_horizontalLayout_dict['this_horizontalLayout_layout_dict']
this_horizontalLayout = this_horizontalLayout_layout_dict['horizontalLayout']
this_button = this_horizontalLayout_layout_dict['start_or_pause_button']
click_button_time_stamp = int(this_button.objectName().split("-")[1])
except Exception as e:
self.log.error(e)
# return
# print(self.thread_manager_dict[str(click_button_time_stamp)+'-download_thread']['thread'])
# return
#获取当前下载状态dict
download_status_info_dict = this_horizontalLayout_dict['download_status_info_dict']
is_Start = download_status_info_dict['is_Start']
is_Paused = download_status_info_dict['is_Paused']
is_End = download_status_info_dict['is_End']
#检测文件是否存在
save_path = this_horizontalLayout_dict['prime_download_info_dict']['save_path']
temp_video_save_path = save_path.strip('mp4')+"tempmp4"
temp_audio_save_path = save_path +"tempmp3"
# temp_audio_save_path = save_path.replace(' ','_')
if os.path.exists(save_path) or os.path.exists(temp_video_save_path) or os.path.exists(temp_audio_save_path):
self.log.warning(f"文件{save_path}已经存在,将覆盖原文件")
# user_choice = QMessageLogUtilClass(set_cancel_button=True).show("提示","文件已经存在,是否覆盖原文件?")
if True: #不问了直接删除
if os.path.exists(save_path):
os.remove(save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
if os.path.exists(temp_video_save_path):
os.remove(temp_video_save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
if os.path.exists(temp_audio_save_path):
os.remove(temp_audio_save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
while(os.path.exists(save_path) or os.path.exists(temp_video_save_path) or os.path.exists(temp_audio_save_path)):
print("还没删除完")
else:
return
#判断
new_button_text = ""
download_thread = this_horizontalLayout_dict['download_thread'] #获取对应的下载线程
self.log.warning(is_Start)
if not is_Start: #说明没开始下载过
if is_Paused: #说明还是暂停状态
is_Start = True
is_Paused = False
new_button_text = "暂停下载"
else:
is_Start = True
is_Paused = False
new_button_text = "开始下载"
#如果is_start为false,说明还没开始下载,同时this_horizontal的download_thread开没有开启,即没有start
#所以则例需要start
# download_thread = this_horizontalLayout_dict['download_thread']
# download_thread.start() #最好别在这启动,因为线程检测到is_pause为true,就停止了
# self.pause_download_signal.connect(download_thread.get_pause_download_signal)
#只有开启了线程,才能执行run代码,也就是线程中运行代码
else:
if is_Paused: #说名是开始之后暂停的
is_Paused = False
new_button_text = "暂停下载"
else:
is_Paused = True
new_button_text = "继续下载"
if new_button_text == "":
return
#然后就是判断 new_button_text 是否为继续下载,如果是,表示暂停了下载,这里就需要重新更新一下下载线程
#这里的做法就是重新创建一个线程,放在一个方法中去执行 直接用 set_per_url_download_thread
self.log.warning(new_button_text)
if new_button_text == "继续下载":
#首先将原来的线程删除
index_in_download_list = this_horizontalLayout_dict["index_in_download_list"]
ds_download_thread_dict = self.thread_manager_dict[str(click_button_time_stamp) + "-download_thread"]
ds_download_thread = ds_download_thread_dict['thread']
ds_download_thread.terminate()
if ds_download_thread.isFinished():
self.log.debug("线程已经结束")
self.thread_manager_dict.pop(str(click_button_time_stamp) + "-download_thread")
ds_download_thread.deleteLater()
self.set_per_url_download_thread(index_in_download_list)
# this_horizontalLayout_dict['download_thread']
#然后设置一下按钮的text
this_horizontalLayout_layout_dict['start_or_pause_button'].setText(new_button_text)
#更新一下状态
new_status_dict = {
"is_Start":is_Start,
"is_End":is_End,
"is_Paused":is_Paused,
}
this_horizontalLayout_dict['download_status_info_dict'] = new_status_dict
if is_Paused:
#发送信号给下载线程,这里使用的click_index为horizontallayout的time_stamp唯一标识
self.pause_download_signal.emit({
"click_index": click_button_time_stamp,
"is_pause":True
})
#先发送暂停信号,再开启线程
return #直接return
else:
#发送开始信号,开始下载
self.pause_download_signal.emit({
"click_index": click_button_time_stamp,
"is_pause":False
})
self.log.warning("开始线程")
download_thread.start() #发送开始信号,开始下载
#更新按钮文本
self.log.error(f"开始设置buttonnew_button_text{new_button_text}")
this_button.setText(new_button_text)
return
def start_or_pause_download(self):
#先检查文件是否又savev_path
if self.save_dir == "":
self.log.error("请先设置保存路径")
QMessageLogUtilClass().show("提示","请选择保存地址")
return
#检测文件是否存在
sender = app.sender() # 获取发送信号的控件
if isinstance(sender, QPushButton):
button_name = sender.objectName()
print("当前点击的按钮:", button_name)
self.log.debug(f"self.thread_manager_dict的线程数量: {len(self.thread_manager_dict)}")
# print(self.thread_manager_dict[key]['thread'] for key in self.thread_manager_dict.keys())
# return
click_button_time_stamp = int(button_name.split("-")[1])
click_button_index_in_download_list = int(button_name.split("-")[-1])
print(self.thread_manager_dict[str(click_button_time_stamp)+'-download_thread']['thread'])
#根据索引获取horizontalLayout
this_horizontalLayout_dict = self.download_list[click_button_index_in_download_list]
# self.log.error(this_horizontalLayout_dict['send_signal_dict'])
# self.log.error(this_horizontalLayout_dict['send_signal_dict']['pause_download_signal'])
# send_pause_download_signal = this_horizontalLayout_dict['send_signal_dict']['pause_download_signal']
# self.log.error(send_pause_download_signal)
this_horizontalLayout_layout_dict = this_horizontalLayout_dict['this_horizontalLayout_layout_dict']
this_horizontalLayout = this_horizontalLayout_layout_dict['horizontalLayout']
this_button = this_horizontalLayout_layout_dict['start_or_pause_button']
# this_button.setText("暂停下载")
# return
#获取当前下载状态dict
download_status_info_dict = this_horizontalLayout_dict['download_status_info_dict']
is_Start = download_status_info_dict['is_Start']
is_Paused = download_status_info_dict['is_Paused']
is_End = download_status_info_dict['is_End']
#检测文件是否存在
save_path = this_horizontalLayout_dict['prime_download_info_dict']['save_path']
temp_video_save_path = save_path.strip('mp4')+"tempmp4"
temp_audio_save_path = save_path +"tempmp3"
# temp_audio_save_path = save_path.replace(' ','_')
if not is_Start:
if os.path.exists(save_path) or os.path.exists(temp_video_save_path) or os.path.exists(temp_audio_save_path):
self.log.warning(f"文件{save_path}已经存在,将覆盖原文件")
user_choice = QMessageLogUtilClass(set_cancel_button=True).show("提示","文件已经存在,是否覆盖原文件?")
if user_choice == QMessageBox.Ok:
if os.path.exists(save_path):
os.remove(save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
if os.path.exists(temp_video_save_path):
os.remove(temp_video_save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
if os.path.exists(temp_audio_save_path):
os.remove(temp_audio_save_path) #这里删除文件也需要时间,要注意开启下载线程的实际,避免冲突
while(os.path.exists(save_path) or os.path.exists(temp_video_save_path) or os.path.exists(temp_audio_save_path)):
print("还没删除完")
else:
return
#判断
new_button_text = ""
download_thread = this_horizontalLayout_dict['download_thread'] #获取对应的下载线程
if not is_Start: #说明没开始下载过
if is_Paused: #说明还是暂停状态
is_Start = True
is_Paused = False
new_button_text = "暂停下载"
else:
is_Start = True
is_Paused = False
new_button_text = "开始下载"
#如果is_start为false,说明还没开始下载,同时this_horizontal的download_thread开没有开启,即没有start
#所以则例需要start
# download_thread = this_horizontalLayout_dict['download_thread']
# download_thread.start() #最好别在这启动,因为线程检测到is_pause为true,就停止了
# self.pause_download_signal.connect(download_thread.get_pause_download_signal)
#只有开启了线程,才能执行run代码,也就是线程中运行代码
else:
if is_Paused: #说名是开始之后暂停的
is_Paused = False
new_button_text = "暂停下载"
else:
is_Paused = True
new_button_text = "继续下载"
if new_button_text == "":
return
#然后就是判断 new_button_text 是否为继续下载,如果是,表示暂停了下载,这里就需要重新更新一下下载线程
#这里的做法就是重新创建一个线程,放在一个方法中去执行 直接用 set_per_url_download_thread
if new_button_text == "继续下载":
#首先将原来的线程删除
index_in_download_list = this_horizontalLayout_dict["index_in_download_list"]
ds_download_thread_dict = self.thread_manager_dict[str(click_button_time_stamp) + "-download_thread"]
ds_download_thread = ds_download_thread_dict['thread']
ds_download_thread.progressChanged_signal.disconnect()
ds_download_thread.terminate()
if ds_download_thread.isFinished():
self.log.debug("线程已经结束")
self.thread_manager_dict.pop(str(click_button_time_stamp) + "-download_thread")
ds_download_thread.deleteLater()
del ds_download_thread
# print(self.thread_manager_dict[str(click_button_time_stamp) + "-download_thread"])
self.set_per_url_download_thread(index_in_download_list)
# this_horizontalLayout_dict['download_thread']
#然后设置一下按钮的text
this_horizontalLayout_layout_dict['start_or_pause_button'].setText(new_button_text)
#更新一下状态
new_status_dict = {
"is_Start":is_Start,
"is_End":is_End,
"is_Paused":is_Paused,
}
this_horizontalLayout_dict['download_status_info_dict'] = new_status_dict
if is_Paused:
#发送信号给下载线程,这里使用的click_index为horizontallayout的time_stamp唯一标识
self.pause_download_signal.emit({
"click_index": click_button_time_stamp,
"is_pause":True
})
#先发送暂停信号,再开启线程
return #直接return
else:
#发送开始信号,开始下载
self.pause_download_signal.emit({
"click_index": click_button_time_stamp,
"is_pause":False
})
download_thread.start() #发送开始信号,开始下载
#更新按钮文本
self.log.error(f"开始设置buttonnew_button_text{new_button_text}")
this_button.setText(new_button_text)
return
def delete_record(self):
sender = app.sender() # 获取发送信号的控件
print("delete_button被点击")
self.log.info(f"当前self.thread_mangeer_dic的长度为 {len(self.thread_manager_dict)}")
self.log.info(f"当前self.cllection的长度为 {len(self.collection_info_list)}")
#显示一下
for key in self.thread_manager_dict.keys():
print(key, (self.thread_manager_dict[key]['options']))
if isinstance(sender, QPushButton):
button_name = sender.objectName()
# print("当前点击的删除按钮:", button_name)
click_button_time_stamp = int(button_name.split("-")[1])
click_button_index_in_download_list = int(button_name.split("-")[-1])
self.log.debug(f"当前点击的删除按钮:{click_button_index_in_download_list}")
click_horizontalLayout_dict = self.download_list[click_button_index_in_download_list]
click_horizontalLayout = click_horizontalLayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
this_delete_button = click_horizontalLayout_dict['this_horizontalLayout_layout_dict']['delete_button']
this_delete_button.setEnabled(False)
#找到控件之后就可以pop了
self.download_list.pop(click_button_index_in_download_list)
del click_horizontalLayout_dict
self.delete_horizonlayout(click_horizontalLayout)
#然后将对应的download_thread也删除
for key,value in self.thread_manager_dict.items():
if int(float(str(key).split("-")[0])) == int(click_button_time_stamp):
thread_to_destroy = self.thread_manager_dict.get(key)['thread']
# del thread_to_destroy
del self.thread_manager_dict[key]
# 终止线程
thread_to_destroy.terminate()
# 检查线程的状态
if thread_to_destroy.isFinished():
self.log.error(f"线程{thread_to_destroy}已经结束")
# 销毁线程对象
thread_to_destroy.deleteLater()
break
#===============================================================================
# click_horizontalLayout_dict = self.download_list[click_button_index_in_download_list]
# click_horizontalLayout = click_horizontalLayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
# self.log.debug(f"当前点击的删除按钮:{click_button_index_in_download_list}")
# #找到之后就和义pop了
# # self.log.warning(f"之前的downloadlist:{self.download_list}")
# print(click_button_index_in_download_list)
# self.log.debug(f"当前下载列表长度为{len(self.download_list)}")
# self.download_list.pop(click_button_index_in_download_list)
# self.log.debug(f"当前下载列表长度为{len(self.download_list)}")
# # self.log.warning(f"之后的downloadlist:{self.download_list}")
# while click_horizontalLayout.count():
# item = click_horizontalLayout.takeAt(0)
# widget_item = item.widget()
# if isinstance(widget_item, QLabel):
# pass
# # print(widget_item.objectName())
# if widget_item:
# widget_item.deleteLater()
# # 删除布局对象
# del click_horizontalLayout
#========================================
else:
self.log.error("未找到当前按钮")
self.log.warning(f"当前下载列表i哦长度为{len(self.download_list)}")
#然后refresh一下downloadlist
options="refresh_download_list"
#然后开一个线程对horizontalLayout的dict进行完善,完善后再显示它
time_stamp = int(time.time())
utilclass_thread = UtilClass(index=time_stamp, download_list=self.download_list,\
options=options,)
#链接线程结束信号
utilclass_thread.REFRESH_DOWNLOAD_LIST_FINISH_SIGNAL.connect(self.get_refresh_download_list_signal)
# self.pause_download_signal.connect(utilclass_thread.temp_pause_download_signal) #这里单独设置与downloaad_theread的连接了
#加入线程manager
utilclass_thread_dict = {}
utilclass_thread_dict['options'] = "refresh_download_list"
utilclass_thread_dict['index'] = len(self.download_list)
utilclass_thread_dict['thread'] = utilclass_thread
utilclass_thread_dict['url'] = self.url
utilclass_thread_dict['timestamp'] = time_stamp #唯一标识
self.thread_manager_dict[str(time_stamp)+"-refresh_download_list_thread"] = utilclass_thread_dict
#然后开启线程
self.thread_manager_dict[str(time_stamp)+"-refresh_download_list_thread"]['thread'].start()
# self.log.debug(f"self.thread_manager_dict为: {self.thread_manager_dict}")
return
def delete_horizonlayout(self,horizonlayout):
pass
# click_horizontalLayout_dict = self.download_list[index_in_download_list]
# click_horizontalLayout = click_horizontalLayout_dict['this_horizontalLayout_layout_dict']['horizontalLayout']
# self.log.debug(f"当前点击的删除按钮:{index_in_download_list}")
#找到之后就和义pop了
# self.log.warning(f"之前的downloadlist:{self.download_list}")
# print(index_in_download_list)
# self.log.debug(f"当前下载列表长度为{len(self.download_list)}")
# self.download_list.pop(index_in_download_list)
# self.log.debug(f"当前下载列表长度为{len(self.download_list)}")
# self.log.warning(f"之后的downloadlist:{self.download_list}")
while horizonlayout.count():
item = horizonlayout.takeAt(0)
widget_item = item.widget()
if isinstance(widget_item, QLabel):
pass
# print(widget_item.objectName())
if widget_item:
self.log.warning(f"删除了{widget_item.objectName()}")
widget_item.deleteLater()
# 删除布局对象
del horizonlayout
#禁用所有控件,或者开启
def disable_or_enable_widgets(self, IS_DISABLE):
if isinstance(self, QWidget):
self.log.warning("开始禁用")
#先禁用几个按钮
self.single_url_parse_button.setEnabled(IS_DISABLE)
self.collection_url_parse_button.setEnabled(IS_DISABLE)
self.download_all_button.setEnabled(IS_DISABLE)
self.delete_all_button.setEnabled(IS_DISABLE)
self.select_save_location_button.setEnabled(IS_DISABLE)
#然后download_lsit中的button
for i, horizontaldict in enumerate(self.download_list):
horizonlayout_dict = horizontaldict['this_horizontalLayout_layout_dict']
start_or_pause_button = horizonlayout_dict['start_or_pause_button']
delete_button = horizonlayout_dict['delete_button']
start_or_pause_button.setEnabled(IS_DISABLE)
delete_button.setEnabled(IS_DISABLE)
# self.setEnabled(False)
#
# expression = QRegExp(u'horizontalLayout*')
# print(self.findChildren(QBoxLayout,"horizontalLayout"))
# for child in self.findChildren(QPushButton):
# print(child)
# if isinstance(child, QPushButton):
# print(f"找到button{child}")
# child.setEnabled(False)
# for i in range(self.count()):
# item = self.itemAt(i)
# item.setEnabled(False)
# return
#================
# if isinstance(item, QGroupBox):
# disable_widgets(item.layout())
# elif isinstance(item, QWidget):
# item.setEnabled(False)
# elif isinstance(item, QLabel):
# item.setEnabled(False)
# elif isinstance(item, QLineEdit):
# item.setEnabled(False)
# elif isinstance(item, QPushButton):
# item.setEnabled(False)
# else:
# disable_widgets(item.layout())
def enable_widgets(self):
if isinstance(self, QWidget):
# self.setEnabled(False)
for child in self.findChildren(QWidget):
if isinstance(child, QPushButton):
child.setEnabled(True)
def select_save_folder(self):
folder_path = QFileDialog.getExistingDirectory(self, "选择文件夹", "/")
# print(self)
if folder_path:
self.show_save_location_label.setText(folder_path)
self.save_dir = folder_path
# with open(os.path.join(abs_dir, "config.json"), "w", encoding='utf-8') as f:
# f.write(json.dumps({"save_dir": self.save_dir}))
else:
self.show_save_location_label.setText("未选择文件夹")
#这里需要修复一个bug,就是如果在解析的时候没有选择save_foler,也就是self.save_dir="",那么解析后的各个horizontallayout
#对应的下载save_dir就是文件名本身,所以这里需要对horizontallayou的save_dir进行一个检查更新
#考虑到又多个layout的情况,这里就用线程进行更新
time_stamp_of_util_thread = int(time.time())
util_thread =UtilClass(options="refresh_save_path", download_list=self.download_list,\
save_dir=self.save_dir, index=time_stamp_of_util_thread)
#将线程加入manager
util_thread_dict = {}
util_thread_dict['options'] = "download_thread"
util_thread_dict['index'] = "?"
util_thread_dict['thread'] = util_thread
util_thread_dict['url'] = ""
util_thread_dict['timestamp'] = time_stamp_of_util_thread #唯一标识
self.thread_manager_dict[str(time_stamp_of_util_thread)+'-'+"refresh_save_path_util_thread"] = util_thread_dict
util_thread.REFRESH_SAVE_PATH_FINISH_SIGNAL.connect(self.get_refresh_save_path_signal)
#这里需要先禁用一下所有按钮,等更新完保存未知后再开启
self.disable_or_enable_widgets(False)
util_thread.start()
def select_video_quality(self):
#检测文件是否存在
sender = app.sender() # 获取发送信号的控件
if isinstance(sender, QComboBox):
combobox_name = sender.objectName()
print("当前点击的combobox:", combobox_name)
# return
self.log.debug(f"self.thread_manager_dict的线程数量: {len(self.thread_manager_dict)}")
# print(self.thread_manager_dict[key]['thread'] for key in self.thread_manager_dict.keys())
# return
if len(combobox_name.split("-")) <2:
return
click_combobox_time_stamp = int(combobox_name.split("-")[1])
click_combobox_index_in_download_list = int(combobox_name.split("-")[-1])
combobox_current_text = sender.currentText()
print(combobox_current_text)
#重新获取baseUrl
this_horizontalLayout_dict = self.download_list[click_combobox_index_in_download_list]
download_info_dict = this_horizontalLayout_dict['download_info_dict']
id = -1
if combobox_current_text == "1080P":
id = 80
elif combobox_current_text == "720P":
id = 64
elif combobox_current_text == "480P":
id = 32
elif combobox_current_text == "360P":
id = 16
else:
self.log.error("未知的video_quality")
return
#然后获取baseUrl
download_url_dict = download_info_dict['download_url_dict']
video_dw_url_list = download_url_dict['video_dw_url_list']
# print(video_dw_url_list)
baseUrl = ""
for dw in video_dw_url_list:
if dw['id'] == id:
baseUrl = dw['baseUrl']
break
if baseUrl == "":
self.log.error("baseUrl为空")
return
# return
#然后就是更新prime_download_info_dict
prime_download_info_dict = this_horizontalLayout_dict['prime_download_info_dict']
prime_download_info_dict['download_video_url'] = baseUrl
#需要重新获取一下total_size,只获取video的即可
video_total_size = DownloadUtilClass().get_single_file_total_size(baseUrl)
prime_download_info_dict['total_video_size'] = video_total_size
prime_download_info_dict['total_size'] = prime_download_info_dict['total_video_size'] + \