From 7eb77875f1b43162c157779275342f503a71aa88 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Sun, 3 Sep 2023 21:59:18 +0800 Subject: [PATCH] =?UTF-8?q?fix=20=E9=87=8D=E8=BF=9E=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/modules/emby/__init__.py | 2 +- app/modules/emby/emby.py | 8 ++++++++ app/modules/jellyfin/__init__.py | 2 +- app/modules/jellyfin/jellyfin.py | 8 ++++++++ app/modules/plex/__init__.py | 2 +- app/modules/plex/plex.py | 8 ++++++++ app/modules/qbittorrent/__init__.py | 2 +- app/modules/qbittorrent/qbittorrent.py | 8 ++++++++ app/modules/transmission/__init__.py | 2 +- app/modules/transmission/transmission.py | 10 +++++++++- 10 files changed, 46 insertions(+), 6 deletions(-) diff --git a/app/modules/emby/__init__.py b/app/modules/emby/__init__.py index f1b1846b..4ec72409 100644 --- a/app/modules/emby/__init__.py +++ b/app/modules/emby/__init__.py @@ -28,7 +28,7 @@ class EmbyModule(_ModuleBase): 定时任务,每10分钟调用一次 """ # 定时重连 - if not self.emby.user: + if not self.emby.is_inactive(): self.emby = Emby() def user_authenticate(self, name: str, password: str) -> Optional[str]: diff --git a/app/modules/emby/emby.py b/app/modules/emby/emby.py index 2ff214dd..ffd1cc70 100644 --- a/app/modules/emby/emby.py +++ b/app/modules/emby/emby.py @@ -27,6 +27,14 @@ class Emby(metaclass=Singleton): self.user = self.get_user() self.folders = self.get_emby_folders() + def is_inactive(self) -> bool: + """ + 判断是否需要重连 + """ + if not self._host or not self._apikey: + return False + return True if not self.user else False + def get_emby_folders(self) -> List[dict]: """ 获取Emby媒体库路径列表 diff --git a/app/modules/jellyfin/__init__.py b/app/modules/jellyfin/__init__.py index 52127897..ed77cc5e 100644 --- a/app/modules/jellyfin/__init__.py +++ b/app/modules/jellyfin/__init__.py @@ -25,7 +25,7 @@ class JellyfinModule(_ModuleBase): 定时任务,每10分钟调用一次 """ # 定时重连 - if not self.jellyfin.user: + if not self.jellyfin.is_inactive(): self.jellyfin = Jellyfin() def stop(self): diff --git a/app/modules/jellyfin/jellyfin.py b/app/modules/jellyfin/jellyfin.py index 47061cc4..a9906d51 100644 --- a/app/modules/jellyfin/jellyfin.py +++ b/app/modules/jellyfin/jellyfin.py @@ -25,6 +25,14 @@ class Jellyfin(metaclass=Singleton): self.user = self.get_user() self.serverid = self.get_server_id() + def is_inactive(self) -> bool: + """ + 判断是否需要重连 + """ + if not self._host or not self._apikey: + return False + return True if not self.user else False + def __get_jellyfin_librarys(self) -> List[dict]: """ 获取Jellyfin媒体库的信息 diff --git a/app/modules/plex/__init__.py b/app/modules/plex/__init__.py index 403a8c51..8ff8a862 100644 --- a/app/modules/plex/__init__.py +++ b/app/modules/plex/__init__.py @@ -28,7 +28,7 @@ class PlexModule(_ModuleBase): 定时任务,每10分钟调用一次 """ # 定时重连 - if not self.plex.get_plex(): + if not self.plex.is_inactive(): self.plex = Plex() def webhook_parser(self, body: Any, form: Any, args: Any) -> WebhookEventInfo: diff --git a/app/modules/plex/plex.py b/app/modules/plex/plex.py index a0b56317..cff6c789 100644 --- a/app/modules/plex/plex.py +++ b/app/modules/plex/plex.py @@ -30,6 +30,14 @@ class Plex(metaclass=Singleton): self._plex = None logger.error(f"Plex服务器连接失败:{str(e)}") + def is_inactive(self) -> bool: + """ + 判断是否需要重连 + """ + if not self._host or not self._token: + return False + return True if not self._plex else False + def get_librarys(self): """ 获取媒体服务器所有媒体库列表 diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 96d2d64e..d6845f31 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -33,7 +33,7 @@ class QbittorrentModule(_ModuleBase): 定时任务,每10分钟调用一次 """ # 定时重连 - if not self.qbittorrent.qbc: + if self.qbittorrent.is_inactive(): self.qbittorrent = Qbittorrent() def download(self, torrent_path: Path, download_dir: Path, cookie: str, diff --git a/app/modules/qbittorrent/qbittorrent.py b/app/modules/qbittorrent/qbittorrent.py index 98279abb..8ed72504 100644 --- a/app/modules/qbittorrent/qbittorrent.py +++ b/app/modules/qbittorrent/qbittorrent.py @@ -27,6 +27,14 @@ class Qbittorrent(metaclass=Singleton): if self._host and self._port: self.qbc = self.__login_qbittorrent() + def is_inactive(self) -> bool: + """ + 判断是否需要重连 + """ + if not self._host or not self._port: + return False + return True if not self.qbc else False + def __login_qbittorrent(self) -> Optional[Client]: """ 连接qbittorrent diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index 2d6f7ad1..f3f71ddb 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -33,7 +33,7 @@ class TransmissionModule(_ModuleBase): 定时任务,每10分钟调用一次 """ # 定时重连 - if not self.transmission.trc: + if not self.transmission.is_inactive(): self.transmission = Transmission() def download(self, torrent_path: Path, download_dir: Path, cookie: str, diff --git a/app/modules/transmission/transmission.py b/app/modules/transmission/transmission.py index 718bb5e7..e9013b4e 100644 --- a/app/modules/transmission/transmission.py +++ b/app/modules/transmission/transmission.py @@ -28,7 +28,7 @@ class Transmission(metaclass=Singleton): self._host, self._port = StringUtils.get_domain_address(address=settings.TR_HOST, prefix=False) self._username = settings.TR_USER self._password = settings.TR_PASSWORD - if self._host and self._port and self._username and self._password: + if self._host and self._port: self.trc = self.__login_transmission() def __login_transmission(self) -> Optional[Client]: @@ -48,6 +48,14 @@ class Transmission(metaclass=Singleton): logger.error(f"transmission 连接出错:{err}") return None + def is_inactive(self) -> bool: + """ + 判断是否需要重连 + """ + if not self._host or not self._port: + return False + return True if not self.trc else False + def get_torrents(self, ids: Union[str, list] = None, status: Union[str, list] = None, tags: Union[str, list] = None) -> Tuple[List[Torrent], bool]: """