diff --git a/app/chain/__init__.py b/app/chain/__init__.py index c63979cc..fa3da2e5 100644 --- a/app/chain/__init__.py +++ b/app/chain/__init__.py @@ -326,19 +326,16 @@ class ChainBase(metaclass=ABCMeta): def filter_torrents(self, rule_groups: List[str], torrent_list: List[TorrentInfo], - season_episodes: Dict[int, list] = None, mediainfo: MediaInfo = None) -> List[TorrentInfo]: """ 过滤种子资源 :param rule_groups: 过滤规则组名称列表 :param torrent_list: 资源列表 - :param season_episodes: 季集数过滤 {season:[episodes]} :param mediainfo: 识别的媒体信息 :return: 过滤后的资源列表,添加资源优先级 """ return self.run_module("filter_torrents", rule_groups=rule_groups, - torrent_list=torrent_list, season_episodes=season_episodes, - mediainfo=mediainfo) + torrent_list=torrent_list, 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 8a026b97..f7a819ac 100644 --- a/app/chain/search.py +++ b/app/chain/search.py @@ -125,7 +125,6 @@ class SearchChain(ChainBase): """ return self.filter_torrents(rule_groups=rule_groups, torrent_list=torrent_list, - season_episodes=season_episodes, mediainfo=mediainfo) or [] # 豆瓣标题处理 @@ -185,7 +184,10 @@ class SearchChain(ChainBase): # 开始过滤 self.progress.update(value=0, text=f'开始过滤,总 {len(torrents)} 个资源,请稍候...', key=ProgressKey.Search) - + # 匹配订阅附加参数 + if filter_params: + logger.info(f'开始附加参数过滤,附加参数:{filter_params} ...') + torrents = [torrent for torrent in torrents if self.torrenthelper.filter_torrent(torrent, filter_params)] # 开始过滤规则过滤 if rule_groups is None: # 取搜索过滤规则 @@ -222,16 +224,18 @@ class SearchChain(ChainBase): if not torrent.title: continue - # 匹配订阅附加参数 - if filter_params and not self.torrenthelper.filter_torrent(torrent_info=torrent, - filter_params=filter_params): - continue - # 识别元数据 torrent_meta = MetaInfo(title=torrent.title, subtitle=torrent.description, custom_words=custom_words) if torrent.title != torrent_meta.org_string: logger.info(f"种子名称应用识别词后发生改变:{torrent.title} => {torrent_meta.org_string}") + # 季集数过滤 + if season_episodes \ + and not self.torrenthelper.match_season_episodes( + torrent=torrent, + meta=torrent_meta, + season_episodes=season_episodes): + continue # 比对IMDBID if torrent.imdbid \ and mediainfo.imdb_id \ diff --git a/app/helper/torrent.py b/app/helper/torrent.py index a52fccd5..54e7fa9a 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -9,7 +9,7 @@ from torrentool.api import Torrent from app.core.config import settings from app.core.context import Context, TorrentInfo, MediaInfo -from app.core.metainfo import MetaInfo +from app.core.metainfo import MetaBase, MetaInfo from app.db.site_oper import SiteOper from app.db.systemconfig_oper import SystemConfigOper from app.log import logger @@ -445,3 +445,38 @@ class TorrentHelper(metaclass=Singleton): return False return True + + @staticmethod + def match_season_episodes(torrent: TorrentInfo, meta: MetaBase, season_episodes: Dict[int, list]) -> bool: + """ + 判断种子是否匹配季集数 + :param torrent: 种子信息 + :param meta: 种子元数据 + :param season_episodes: 季集数 {season:[episodes]} + """ + # 匹配季 + seasons = season_episodes.keys() + # 种子季 + torrent_seasons = meta.season_list + if not torrent_seasons: + # 按第一季处理 + torrent_seasons = [1] + # 种子集 + torrent_episodes = meta.episode_list + if not set(torrent_seasons).issubset(set(seasons)): + # 种子季不在过滤季中 + logger.debug( + f"种子 {torrent.site_name} - {torrent.title} 包含季 {torrent_seasons} 不是需要的季 {list(seasons)}") + return False + if not torrent_episodes: + # 整季按匹配处理 + return True + if len(torrent_seasons) == 1: + need_episodes = season_episodes.get(torrent_seasons[0]) + if need_episodes \ + and not set(torrent_episodes).intersection(set(need_episodes)): + # 单季集没有交集的不要 + logger.debug(f"种子 {torrent.site_name} - {torrent.title} " + f"集 {torrent_episodes} 没有需要的集:{need_episodes}") + return False + return True diff --git a/app/modules/filter/__init__.py b/app/modules/filter/__init__.py index 80f2117a..2d6724e3 100644 --- a/app/modules/filter/__init__.py +++ b/app/modules/filter/__init__.py @@ -192,13 +192,11 @@ class FilterModule(_ModuleBase): def filter_torrents(self, rule_groups: List[str], torrent_list: List[TorrentInfo], - season_episodes: Dict[int, list] = None, mediainfo: MediaInfo = None) -> List[TorrentInfo]: """ 过滤种子资源 :param rule_groups: 过滤规则组名称列表 :param torrent_list: 资源列表 - :param season_episodes: 季集数过滤 {season:[episodes]} :param mediainfo: 媒体信息 :return: 过滤后的资源列表,添加资源优先级 """ @@ -215,24 +213,18 @@ class FilterModule(_ModuleBase): torrent_list = self.__filter_torrents( rule_string=group.rule_string, rule_name=group.name, - torrent_list=torrent_list, - season_episodes=season_episodes - ) + torrent_list=torrent_list + ) return torrent_list def __filter_torrents(self, rule_string: str, rule_name: str, - torrent_list: List[TorrentInfo], - season_episodes: Dict[int, list]) -> List[TorrentInfo]: + torrent_list: List[TorrentInfo]) -> List[TorrentInfo]: """ 过滤种子 """ # 返回种子列表 ret_torrents = [] for torrent in torrent_list: - # 季集数过滤 - if season_episodes \ - and not self.__match_season_episodes(torrent, season_episodes): - continue # 能命中优先级的才返回 if not self.__get_order(torrent, rule_string): logger.debug(f"种子 {torrent.site_name} - {torrent.title} {torrent.description} " @@ -242,39 +234,6 @@ class FilterModule(_ModuleBase): return ret_torrents - @staticmethod - def __match_season_episodes(torrent: TorrentInfo, season_episodes: Dict[int, list]): - """ - 判断种子是否匹配季集数 - """ - # 匹配季 - seasons = season_episodes.keys() - meta = MetaInfo(title=torrent.title, subtitle=torrent.description) - # 种子季 - torrent_seasons = meta.season_list - if not torrent_seasons: - # 按第一季处理 - torrent_seasons = [1] - # 种子集 - torrent_episodes = meta.episode_list - if not set(torrent_seasons).issubset(set(seasons)): - # 种子季不在过滤季中 - logger.debug( - f"种子 {torrent.site_name} - {torrent.title} 包含季 {torrent_seasons} 不是需要的季 {list(seasons)}") - return False - if not torrent_episodes: - # 整季按匹配处理 - return True - if len(torrent_seasons) == 1: - need_episodes = season_episodes.get(torrent_seasons[0]) - if need_episodes \ - and not set(torrent_episodes).intersection(set(need_episodes)): - # 单季集没有交集的不要 - logger.debug(f"种子 {torrent.site_name} - {torrent.title} " - f"集 {torrent_episodes} 没有需要的集:{need_episodes}") - return False - return True - def __get_order(self, torrent: TorrentInfo, rule_str: str) -> Optional[TorrentInfo]: """ 获取种子匹配的规则优先级,值越大越优先,未匹配时返回None