The only downloader you need. 下载器的集大成者。
TypeScript asyncasynciobittorentbtcross-platformdownload-managerdownloaderfluent-designftphttp3magnetpyqtpyside6pythonqtquicreactsoftwarestreaming
优化文件名获取逻辑,修改路径获取方式,修改requirements.txt
XiaoYouChRc4cb54694231
diff --git a/app/common/config.py b/app/common/config.pyindex 05967d43..46080a37 100644--- a/app/common/config.py+++ b/app/common/config.py@@ -1,6 +1,7 @@# coding:utf-8from PySide6.QtCore import QDir+from PySide6.QtWidgets import QApplicationfrom qfluentwidgets import (qconfig, QConfig, ConfigItem, OptionsConfigItem, BoolValidator,OptionsValidator, RangeConfigItem, RangeValidator,FolderValidator)@@ -37,4 +38,4 @@ FEEDBACK_URL = "https://github.com/XiaoYouChR/Ghost-Downloader-3/issues"cfg = Config()-qconfig.load('./Ghost Downloader 配置文件.json', cfg)\ No newline at end of file+qconfig.load('{}/Ghost Downloader 配置文件.json'.format(QApplication.applicationDirPath()), cfg)\ No newline at end of filediff --git a/app/common/download_task.py b/app/common/download_task.pyindex c0cad1a7..799cca97 100644--- a/app/common/download_task.py+++ b/app/common/download_task.py@@ -111,6 +111,13 @@ class DownloadTask(QThread):match = re.search(r'filename\s*=\s*["\']?([^"\';]+)["\']?', headerValue, re.IGNORECASE)if match:fileName = match.group(1)++ # 移除文件名头尾可能存在的引号+ if fileName:+ fileName = fileName.strip('"\'')+ else:+ raise KeyError+logger.debug(f"方法1获取文件名成功, 文件名:{fileName}")except (KeyError, IndexError) as e:try:@@ -126,11 +133,12 @@ class DownloadTask(QThread):fileName = fileName[1:-1]elif fileName.startswith("'") and fileName.endswith("'"):fileName = fileName[1:-1]- logger.debug(f"方法2获取文件名成功, 文件名:{fileName}")if not fileName:raise KeyError+ logger.debug(f"方法2获取文件名成功, 文件名:{fileName}")+except (KeyError, IndexError) as e:# 处理没有文件名的情况logger.info(f"方法2获取文件名失败, KeyError or IndexError:{e}")diff --git a/app/common/methods.py b/app/common/methods.pyindex ac574378..7bd07caa 100644--- a/app/common/methods.py+++ b/app/common/methods.py@@ -3,6 +3,7 @@ import inspectimport osimport sys+from PySide6.QtWidgets import QApplicationfrom loguru import loggerfrom app.common.plugin_base import PluginBase@@ -12,7 +13,7 @@ plugins = []# def isWin11():# return sys.platform == 'win32' and sys.getwindowsversion().build >= 22000-def loadPlugins(mainWindow, directory="./plugins"):+def loadPlugins(mainWindow, directory="{}/plugins".format(QApplication.applicationDirPath())):try:for filename in os.listdir(directory):if filename.endswith(".py"):diff --git a/app/components/Ui_TaskCard.py b/app/components/Ui_TaskCard.pyindex 4b77b9fe..987708bd 100644--- a/app/components/Ui_TaskCard.py+++ b/app/components/Ui_TaskCard.py@@ -12,7 +12,7 @@ from PySide6.QtCore import (QCoreApplication, QMetaObject, QSize, Qt)from PySide6.QtWidgets import (QHBoxLayout, QSizePolicy, QSpacerItem,QVBoxLayout)-from qfluentwidgets import (BodyLabel, PixmapLabel, TitleLabel, ToolButton, PrimaryToolButton, DropDownToolButton, RoundMenu, Action)+from qfluentwidgets import (BodyLabel, PixmapLabel, TitleLabel, ToolButton, RoundMenu, Action, PrimaryDropDownToolButton)from qfluentwidgets import FluentIcon as FIF@@ -64,13 +64,27 @@ class Ui_TaskCard(object):self.horizontalLayout.addWidget(self.pauseButton)+ self.folderButton = ToolButton(TaskCard)+ self.folderButton.setObjectName(u"folderButton")+ self.folderButton.setMinimumSize(QSize(31, 31))+ self.folderButton.setMaximumSize(QSize(31, 31))++ self.horizontalLayout.addWidget(self.folderButton)++ self.fileButton = ToolButton(TaskCard)+ self.fileButton.setObjectName(u"folderButton")+ self.fileButton.setMinimumSize(QSize(31, 31))+ self.fileButton.setMaximumSize(QSize(31, 31))++ self.horizontalLayout.addWidget(self.fileButton)+self.Menu = RoundMenu(parent=self)self.delAction = Action(FIF.DELETE, "删除")self.completelyDelAction = Action(FIF.CLOSE, "彻底删除")self.Menu.addAction(self.delAction)self.Menu.addAction(self.completelyDelAction)- self.cancelButton = DropDownToolButton(TaskCard)+ self.cancelButton = PrimaryDropDownToolButton(TaskCard)self.cancelButton.setObjectName(u"cancelButton")self.cancelButton.setMenu(self.Menu)self.cancelButton.setMinimumSize(QSize(61, 31))@@ -78,13 +92,6 @@ class Ui_TaskCard(object):self.horizontalLayout.addWidget(self.cancelButton)- self.folderButton = PrimaryToolButton(TaskCard)- self.folderButton.setObjectName(u"folderButton")- self.folderButton.setMinimumSize(QSize(31, 31))- self.folderButton.setMaximumSize(QSize(31, 31))-- self.horizontalLayout.addWidget(self.folderButton)-self.verticalLayout.addLayout(self.horizontalLayout)self.horizontalLayout_2 = QHBoxLayout()@@ -123,6 +130,7 @@ class Ui_TaskCard(object):self.pauseButton.setIcon(FIF.PAUSE)self.cancelButton.setIcon(FIF.DELETE)self.folderButton.setIcon(FIF.FOLDER)+ self.fileButton.setIcon(FIF.LIBRARY)self.retranslateUi(TaskCard)@@ -135,5 +143,6 @@ class Ui_TaskCard(object):self.pauseButton.setText("")self.cancelButton.setText("")self.folderButton.setText("")+ self.fileButton.setText("")self.processLabel.setText("")# retranslateUidiff --git a/app/components/task_card.py b/app/components/task_card.pyindex 2b60506c..3e1af034 100644--- a/app/components/task_card.py+++ b/app/components/task_card.py@@ -7,7 +7,7 @@ from time import sleepfrom PySide6.QtCore import QThread, Signal, QFileInfofrom PySide6.QtGui import QPixmap-from PySide6.QtWidgets import QFileIconProvider+from PySide6.QtWidgets import QFileIconProvider, QApplicationfrom loguru import loggerfrom qfluentwidgets import CardWidgetfrom qfluentwidgets import FluentIcon as FIF@@ -109,10 +109,11 @@ class TaskCard(CardWidget, Ui_TaskCard):self.folderButton.clicked.connect(lambda: os.startfile(path))else: # Linux 下打开文件夹self.folderButton.clicked.connect(lambda: os.system(f"xdg-open {path}"))+ self.fileButton.clicked.connect(lambda :os.system(f"{path}/{self.fileName}"))# 写入未完成任务记录文件,以供下次打开时继续下载if not autoCreated:- with open("./Ghost Downloader 记录文件", "a", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "a", encoding="utf-8") as f:_ = {"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),"blockNum": self.maxBlockNum, "status": self.status}f.write(str(_) + "\n")@@ -137,7 +138,7 @@ class TaskCard(CardWidget, Ui_TaskCard):self.task.terminate()# 改变记录状态- with open("./Ghost Downloader 记录文件", "r", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "r", encoding="utf-8") as f:_ = f.read()_ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),@@ -145,7 +146,7 @@ class TaskCard(CardWidget, Ui_TaskCard):str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),"blockNum": self.maxBlockNum, "status": "paused"}) + "\n")- with open("./Ghost Downloader 记录文件", "w", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "w", encoding="utf-8") as f:f.write(_)self.speedLable.setText("任务已经暂停")@@ -161,7 +162,7 @@ class TaskCard(CardWidget, Ui_TaskCard):self.task.taskFinished.connect(self.taskFinished)# 改变记录状态- with open("./Ghost Downloader 记录文件", "r", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "r", encoding="utf-8") as f:_ = f.read()_ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),@@ -169,7 +170,7 @@ class TaskCard(CardWidget, Ui_TaskCard):str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),"blockNum": self.maxBlockNum, "status": "working"}) + "\n")- with open("./Ghost Downloader 记录文件", "w", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "w", encoding="utf-8") as f:f.write(_)self.speedLable.setText("任务正在开始")@@ -206,13 +207,13 @@ class TaskCard(CardWidget, Ui_TaskCard):sleep(0.1)# 删除记录文件- with open("./Ghost Downloader 记录文件", "r", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "r", encoding="utf-8") as f:_ = f.read()_ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),"blockNum": self.maxBlockNum, "status": self.status}) + "\n", "")- with open("./Ghost Downloader 记录文件", "w", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "w", encoding="utf-8") as f:f.write(_)self.status = "canceled"@@ -257,7 +258,7 @@ class TaskCard(CardWidget, Ui_TaskCard):if not self.status == "finished": # 不是自动创建的已完成任务# 改变记录状态- with open("./Ghost Downloader 记录文件", "r", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "r", encoding="utf-8") as f:_ = f.read()_ = _.replace(str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),@@ -265,7 +266,7 @@ class TaskCard(CardWidget, Ui_TaskCard):str({"url": self.url, "fileName": self.fileName, "filePath": str(self.filePath),"blockNum": self.maxBlockNum, "status": "finished"}) + "\n")- with open("./Ghost Downloader 记录文件", "w", encoding="utf-8") as f:+ with open("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()), "w", encoding="utf-8") as f:f.write(_)# 再获取一次图标diff --git a/app/view/main_window.py b/app/view/main_window.pyindex 55a20a28..6e6e411a 100644--- a/app/view/main_window.py+++ b/app/view/main_window.py@@ -47,7 +47,7 @@ class MainWindow(MSFluentWindow):self.themeChangedListener.start()# 创建未完成的任务- historyFile = Path("./Ghost Downloader 记录文件")+ historyFile = Path("{}/Ghost Downloader 记录文件".format(QApplication.applicationDirPath()))# 未完成任务记录文件格式示例: [{"url": "xxx", "fileName": "xxx", "filePath": "xxx", "blockNum": x, "status": "xxx"}]if historyFile.exists():with open(historyFile, 'r', encoding='utf-8') as f:diff --git a/app/view/setting_interface.py b/app/view/setting_interface.pyindex 500d8cd2..347d56e2 100644--- a/app/view/setting_interface.py+++ b/app/view/setting_interface.py@@ -3,7 +3,7 @@ import sysfrom PySide6.QtCore import Qt, Signal, QUrl, QResourcefrom PySide6.QtGui import QDesktopServices-from PySide6.QtWidgets import QWidget, QFileDialog, QVBoxLayout+from PySide6.QtWidgets import QWidget, QFileDialog, QVBoxLayout, QApplicationfrom qfluentwidgets import FluentIcon as FIF, InfoBarPositionfrom qfluentwidgets import InfoBarfrom qfluentwidgets import (SettingCardGroup, SwitchSettingCard, OptionsSettingCard, PushSettingCard,@@ -242,7 +242,7 @@ class SettingInterface(ScrollArea):key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Run', 0, winreg.KEY_WRITE)winreg.SetValueEx(key, 'GhostDownloader', 0, winreg.REG_SZ,- f'"{sys.argv[0]}" --silence')+ '"{}" --silence'.format(QApplication.applicationFilePath().replace("/", "\\")))else:key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r'Software\Microsoft\Windows\CurrentVersion\Run', 0, winreg.KEY_WRITE)diff --git a/main.py b/main.pyindex e5be19d6..d26dd3aa 100644--- a/main.py+++ b/main.py@@ -50,64 +50,63 @@ app.setAttribute(Qt.AA_DontCreateNativeWidgetSiblings)setTheme(Theme.DARK if darkdetect.isDark() else Theme.LIGHT, save=False)# Get Theme Color-# Windows Only-if sys.platform == "win32":- try:- # 定义用于获取主题色的函数-- ctypes.windll.dwmapi.DwmGetColorizationColor.restype = ctypes.c_ulong- ctypes.windll.dwmapi.DwmGetColorizationColor.argtypes = [ctypes.POINTER(ctypes.c_ulong),- ctypes.POINTER(ctypes.c_bool)]-- color = ctypes.c_ulong()- opaque = ctypes.c_bool()-- # 获取主题颜色值- ctypes.windll.dwmapi.DwmGetColorizationColor(ctypes.byref(color), ctypes.byref(opaque))-- # 将颜色值转换为RGB元组- b, g, r = color.value % 256, (color.value >> 8) % 256, (color.value >> 16) % 256-- setThemeColor(QColor(r, g, b), save=False)-- except Exception as e:- logger.error(f"Cannot get theme color: {e}")-elif sys.platform == "darwin": # macOS Only- # 咱就是说为什么苹果要把开发者文档做成英文,出个中文版不好吗?- # TM的让我找得好苦…… - By YHX- import objc # PyObjC- from Foundation import NSBundle-- # 加载AppKit框架- AppKit = NSBundle.bundleWithIdentifier_('com.apple.AppKit')-- # 获取NSColor类- NSColor = AppKit.classNamed_('NSColor')- NSColorSpace = AppKit.classNamed_('NSColorSpace')-- # 获取当前主题色- theme_color = NSColor.controlAccentColor() #md就是这个API让我找了好久……- # 欸,这时还不能用,因为现在这是NSColor Catalog color,还要转换!- color = theme_color.colorUsingColorSpace_(NSColorSpace.sRGBColorSpace())- # 获取颜色的 RGB 分量- red = color.redComponent()- green = color.greenComponent()- blue = color.blueComponent()-- # 将颜色分量转换为 0-255 范围- red = int(red * 255)- green = int(green * 255)- blue = int(blue * 255)+try:+ # Windows Only+ if sys.platform == "win32":+ # 定义用于获取主题色的函数- setThemeColor(QColor(red, green, blue), save=False)-else:- logger.warning("Cannot get theme color on this platform: "+sys.platform)+ ctypes.windll.dwmapi.DwmGetColorizationColor.restype = ctypes.c_ulong+ ctypes.windll.dwmapi.DwmGetColorizationColor.argtypes = [ctypes.POINTER(ctypes.c_ulong),+ ctypes.POINTER(ctypes.c_bool)]++ color = ctypes.c_ulong()+ opaque = ctypes.c_bool()++ # 获取主题颜色值+ ctypes.windll.dwmapi.DwmGetColorizationColor(ctypes.byref(color), ctypes.byref(opaque))++ # 将颜色值转换为RGB元组+ b, g, r = color.value % 256, (color.value >> 8) % 256, (color.value >> 16) % 256++ setThemeColor(QColor(r, g, b), save=False)++ elif sys.platform == "darwin": # macOS Only+ # 咱就是说为什么苹果要把开发者文档做成英文,出个中文版不好吗?+ # TM的让我找得好苦…… - By YHX (#17)+ import objc # PyObjC+ from Foundation import NSBundle++ # 加载AppKit框架+ AppKit = NSBundle.bundleWithIdentifier_('com.apple.AppKit')++ # 获取NSColor类+ NSColor = AppKit.classNamed_('NSColor')+ NSColorSpace = AppKit.classNamed_('NSColorSpace')++ # 获取当前主题色+ color = NSColor.controlAccentColor() #md就是这个API让我找了好久……+ # 欸,这时还不能用,因为现在这是NSColor Catalog color,还要转换!+ color = color.colorUsingColorSpace_(NSColorSpace.sRGBColorSpace())+ # 获取颜色的 RGB 分量+ red = color.redComponent()+ green = color.greenComponent()+ blue = color.blueComponent()++ # 将颜色分量转换为 0-255 范围+ red = int(red * 255)+ green = int(green * 255)+ blue = int(blue * 255)++ setThemeColor(QColor(red, green, blue), save=False)+except Exception as e:+ logger.error(f"Cannot get theme color: {e}")# create main windoww = MainWindow()# loading plugins-pluginsPath=os.path.join(os.path.dirname(__file__), "plugins")+# pluginsPath=os.path.join(app.applicationDirPath(), "plugins")+pluginsPath=("./plugins")loadPlugins(w, pluginsPath)try: # 静默启动diff --git a/requirements.txt b/requirements.txtindex b260f3d1..2e33df55 100644--- a/requirements.txt+++ b/requirements.txt@@ -1,5 +1,5 @@PySide6-Fluent-Widgets>=1.1.8-PySide6>=6.4.2+PySide6<=6.6.3.1loguru>=0.7.2httpx>=0.27.0-pyobjc>=10.3.1+pyobjc>=10.3.1; sys_platform == 'darwin'