fix rule group filter

This commit is contained in:
jxxghp
2024-09-19 13:36:07 +08:00
parent 786b317cea
commit f3b2bbfb6f
2 changed files with 28 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
from typing import List, Optional from typing import List, Optional
from app.core.context import MediaInfo
from app.db.systemconfig_oper import SystemConfigOper from app.db.systemconfig_oper import SystemConfigOper
from app.schemas import FilterRuleGroup, CustomRule from app.schemas import FilterRuleGroup, CustomRule
from app.schemas.types import SystemConfigKey from app.schemas.types import SystemConfigKey
@@ -32,6 +33,23 @@ class RuleHelper:
return group return group
return None return None
def get_rule_group_by_media(self, media: MediaInfo, group_names: list = None) -> List[FilterRuleGroup]:
"""
根据媒体信息获取规则组
"""
ret_groups = []
rule_groups = self.get_rule_groups()
if group_names:
rule_groups = [group for group in rule_groups if group.name in group_names]
for group in rule_groups:
if not group.media_type:
ret_groups.append(group)
elif not group.category and group.media_type == media.type.value:
ret_groups.append(group)
elif group.category == media.category:
ret_groups.append(group)
return ret_groups
def get_custom_rules(self) -> List[CustomRule]: def get_custom_rules(self) -> List[CustomRule]:
""" """
获取用户所有自定义规则 获取用户所有自定义规则

View File

@@ -178,26 +178,16 @@ class FilterModule(_ModuleBase):
return torrent_list return torrent_list
self.media = mediainfo self.media = mediainfo
# 查询规则表详情 # 查询规则表详情
for group_name in rule_groups: groups = self.rulehelper.get_rule_group_by_media(media=mediainfo, group_names=rule_groups)
rule_group = self.rulehelper.get_rule_group(group_name) if groups:
if not rule_group: for group in groups:
logger.error(f"规则组 {group_name} 不存在") # 过滤种子
continue torrent_list = self.__filter_torrents(
if rule_group.media_type and rule_group.media_type != mediainfo.type.value: rule_string=group.rule_string,
# 规则组不适用当前媒体类型 rule_name=group.name,
logger.debug(f"规则组 {group_name} 不适用于 {mediainfo.type.value}") torrent_list=torrent_list,
continue season_episodes=season_episodes
if rule_group.category and mediainfo.category and mediainfo.category != rule_group.category: )
# 规则组不适用于当前媒体类别
logger.debug(f"规则组 {group_name} 不适用于 {mediainfo.category}")
continue
# 过滤种子
torrent_list = self.__filter_torrents(
rule_string=rule_group.rule_string,
rule_name=rule_group.name,
torrent_list=torrent_list,
season_episodes=season_episodes
)
return torrent_list return torrent_list
def __filter_torrents(self, rule_string: str, rule_name: str, def __filter_torrents(self, rule_string: str, rule_name: str,