From 5b66803f6d2009985229371d559f1d19a7be7584 Mon Sep 17 00:00:00 2001 From: Attente <19653207+wikrin@users.noreply.github.com> Date: Wed, 15 Jan 2025 03:43:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E8=AE=A2=E9=98=85?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E8=AF=8D=E5=9C=A8=E4=B8=8B=E8=BD=BD=E9=98=B6?= =?UTF-8?q?=E6=AE=B5=E4=B8=8D=E7=94=9F=E6=95=88=E7=9A=84=E9=97=AE=E9=A2=98?= =?UTF-8?q?=20-=20=E5=B0=86`=E5=AD=A3=E9=9B=86=E5=8C=B9=E9=85=8D`=E4=BB=8E?= =?UTF-8?q?`=E4=BC=98=E5=85=88=E7=BA=A7=E8=A7=84=E5=88=99=E7=BB=84?= =?UTF-8?q?=E5=8C=B9=E9=85=8D=E6=A8=A1=E5=9D=97`=E7=A7=BB=E8=87=B3`?= =?UTF-8?q?=E7=A7=8D=E5=AD=90=E5=B8=AE=E5=8A=A9=E7=B1=BB`=20-=20-=20`Filte?= =?UTF-8?q?rModule.=5F=5Fmatch=5Fseason=5Fepisodes()`=20=3D=3D>=20`Torrent?= =?UTF-8?q?Helper.match=5Fseason=5Fepisodes()`=20-=20-=20=E7=A1=AE?= =?UTF-8?q?=E4=BF=9D=E9=9C=80=E8=A6=81`=E8=AE=A2=E9=98=85=E8=AF=86?= =?UTF-8?q?=E5=88=AB=E8=AF=8D`=20`=E5=81=8F=E7=A7=BB=E5=AD=A3=E9=9B=86`?= =?UTF-8?q?=E7=9A=84=E7=A7=8D=E5=AD=90=E8=83=BD=E5=A4=9F=E6=AD=A3=E7=A1=AE?= =?UTF-8?q?=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/chain/__init__.py | 5 +--- app/chain/search.py | 18 ++++++++----- app/helper/torrent.py | 37 +++++++++++++++++++++++++- app/modules/filter/__init__.py | 47 +++------------------------------- 4 files changed, 51 insertions(+), 56 deletions(-) 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