-
Notifications
You must be signed in to change notification settings - Fork 3
/
filter.py
60 lines (45 loc) · 1.78 KB
/
filter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from datetime import datetime
from typing import List, Optional
from regex import regex
class Filter(object):
@classmethod
def from_dicts(cls, filters: Optional[List[dict]]):
if not filters:
return []
return [cls.from_dict(**d) for d in filters if d]
@classmethod
def from_dict(cls, **kwargs):
if len(kwargs.items()) == 0:
raise ValueError("No arguments provided")
if title := kwargs.get("title"):
return TitleFilter(
pattern=title,
case_sensitive=kwargs.get("case_sensitive", False),
invert=kwargs.get("invert", False),
)
if in_seconds := kwargs.get("in_seconds"):
return TimeFilter(in_seconds=in_seconds)
raise ValueError(f"Unknown filter: {kwargs}")
def predicate_item(self, item):
raise NotImplementedError
def filter_items(self, items):
return [item for item in items if self.predicate_item(item)]
class TitleFilter(Filter):
def __init__(self, pattern: str, case_sensitive=False, invert=False):
self.pattern = regex.compile(pattern if case_sensitive else pattern.lower())
self.invert = invert
self.case_sensitive = case_sensitive
def predicate_item(self, item):
title = item["title"] if self.case_sensitive else item["title"].lower()
contains = self.pattern.search(title) is not None
return contains ^ self.invert
class TimeFilter(Filter):
def __init__(self, in_seconds: int):
self.in_seconds = in_seconds
def predicate_item(self, item):
if "pubdate" not in item:
return False
return (
datetime.timestamp(datetime.now()) - datetime.timestamp(item["pubdate"])
< self.in_seconds
)