fix filter

This commit is contained in:
jxxghp
2023-06-10 20:32:37 +08:00
parent f1006676c7
commit be9c6b0571
7 changed files with 70 additions and 16 deletions

View File

@ -1,6 +1,6 @@
from abc import abstractmethod, ABCMeta
from pathlib import Path
from typing import Optional, List, Tuple, Union, Set, Any
from typing import Optional, List, Tuple, Union, Set, Any, Dict
from ruamel.yaml import CommentedMap
@ -116,10 +116,12 @@ class _ModuleBase(metaclass=ABCMeta):
"""
pass
def filter_torrents(self, torrent_list: List[TorrentInfo]) -> List[TorrentInfo]:
def filter_torrents(self, torrent_list: List[TorrentInfo],
season_episodes: Dict[int, dict] = None) -> List[TorrentInfo]:
"""
过滤资源
:param torrent_list: 资源列表
:param season_episodes: 过滤的剧集信息
:return: 过滤后的资源列表注意如果返回None有可能是没有对应的处理模块应无视结果
"""
pass

View File

@ -3,6 +3,8 @@ from typing import List, Tuple, Union, Dict, Optional
from app.core.context import TorrentInfo
from app.core.config import settings
from app.core.metainfo import MetaInfo
from app.log import logger
from app.modules import _ModuleBase
from app.modules.filter.RuleParser import RuleParser
@ -70,21 +72,57 @@ class FilterModule(_ModuleBase):
def init_setting(self) -> Tuple[str, Union[str, bool]]:
return "FILTER_RULE", True
def filter_torrents(self, torrent_list: List[TorrentInfo]) -> List[TorrentInfo]:
def filter_torrents(self, torrent_list: List[TorrentInfo],
season_episodes: Dict[int, list] = None) -> List[TorrentInfo]:
"""
过滤种子资源
:param torrent_list: 资源列表
:param season_episodes: 季集数过滤 {season:[episodes]}
:return: 过滤后的资源列表,添加资源优先级
"""
# 返回种子列表
ret_torrents = []
for torrent in torrent_list:
# 能命中优先级的才返回
if self.__get_order(torrent, settings.FILTER_RULE):
ret_torrents.append(torrent)
if not self.__get_order(torrent, settings.FILTER_RULE):
continue
# 季集数过滤
if season_episodes \
and not self.__match_season_episodes(torrent, season_episodes):
continue
ret_torrents.append(torrent)
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.get_season_list()
if not torrent_seasons:
# 按第一季处理
torrent_seasons = [1]
# 种子集
torrent_episodes = meta.get_episode_list()
if not set(torrent_seasons).issubset(set(seasons)):
# 种子季不在过滤季中
logger.info(f"种子 {torrent.title} 不是需要的季")
return False
if not torrent_episodes:
# 整季按匹配处理
return True
if len(torrent_episodes) == 1 \
and not set(torrent_seasons).issubset(set(season_episodes.get(torrent_seasons[0]))):
# 单季不是子集的种子不要
logger.info(f"种子 {torrent.title}{torrent_episodes} 不是需要的集")
return False
return True
def __get_order(self, torrent: TorrentInfo, rule_str: str) -> Optional[TorrentInfo]:
"""
获取种子匹配的规则优先级值越大越优先未匹配时返回None

View File

@ -351,7 +351,7 @@ class TmdbHelper:
seasons = self.__get_tv_seasons(tv_info)
for season, season_info in seasons.items():
if season_info.get("air_date"):
if season.get("air_date")[0:4] == str(_season_year) \
if season_info.get("air_date")[0:4] == str(_season_year) \
and season == int(season_number):
return True
except Exception as e1: