refactor(helper): 将DirectoryHelper、RuleHelper和TorrentHelper方法改为静态方法

- 移除不必要的实例化操作,直接使用类方法
This commit is contained in:
Attente
2026-03-13 19:50:10 +08:00
parent e09cfc6704
commit b89825525a
20 changed files with 81 additions and 79 deletions

View File

@@ -27,31 +27,36 @@ class DirectoryHelper:
return []
return [schemas.TransferDirectoryConf(**d) for d in dir_confs]
def get_download_dirs(self) -> List[schemas.TransferDirectoryConf]:
@staticmethod
def get_download_dirs() -> List[schemas.TransferDirectoryConf]:
"""
获取所有下载目录
"""
return sorted([d for d in self.get_dirs() if d.download_path], key=lambda x: x.priority)
return sorted([d for d in DirectoryHelper.get_dirs() if d.download_path], key=lambda x: x.priority)
def get_local_download_dirs(self) -> List[schemas.TransferDirectoryConf]:
@staticmethod
def get_local_download_dirs() -> List[schemas.TransferDirectoryConf]:
"""
获取所有本地的可下载目录
"""
return [d for d in self.get_download_dirs() if d.storage == "local"]
return [d for d in DirectoryHelper.get_download_dirs() if d.storage == "local"]
def get_library_dirs(self) -> List[schemas.TransferDirectoryConf]:
@staticmethod
def get_library_dirs() -> List[schemas.TransferDirectoryConf]:
"""
获取所有媒体库目录
"""
return sorted([d for d in self.get_dirs() if d.library_path], key=lambda x: x.priority)
return sorted([d for d in DirectoryHelper.get_dirs() if d.library_path], key=lambda x: x.priority)
def get_local_library_dirs(self) -> List[schemas.TransferDirectoryConf]:
@staticmethod
def get_local_library_dirs() -> List[schemas.TransferDirectoryConf]:
"""
获取所有本地的媒体库目录
"""
return [d for d in self.get_library_dirs() if d.library_storage == "local"]
return [d for d in DirectoryHelper.get_library_dirs() if d.library_storage == "local"]
def get_dir(self, media: Optional[MediaInfo], include_unsorted: Optional[bool] = False,
@staticmethod
def get_dir(media: Optional[MediaInfo], include_unsorted: Optional[bool] = False,
storage: Optional[str] = None, src_path: Path = None,
target_storage: Optional[str] = None, dest_path: Path = None
) -> Optional[schemas.TransferDirectoryConf]:
@@ -66,7 +71,7 @@ class DirectoryHelper:
"""
# 电影/电视剧
media_type = media.type.value if media else None
dirs = self.get_dirs()
dirs = DirectoryHelper.get_dirs()
# 如果存在源目录,并源目录为任一下载目录的子目录时,则进行源目录匹配,否则,允许源目录按同盘优先的逻辑匹配
matching_dirs = [d for d in dirs if src_path.is_relative_to(d.download_path)] if src_path else []
@@ -106,7 +111,7 @@ class DirectoryHelper:
# 优先源目录同盘
for matched_dir in matched_dirs:
matched_path = Path(matched_dir.download_path)
if self._is_same_source((src_path, storage or "local"), (matched_path, matched_dir.library_storage)):
if DirectoryHelper._is_same_source((src_path, storage or "local"), (matched_path, matched_dir.library_storage)):
return matched_dir
return matched_dirs[0]
return None

View File

@@ -21,22 +21,24 @@ class RuleHelper:
return []
return [FilterRuleGroup(**group) for group in rule_groups]
def get_rule_group(self, group_name: str) -> Optional[FilterRuleGroup]:
@staticmethod
def get_rule_group(group_name: str) -> Optional[FilterRuleGroup]:
"""
获取规则组
"""
rule_groups = self.get_rule_groups()
rule_groups = RuleHelper.get_rule_groups()
for group in rule_groups:
if group.name == group_name:
return group
return None
def get_rule_group_by_media(self, media: MediaInfo = None, group_names: list = None) -> List[FilterRuleGroup]:
@staticmethod
def get_rule_group_by_media(media: MediaInfo = None, group_names: list = None) -> List[FilterRuleGroup]:
"""
根据媒体信息获取规则组
"""
ret_groups = []
rule_groups = self.get_rule_groups()
rule_groups = RuleHelper.get_rule_groups()
if group_names:
rule_groups = [group for group in rule_groups if group.name in group_names]
for group in rule_groups:
@@ -58,11 +60,12 @@ class RuleHelper:
return []
return [CustomRule(**rule) for rule in rules]
def get_custom_rule(self, rule_id: str) -> Optional[CustomRule]:
@staticmethod
def get_custom_rule(rule_id: str) -> Optional[CustomRule]:
"""
获取自定义规则
"""
rules = self.get_custom_rules()
rules = RuleHelper.get_custom_rules()
for rule in rules:
if rule.id == rule_id:
return rule

View File

@@ -145,7 +145,8 @@ class TorrentHelper:
self.add_invalid(url)
return cache_path, None, "", [], f"下载种子出错,状态码:{req.status_code}"
def get_torrent_info(self, torrent_path: Path) -> Tuple[str, List[str]]:
@staticmethod
def get_torrent_info(torrent_path: Path) -> Tuple[str, List[str]]:
"""
获取种子文件的文件夹名和文件清单
:param torrent_path: 种子文件路径
@@ -156,7 +157,7 @@ class TorrentHelper:
try:
torrentinfo = Torrent.from_file(torrent_path)
# 获取文件清单
return self.get_fileinfo_from_torrent(torrentinfo)
return TorrentHelper.get_fileinfo_from_torrent(torrentinfo)
except Exception as err:
logger.error(f"种子文件解析失败:{str(err)}")
return "", []
@@ -192,7 +193,8 @@ class TorrentHelper:
logger.debug(f"解析种子:{torrent.name} => 目录:{folder_name},文件清单:{file_list}")
return folder_name, file_list
def get_fileinfo_from_torrent_content(self, torrent_content: Union[str, bytes]) -> Tuple[str, List[str]]:
@staticmethod
def get_fileinfo_from_torrent_content(torrent_content: Union[str, bytes]) -> Tuple[str, List[str]]:
"""
从种子内容中获取文件夹名和文件清单
:param torrent_content: 种子内容
@@ -210,7 +212,7 @@ class TorrentHelper:
# 解析种子内容
torrentinfo = Torrent.from_string(torrent_content)
# 获取文件清单
return self.get_fileinfo_from_torrent(torrentinfo)
return TorrentHelper.get_fileinfo_from_torrent(torrentinfo)
except Exception as err:
logger.error(f"种子内容解析失败:{str(err)}")
return "", []
@@ -292,7 +294,8 @@ class TorrentHelper:
# 排序
return sorted(torrent_list, key=lambda x: get_sort_str(x), reverse=True)
def sort_group_torrents(self, torrent_list: List[Context]) -> List[Context]:
@staticmethod
def sort_group_torrents(torrent_list: List[Context]) -> List[Context]:
"""
对媒体信息进行排序、去重
"""
@@ -300,7 +303,7 @@ class TorrentHelper:
return []
# 排序
torrent_list = self.sort_torrents(torrent_list)
torrent_list = TorrentHelper.sort_torrents(torrent_list)
# 控重
result = []