diff --git a/app/modules/qbittorrent/qbittorrent.py b/app/modules/qbittorrent/qbittorrent.py index b0acaf14..54746ba5 100644 --- a/app/modules/qbittorrent/qbittorrent.py +++ b/app/modules/qbittorrent/qbittorrent.py @@ -20,10 +20,16 @@ class Qbittorrent(metaclass=Singleton): qbc: Client = None - def __init__(self): - self._host, self._port = StringUtils.get_domain_address(address=settings.QB_HOST, prefix=True) - self._username = settings.QB_USER - self._password = settings.QB_PASSWORD + def __init__(self, host: str = None, port: int = None, username: str = None, password: str = None): + """ + 若不设置参数,则创建配置文件设置的下载器 + """ + if host and port: + self._host, self._port = host, port + else: + self._host, self._port = StringUtils.get_domain_address(address=settings.QB_HOST, prefix=True) + self._username = username if username else settings.QB_USER + self._password = password if password else settings.QB_PASSWORD if self._host and self._port: self.qbc = self.__login_qbittorrent() diff --git a/app/modules/transmission/transmission.py b/app/modules/transmission/transmission.py index fa7b9bd8..4824df33 100644 --- a/app/modules/transmission/transmission.py +++ b/app/modules/transmission/transmission.py @@ -24,10 +24,16 @@ class Transmission(metaclass=Singleton): "peersGettingFromUs", "peersSendingToUs", "uploadRatio", "uploadedEver", "downloadedEver", "downloadDir", "error", "errorString", "doneDate", "queuePosition", "activityDate", "trackers"] - def __init__(self): - self._host, self._port = StringUtils.get_domain_address(address=settings.TR_HOST, prefix=False) - self._username = settings.TR_USER - self._password = settings.TR_PASSWORD + def __init__(self, host: str = None, port: int = None, username: str = None, password: str = None): + """ + 若不设置参数,则创建配置文件设置的下载器 + """ + if host and port: + self._host, self._port = host, port + else: + self._host, self._port = StringUtils.get_domain_address(address=settings.TR_HOST, prefix=False) + self._username = username if username else settings.TR_USER + self._password = password if password else settings.TR_PASSWORD if self._host and self._port: self.trc = self.__login_transmission() diff --git a/app/utils/singleton.py b/app/utils/singleton.py index a6bf423d..908b2ffd 100644 --- a/app/utils/singleton.py +++ b/app/utils/singleton.py @@ -9,9 +9,10 @@ class Singleton(abc.ABCMeta, type): _instances: dict = {} def __call__(cls, *args, **kwargs): - if cls not in cls._instances: - cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs) - return cls._instances[cls] + key = (cls, args, frozenset(kwargs.items())) + if key not in cls._instances: + cls._instances[key] = super().__call__(*args, **kwargs) + return cls._instances[key] class AbstractSingleton(abc.ABC, metaclass=Singleton):