The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
增加稍后下载功能, 为增加下载任务功能增加错误捕获, 优化开始下载按钮的启用时间
XiaoYouChRde72e996cad8
diff --git a/app/components/Ui_AddTaskOptionDialog.py b/app/components/Ui_AddTaskOptionDialog.pyindex da2e8e51..e187ec2a 100644--- a/app/components/Ui_AddTaskOptionDialog.py+++ b/app/components/Ui_AddTaskOptionDialog.py@@ -10,9 +10,11 @@from PySide6.QtCore import (QCoreApplication, QMetaObject, QSize, Qt)from PySide6.QtWidgets import (QHBoxLayout, QSizePolicy, QTableWidgetItem, QVBoxLayout, QApplication, QHeaderView)+from qfluentwidgets import FluentIcon as FIF+from qfluentwidgets import (PushButton, SubtitleLabel,+ TableWidget, TextEdit, RoundMenu, Action)+from qfluentwidgets.components.widgets.button import PrimarySplitPushButton-from qfluentwidgets import (PrimaryPushButton, PushButton, SubtitleLabel,- TableWidget, TextEdit)class DisabledRichTextEdit(TextEdit):def __init__(self, parent=None):@@ -96,17 +98,28 @@ class Ui_AddTaskOptionDialog(object):self.noButton = PushButton(AddTaskOptionDialog)self.noButton.setObjectName(u"noButton")- self.buttonLayout.addWidget(self.noButton)+ self.buttonLayout.addWidget(self.noButton, stretch=1)- self.yesButton = PrimaryPushButton(AddTaskOptionDialog)+ self.laterMenu = RoundMenu(parent=AddTaskOptionDialog)+ self.laterMenu.setObjectName(u"laterMenu")+ self.laterAction = Action(FIF.STOP_WATCH, "稍后下载")+ self.laterMenu.addAction(self.laterAction)++ self.yesButton = PrimarySplitPushButton(AddTaskOptionDialog)self.yesButton.setObjectName(u"yesButton")- self.yesButton.setEnabled(False)- self.buttonLayout.addWidget(self.yesButton)+ # Fix PyQt-Fluent-Widgets Bug+ self.yesButton.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)+ _ = self.yesButton.hBoxLayout.takeAt(0).widget()+ _.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)+ self.yesButton.hBoxLayout.insertWidget(0, _)+ self.yesButton.setEnabled(False)+ self.yesButton.setFlyout(self.laterMenu)- self.verticalLayout.addLayout(self.buttonLayout)+ self.buttonLayout.addWidget(self.yesButton, stretch=1)+ self.verticalLayout.addLayout(self.buttonLayout)self.retranslateUi(AddTaskOptionDialog)diff --git a/app/components/add_task_dialog.py b/app/components/add_task_dialog.pyindex b46450ff..23b1388d 100644--- a/app/components/add_task_dialog.py+++ b/app/components/add_task_dialog.py@@ -1,7 +1,7 @@import osimport re-import threadingfrom pathlib import Path+from threading import Threadfrom PySide6.QtCore import Signal, Qt, QTimerfrom PySide6.QtGui import QColor@@ -37,6 +37,7 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):startSignal = Signal()__addTableRowSignal = Signal(str, str, str) # fileName, fileSize, Url, 同理因为int最大值仅支持到2^31 PyQt无法定义int64 故只能使用str代替+ __gotWrong = Signal(str, int) # error, indexdef __init__(self, parent=None):super().__init__(parent=parent)@@ -82,9 +83,23 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):self.noButton.clicked.connect(self.close)self.yesButton.clicked.connect(self.__onYesButtonClicked)+ self.laterAction.triggered.connect(self.__onLaterActionTriggered)self.taskTableWidget.itemChanged.connect(self.__onTaskTableWidgetItemChanged)self.linkTextEdit.textChanged.connect(self.__onLinkTextChanged)self.__addTableRowSignal.connect(self.__addTableRow)+ self.__gotWrong.connect(self.__handleWrong)++ def __handleWrong(self, error: str, index: int):+ InfoBar.error(+ title='错误',+ content=f"解析第 {index} 个链接时遇到错误: {error}",+ orient=Qt.Horizontal,+ isClosable=True,+ position=InfoBarPosition.TOP,+ # position='Custom', # NOTE: use custom info bar manager+ duration=10000,+ parent=self.parent()+ )def __onYesButtonClicked(self):path = Path(self.downloadFolderCard.contentLabel.text())@@ -108,6 +123,28 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):self.close()+ def __onLaterActionTriggered(self):+ path = Path(self.downloadFolderCard.contentLabel.text())++ # 检测路径是否有权限写入+ if not path.exists():+ try:+ path.mkdir(parents=True, exist_ok=True)+ except Exception as e:+ MessageBox("错误", str(e), self)+ else:+ if not os.access(path, os.W_OK):+ MessageBox("错误", "似乎是没有权限向此目录写入文件", self)++ for i in range(self.taskTableWidget.rowCount()):+ item = self.taskTableWidget.item(i, 0)++ signalBus.addTaskSignal.emit(item.data(1),+ str(path), self.blockNumCard.configItem.value,+ item.text(), "paused", False)++ self.close()+def __onDownloadFolderCardClicked(self):""" download folder card clicked slot """folder = QFileDialog.getExistingDirectory(@@ -127,10 +164,14 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):self._timer.timeout.connect(self.__processTextChange)self._timer.start(1000) # 1秒后处理- def __handleUrl(self, url: str):- _url, fileName, fileSize = getLinkInfo(url, Headers)+ def __handleUrl(self, url: str, index: int):+ try:+ _url, fileName, fileSize = getLinkInfo(url, Headers)+ self.__addTableRowSignal.emit(fileName, str(fileSize), url) # 不希望使用重定向后的url,故使用原始url++ except Exception as e:+ self.__gotWrong.emit(repr(e), index)- self.__addTableRowSignal.emit(fileName, str(fileSize), url) # 不希望使用重定向后的url,故使用原始urldef __addTableRow(self, fileName: str, fileSize: str, url: str):""" add table row slot """@@ -154,6 +195,9 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):""" link text changed slot """# 清除所有行self.taskTableWidget.setRowCount(0)+ self.threads = []++ self.yesButton.setEnabled(False)text: list = self.linkTextEdit.toPlainText().split("\n")@@ -162,8 +206,7 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):_ = urlRe.search(url)if _:- self.yesButton.setEnabled(True)- threading.Thread(target=self.__handleUrl, args=(url,), daemon=True).start()+ self.threads.append(Thread(target=self.__handleUrl, args=(url, index), daemon=True))else:InfoBar.warning(@@ -176,3 +219,16 @@ class AddTaskOptionDialog(MaskDialogBase, Ui_AddTaskOptionDialog):duration=1000,parent=self.parent())++ if self.threads:+ for thread in self.threads:+ thread.start()++ Thread(target=self.__waitForThreads, daemon=True).start()++ def __waitForThreads(self):+ for thread in self.threads:+ thread.join()++ if self.taskTableWidget.rowCount() >= 0:+ self.yesButton.setEnabled(True)\ No newline at end of file