From 7a5d21016731fc1bd9e9d81699598d4f169e0ac3 Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 19 Sep 2023 09:17:48 +0800 Subject: [PATCH 1/6] =?UTF-8?q?fix=20#553=20=E4=BF=AE=E5=A4=8Dunraid?= =?UTF-8?q?=E5=88=A0=E9=99=A4=E8=B5=84=E6=BA=90=E6=85=A2=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/transfer.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/app/chain/transfer.py b/app/chain/transfer.py index e31cefc6..d6becbe0 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -1,3 +1,4 @@ +import glob import re import shutil import threading @@ -601,8 +602,10 @@ class TransferChain(ChainBase): if not path.exists(): return if path.is_file(): - # 删除文件 - path.unlink() + # 删除文件、nfo、jpg + files = glob.glob(f"{Path(path.parent).joinpath(str(path.name).split('.')[0])}*") + for file in files: + Path(file).unlink() logger.warn(f"文件 {path} 已删除") # 需要删除父目录 elif str(path.parent) == str(path.root): @@ -615,11 +618,25 @@ class TransferChain(ChainBase): # 删除目录 logger.warn(f"目录 {path} 已删除") # 需要删除父目录 - # 判断父目录是否为空, 为空则删除 - for parent_path in path.parents: - if str(parent_path.parent) != str(path.root): - # 父目录非根目录,才删除父目录 - files = SystemUtils.list_files(parent_path, settings.RMT_MEDIAEXT) - if not files: - shutil.rmtree(parent_path) - logger.warn(f"目录 {parent_path} 已删除") + + # 判断当前媒体父路径下是否有媒体文件,如有则无需遍历父级 + files = SystemUtils.list_files(path.parent, settings.RMT_MEDIAEXT) + if not files: + # 媒体库二级分类根路径 + library_root_names = [ + settings.LIBRARY_MOVIE_NAME or '电影', + settings.LIBRARY_TV_NAME or '电视剧', + settings.LIBRARY_ANIME_NAME or '动漫', + ] + + # 判断父目录是否为空, 为空则删除 + for parent_path in path.parents: + # 遍历父目录到媒体库二级分类根路径 + if str(parent_path.name) in library_root_names: + break + if str(parent_path.parent) != str(path.root): + # 父目录非根目录,才删除父目录 + files = SystemUtils.list_files(parent_path, settings.RMT_MEDIAEXT) + if not files: + shutil.rmtree(parent_path) + logger.warn(f"目录 {parent_path} 已删除") From c147d36cb213d401dc60f7800022d9c18cb45ad5 Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 19 Sep 2023 11:15:14 +0800 Subject: [PATCH 2/6] =?UTF-8?q?fix=20=E8=B5=84=E6=BA=90=E4=B8=8B=E8=BD=BDm?= =?UTF-8?q?sg=E5=A2=9E=E5=8A=A0=E4=B8=8B=E8=BD=BD=E7=94=A8=E6=88=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/download.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/chain/download.py b/app/chain/download.py index 3ae393ea..cda85569 100644 --- a/app/chain/download.py +++ b/app/chain/download.py @@ -225,7 +225,7 @@ class DownloadChain(ChainBase): self.downloadhis.add_files(files_to_add) # 发送消息 - self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel) + self.post_download_message(meta=_meta, mediainfo=_media, torrent=_torrent, channel=channel, userid=userid) # 下载成功后处理 self.download_added(context=context, download_dir=download_dir, torrent_path=torrent_file) # 广播事件 From 8be6e289339ed3fe28aa249b33022abbe6dd15d7 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 19 Sep 2023 12:42:10 +0800 Subject: [PATCH 3/6] =?UTF-8?q?feat=20=E4=B8=AD=E6=96=87=E5=AD=97=E5=B9=95?= =?UTF-8?q?=E8=BF=87=E6=BB=A4=E8=A7=84=E5=88=99=E5=8F=AA=E9=92=88=E5=AF=B9?= =?UTF-8?q?=E5=8E=9F=E8=AF=AD=E7=A7=8D=E4=B8=BA=E9=9D=9E=E4=B8=AD=E6=96=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/__init__.py | 7 ++- app/chain/search.py | 3 +- app/chain/subscribe.py | 3 +- app/modules/filter/__init__.py | 65 +++++++++++++++++++++++++--- app/plugins/rsssubscribe/__init__.py | 3 +- 5 files changed, 71 insertions(+), 10 deletions(-) diff --git a/app/chain/__init__.py b/app/chain/__init__.py index 092b5cc0..8f0675dc 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -223,16 +223,19 @@ class ChainBase(metaclass=ABCMeta): def filter_torrents(self, rule_string: str, torrent_list: List[TorrentInfo], - season_episodes: Dict[int, list] = None) -> List[TorrentInfo]: + season_episodes: Dict[int, list] = None, + mediainfo: MediaInfo = None) -> List[TorrentInfo]: """ 过滤种子资源 :param rule_string: 过滤规则 :param torrent_list: 资源列表 :param season_episodes: 季集数过滤 {season:[episodes]} + :param mediainfo: 识别的媒体信息 :return: 过滤后的资源列表,添加资源优先级 """ return self.run_module("filter_torrents", rule_string=rule_string, - torrent_list=torrent_list, season_episodes=season_episodes) + torrent_list=torrent_list, season_episodes=season_episodes, + mediainfo=mediainfo) def download(self, content: Union[Path, str], download_dir: Path, cookie: str, episodes: Set[int] = None, category: str = None diff --git a/app/chain/search.py b/app/chain/search.py index 2c390dbc..413279e1 100644 --- a/app/chain/search.py +++ b/app/chain/search.py @@ -135,7 +135,8 @@ class SearchChain(ChainBase): logger.info(f'开始过滤资源,当前规则:{filter_rule} ...') result: List[TorrentInfo] = self.filter_torrents(rule_string=filter_rule, torrent_list=torrents, - season_episodes=season_episodes) + season_episodes=season_episodes, + mediainfo=mediainfo) if result is not None: torrents = result if not torrents: diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index be86557f..d3e57e00 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -512,7 +512,8 @@ class SubscribeChain(ChainBase): filter_rule = self.systemconfig.get(SystemConfigKey.FilterRules) result: List[TorrentInfo] = self.filter_torrents( rule_string=filter_rule, - torrent_list=[torrent_info]) + torrent_list=[torrent_info], + mediainfo=torrent_mediainfo) if result is not None and not result: # 不符合过滤规则 logger.info(f"{torrent_info.title} 不匹配当前过滤规则") diff --git a/app/modules/filter/__init__.py b/app/modules/filter/__init__.py index a8e3303a..d17ea96a 100644 --- a/app/modules/filter/__init__.py +++ b/app/modules/filter/__init__.py @@ -1,7 +1,7 @@ import re from typing import List, Tuple, Union, Dict, Optional -from app.core.context import TorrentInfo +from app.core.context import TorrentInfo, MediaInfo from app.core.metainfo import MetaInfo from app.log import logger from app.modules import _ModuleBase @@ -9,9 +9,10 @@ from app.modules.filter.RuleParser import RuleParser class FilterModule(_ModuleBase): - # 规则解析器 parser: RuleParser = None + # 媒体信息 + media: MediaInfo = None # 内置规则集 rule_set: Dict[str, dict] = { @@ -37,8 +38,13 @@ class FilterModule(_ModuleBase): }, # 中字 "CNSUB": { - "include": [r'[中国國繁简](/|\s|\\|\|)?[繁简英粤]|[英简繁](/|\s|\\|\|)?[中繁简]|繁體|简体|[中国國][字配]|国语|國語|中文|中字'], - "exclude": [] + "include": [ + r'[中国國繁简](/|\s|\\|\|)?[繁简英粤]|[英简繁](/|\s|\\|\|)?[中繁简]|繁體|简体|[中国國][字配]|国语|國語|中文|中字'], + "exclude": [], + # 只处理对应TMDB信息的数据 + "tmdb": { + "original_language": "zh,cn" + } }, # 特效字幕 "SPECSUB": { @@ -107,16 +113,19 @@ class FilterModule(_ModuleBase): def filter_torrents(self, rule_string: str, torrent_list: List[TorrentInfo], - season_episodes: Dict[int, list] = None) -> List[TorrentInfo]: + season_episodes: Dict[int, list] = None, + mediainfo: MediaInfo = None) -> List[TorrentInfo]: """ 过滤种子资源 :param rule_string: 过滤规则 :param torrent_list: 资源列表 :param season_episodes: 季集数过滤 {season:[episodes]} + :param mediainfo: 媒体信息 :return: 过滤后的资源列表,添加资源优先级 """ if not rule_string: return torrent_list + self.media = mediainfo # 返回种子列表 ret_torrents = [] for torrent in torrent_list: @@ -215,6 +224,11 @@ class FilterModule(_ModuleBase): if not self.rule_set.get(rule_name): # 规则不存在 return False + # TMDB规则 + tmdb = self.rule_set[rule_name].get("tmdb") + # 不符合TMDB规则的直接返回True,即不过滤 + if tmdb and not self.__match_tmdb(tmdb): + return True # 包含规则项 includes = self.rule_set[rule_name].get("include") or [] # 排除规则项 @@ -236,3 +250,44 @@ class FilterModule(_ModuleBase): # FREE规则不匹配 return False return True + + def __match_tmdb(self, tmdb: dict) -> bool: + """ + 判断种子是否匹配TMDB规则 + """ + def __get_media_value(key: str): + try: + return getattr(self.media, key) + except ValueError: + return "" + + if not self.media: + return False + + for attr, value in tmdb.items(): + if not value: + continue + # 获取media信息的值 + info_value = __get_media_value(attr) + if not info_value: + # 没有该值,不匹配 + return False + elif attr == "production_countries": + # 国家信息 + info_values = [str(val.get("iso_3166_1")).upper() for val in info_value] + else: + # media信息转化为数组 + if isinstance(info_value, list): + info_values = [str(val).upper() for val in info_value] + else: + info_values = [str(info_value).upper()] + # 过滤值转化为数组 + if value.find(",") != -1: + values = [str(val).upper() for val in value.split(",")] + else: + values = [str(value).upper()] + # 没有交集为不匹配 + if not set(values).intersection(set(info_values)): + return False + + return True diff --git a/app/plugins/rsssubscribe/__init__.py b/app/plugins/rsssubscribe/__init__.py index 08c6f4b7..bce06b73 100644 --- a/app/plugins/rsssubscribe/__init__.py +++ b/app/plugins/rsssubscribe/__init__.py @@ -593,7 +593,8 @@ class RssSubscribe(_PluginBase): if self._filter: result = self.chain.filter_torrents( rule_string=filter_rule, - torrent_list=[torrentinfo] + torrent_list=[torrentinfo], + mediainfo=mediainfo ) if not result: logger.info(f"{title} {description} 不匹配过滤规则") From dac718edc8e491f9043feffbbf8695ac74bb6022 Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 19 Sep 2023 16:15:05 +0800 Subject: [PATCH 4/6] fix 7a5d2101 --- app/chain/transfer.py | 5 ++--- app/utils/system.py | 32 +++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/chain/transfer.py b/app/chain/transfer.py index d6becbe0..f7d8f470 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -603,7 +603,7 @@ class TransferChain(ChainBase): return if path.is_file(): # 删除文件、nfo、jpg - files = glob.glob(f"{Path(path.parent).joinpath(str(path.name).split('.')[0])}*") + files = glob.glob(f"{Path(path.parent).joinpath(path.stem)}*") for file in files: Path(file).unlink() logger.warn(f"文件 {path} 已删除") @@ -620,8 +620,7 @@ class TransferChain(ChainBase): # 需要删除父目录 # 判断当前媒体父路径下是否有媒体文件,如有则无需遍历父级 - files = SystemUtils.list_files(path.parent, settings.RMT_MEDIAEXT) - if not files: + if not SystemUtils.exits_files(path.parent, settings.RMT_MEDIAEXT): # 媒体库二级分类根路径 library_root_names = [ settings.LIBRARY_MOVIE_NAME or '电影', diff --git a/app/utils/system.py b/app/utils/system.py index 36b553bd..2776ccf3 100644 --- a/app/utils/system.py +++ b/app/utils/system.py @@ -106,7 +106,7 @@ class SystemUtils: if directory.is_file(): return [directory] - + if not min_filesize: min_filesize = 0 @@ -122,6 +122,36 @@ class SystemUtils: return files + @staticmethod + def exits_files(directory: Path, extensions: list, min_filesize: int = 0) -> bool: + """ + 判断目录下是否存在指定扩展名的文件 + :return True存在 False不存在 + """ + + if not min_filesize: + min_filesize = 0 + + if not directory.exists(): + return False + + if directory.is_file(): + return True + + if not min_filesize: + min_filesize = 0 + + pattern = r".*(" + "|".join(extensions) + ")$" + + # 遍历目录及子目录 + for path in directory.rglob('**/*'): + if path.is_file() \ + and re.match(pattern, path.name, re.IGNORECASE) \ + and path.stat().st_size >= min_filesize * 1024 * 1024: + return True + + return False + @staticmethod def list_sub_files(directory: Path, extensions: list) -> List[Path]: """ From d097ef45ebba751e6b219aa93d54c6bb7cb65c2a Mon Sep 17 00:00:00 2001 From: thsrite Date: Tue, 19 Sep 2023 16:44:20 +0800 Subject: [PATCH 5/6] =?UTF-8?q?fix=20=E5=BD=93=E5=89=8D=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E4=B8=8B=E6=B2=A1=E6=9C=89=E5=AA=92=E4=BD=93=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=88=99=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/transfer.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/chain/transfer.py b/app/chain/transfer.py index f7d8f470..12236fee 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -635,7 +635,7 @@ class TransferChain(ChainBase): break if str(parent_path.parent) != str(path.root): # 父目录非根目录,才删除父目录 - files = SystemUtils.list_files(parent_path, settings.RMT_MEDIAEXT) - if not files: + if not SystemUtils.exits_files(path.parent, settings.RMT_MEDIAEXT): + # 当前路径下没有媒体文件则删除 shutil.rmtree(parent_path) logger.warn(f"目录 {parent_path} 已删除") From b1bb0ced7aedd41ad45cec8d4696bf3b842cfe6b Mon Sep 17 00:00:00 2001 From: jxxghp Date: Tue, 19 Sep 2023 17:15:16 +0800 Subject: [PATCH 6/6] fix #608 --- app/modules/transmission/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index b81e06c9..78eb6456 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -131,7 +131,7 @@ class TransmissionModule(_ModuleBase): title=torrent.name, path=Path(torrent.download_dir) / torrent.name, hash=torrent.hashString, - tags=torrent.labels + tags=",".join(torrent.labels or []) )) elif status == TorrentStatus.DOWNLOADING: # 获取正在下载的任务