feed: add support to filter posts

The post filter is kept very simple, it works by checking if a fixed
string exists in the configured field.  This could easily be expanded
allowing for regex or even more complex filters like date-time
comparisons.
This commit is contained in:
ducklet 2020-11-07 21:25:36 +01:00
parent 78777a4da9
commit 15c3cb0221
2 changed files with 23 additions and 2 deletions

View file

@ -15,10 +15,15 @@ FeedId = str
PostId = str
def no_filter(x):
return True
@dataclass
class Feed:
id: FeedId
url: str
filter: Callable[["Post"], bool] = no_filter
title: Optional[str] = None
posts: List["Post"] = field(default_factory=list)
etag: Optional[str] = None
@ -55,13 +60,14 @@ class Feed:
if "title" in r.feed:
self.title = r.feed.title
posts = [Post.from_entry(e) for e in r.entries]
posts = [p for e in r.entries if self.filter(p := Post.from_entry(e))]
for post in posts:
if post.date is None:
post.date = pubdate(r.feed)
posts.sort(key=lambda e: e.date, reverse=True)
self.posts = posts
# Find link to next feed page.
for link in r.feed.get("links", []):
if link.get("rel") == "next":
self.next_url = link.get("href")