From 30ae5837042b8dd3b4ff083464871ef113dd7420 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Thu, 4 Jul 2024 22:16:20 +0800 Subject: [PATCH] fix torrent filter --- app/chain/subscribe.py | 23 +++++++++++++++++++ app/helper/torrent.py | 51 +++++++++++++++++++++++++++++++++++++++++- app/schemas/types.py | 8 ++++--- 3 files changed, 78 insertions(+), 4 deletions(-) diff --git a/app/chain/subscribe.py b/app/chain/subscribe.py index 94c25ecd..6e831fca 100644 --- a/app/chain/subscribe.py +++ b/app/chain/subscribe.py @@ -718,6 +718,11 @@ class SubscribeChain(ChainBase): logger.info(f'{subscribe.name} 正在洗版,{torrent_info.title} 优先级低于或等于已下载优先级') continue + # 匹配订阅参数 + if not self.torrenthelper.filter_torrent(torrent_info=torrent_info, + filter_params=self.get_params(subscribe)): + continue + # 匹配成功 logger.info(f'{mediainfo.title_year} 匹配成功:{torrent_info.title}') _match_context.append(context) @@ -1132,3 +1137,21 @@ class SubscribeChain(ChainBase): if not value: return None return value.get(default_config_key) or None + + def get_params(self, subscribe: Subscribe): + """ + 获取订阅默认参数 + """ + # 默认过滤规则 + default_rule = self.systemconfig.get(SystemConfigKey.SubscribeDefaultParams) or {} + return { + "include": subscribe.include or default_rule.get("include"), + "exclude": subscribe.exclude or default_rule.get("exclude"), + "quality": subscribe.quality or default_rule.get("quality"), + "resolution": subscribe.resolution or default_rule.get("resolution"), + "effect": subscribe.effect or default_rule.get("effect"), + "tv_size": default_rule.get("tv_size"), + "movie_size": default_rule.get("movie_size"), + "min_seeders": default_rule.get("min_seeders"), + "min_seeders_time": default_rule.get("min_seeders_time"), + } diff --git a/app/helper/torrent.py b/app/helper/torrent.py index 55eb97b2..ffcef262 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -1,7 +1,7 @@ import datetime import re from pathlib import Path -from typing import Tuple, Optional, List, Union +from typing import Tuple, Optional, List, Union, Dict from urllib.parse import unquote from requests import Response @@ -384,3 +384,52 @@ class TorrentHelper(metaclass=Singleton): # 未匹配 logger.debug(f'{torrent.site_name} - {torrent.title} 标题不匹配,识别名称:{meta_names}') return False + + @staticmethod + def filter_torrent(torrent_info: TorrentInfo, + filter_params: Dict[str, str]) -> bool: + """ + 检查种子是否匹配订阅过滤规则 + """ + + if not filter_params: + return True + + # 匹配内容 + content = (f"{torrent_info.title} " + f"{torrent_info.description} " + f"{' '.join(torrent_info.labels or [])} " + f"{torrent_info.volume_factor}") + + # 包含 + include = filter_params.get("include") + if include: + if not re.search(r"%s" % include, content, re.I): + logger.info(f"{content} 不匹配包含规则 {include}") + return False + # 排除 + exclude = filter_params.get("exclude") + if exclude: + if re.search(r"%s" % exclude, content, re.I): + logger.info(f"{content} 匹配排除规则 {exclude}") + return False + # 质量 + quality = filter_params.get("quality") + if quality: + if not re.search(r"%s" % quality, torrent_info.title, re.I): + logger.info(f"{torrent_info.title} 不匹配质量规则 {quality}") + return False + # 分辨率 + resolution = filter_params.get("resolution") + if resolution: + if not re.search(r"%s" % resolution, torrent_info.title, re.I): + logger.info(f"{torrent_info.title} 不匹配分辨率规则 {resolution}") + return False + # 特效 + effect = filter_params.get("effect") + if effect: + if not re.search(r"%s" % effect, torrent_info.title, re.I): + logger.info(f"{torrent_info.title} 不匹配特效规则 {effect}") + return False + + return True diff --git a/app/schemas/types.py b/app/schemas/types.py index aa67ac46..af6d8bb9 100644 --- a/app/schemas/types.py +++ b/app/schemas/types.py @@ -88,11 +88,13 @@ class SystemConfigKey(Enum): CustomFilterRules = "CustomFilterRules" # 用户规则组 UserRuleGroups = "UserRuleGroups" - # 搜索默认过滤规则 + # 搜索默认过滤规则组 SearchFilterRuleGroups = "SearchFilterRuleGroups" - # 订阅默认过滤规则 + # 订阅默认过滤规则组 SubscribeFilterRuleGroups = "SubscribeFilterRuleGroups" - # 洗版默认过滤规则 + # 订阅默认参数 + SubscribeDefaultParams = "SubscribeDefaultParams" + # 洗版默认过滤规则组 BeseVersionFilterRuleGroups = "BeseVersionFilterRuleGroups" # 订阅统计 SubscribeReport = "SubscribeReport"