From 0274bbe1862d53cbecb713373e934af6f25c0fb0 Mon Sep 17 00:00:00 2001 From: hkc Date: Mon, 29 Aug 2022 17:17:57 +0300 Subject: [PATCH] Various filter fixes --- mastoposter/filters/__init__.py | 2 +- mastoposter/filters/boost.py | 4 ++-- mastoposter/filters/mention.py | 4 ++-- mastoposter/filters/spoiler.py | 2 +- mastoposter/filters/text.py | 3 ++- 5 files changed, 8 insertions(+), 7 deletions(-) diff --git a/mastoposter/filters/__init__.py b/mastoposter/filters/__init__.py index 1529238..45d14d0 100644 --- a/mastoposter/filters/__init__.py +++ b/mastoposter/filters/__init__.py @@ -1,7 +1,7 @@ from typing import List from mastoposter.types import Status -from .base import FilterInstance # NOQA +from .base import FilterInstance from mastoposter.filters.boost import BoostFilter # NOQA from mastoposter.filters.combined import CombinedFilter # NOQA from mastoposter.filters.mention import MentionFilter # NOQA diff --git a/mastoposter/filters/boost.py b/mastoposter/filters/boost.py index f156bdb..687b60b 100644 --- a/mastoposter/filters/boost.py +++ b/mastoposter/filters/boost.py @@ -10,7 +10,7 @@ class BoostFilter(BaseFilter, filter_name="boost"): self.list = section.get("list", "").split() @classmethod - def check_account(cls, acct: str, mask: str): + def check_account(cls, acct: str, mask: str) -> bool: return fnmatch(acct, mask) def __call__(self, status: Status) -> bool: @@ -20,7 +20,7 @@ class BoostFilter(BaseFilter, filter_name="boost"): return True return any( [ - self.check_account(status.reblog.account.acct, mask) + self.check_account("@" + status.reblog.account.acct, mask) for mask in self.list ] ) diff --git a/mastoposter/filters/mention.py b/mastoposter/filters/mention.py index 28e75c1..8ba70e4 100644 --- a/mastoposter/filters/mention.py +++ b/mastoposter/filters/mention.py @@ -14,8 +14,8 @@ class MentionFilter(BaseFilter, filter_name="mention"): self.list = section.get("list", "").split() @classmethod - def check_account(cls, acct: str, mask: str): - return fnmatch(acct, mask) + def check_account(cls, acct: str, mask: str) -> bool: + return fnmatch("@" + acct, mask) def __call__(self, status: Status) -> bool: if not self.list and status.mentions: diff --git a/mastoposter/filters/spoiler.py b/mastoposter/filters/spoiler.py index 2d0f5df..6e23fcd 100644 --- a/mastoposter/filters/spoiler.py +++ b/mastoposter/filters/spoiler.py @@ -7,7 +7,7 @@ from mastoposter.types import Status class SpoilerFilter(BaseFilter, filter_name="spoiler"): def __init__(self, section: SectionProxy): super().__init__(section) - self.regexp: Pattern = regexp(section["regexp"]) + self.regexp: Pattern = regexp(section.get("regexp", "^.*$")) def __call__(self, status: Status) -> bool: return self.regexp.match(status.spoiler_text) is not None diff --git a/mastoposter/filters/text.py b/mastoposter/filters/text.py index 12c09ee..7ced4ec 100644 --- a/mastoposter/filters/text.py +++ b/mastoposter/filters/text.py @@ -16,7 +16,7 @@ class TextFilter(BaseFilter, filter_name="content"): if self.mode == "regexp": self.regexp = regexp(section["regexp"]) - elif self.mode == "hashtag": + elif self.mode in ("hashtag", "tag"): self.tags = set(section["tags"].split()) else: raise ValueError(f"Invalid filter mode {self.mode}") @@ -46,6 +46,7 @@ class TextFilter(BaseFilter, filter_name="content"): is not None ) elif self.tags: + print(f"{self.tags=} {source.tags=}") return len(self.tags & {t.name for t in source.tags}) > 0 else: raise ValueError("Neither regexp or tags were set. Why?")