From 85d5c134b58a6cd044c5033f277430c92330c6d5 Mon Sep 17 00:00:00 2001 From: crunk Date: Mon, 17 Jul 2023 23:28:19 +0200 Subject: [PATCH] tag filter --- app.py | 3 +++ column.py | 6 +++++- columns.toml | 1 + parse_rss_feeds.py | 15 ++++++++++++--- 4 files changed, 21 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 2129635..b33fc29 100644 --- a/app.py +++ b/app.py @@ -41,6 +41,9 @@ def update_feeds(json_file): column.set_limit(column_from_file["limit"]) if "sort_order" in column_from_file: column.set_sort_order(column_from_file["sort_order"]) + if "tag_filter" in column_from_file: + column.set_tag_filter(column_from_file["tag_filter"]) + column.load_content_from_feeds() columns[title] = column.__dict__ json_file.update(columns) diff --git a/column.py b/column.py index 17cbca6..8dcf580 100644 --- a/column.py +++ b/column.py @@ -13,6 +13,10 @@ class Column(object): self.limit = None self.sort_order = None self.entries = None + self.tag_filter = None + + def set_tag_filter(self, tag_filter): + self.tag_filter = tag_filter def set_sort_order(self, sort_order): self.sort_order = sort_order @@ -55,7 +59,7 @@ class Column(object): self.entries.popitem() def load_content_from_feeds(self): - entries = parse_rss_feeds(self.urls) + entries = parse_rss_feeds(self.urls, self.tag_filter) self.entries = entries if self.sort_order: self._sort_by_order() diff --git a/columns.toml b/columns.toml index d51ed2d..e28f2e3 100644 --- a/columns.toml +++ b/columns.toml @@ -8,6 +8,7 @@ sort_order = "random" urls = ["https://post.lurk.org/@cmos4040.rss"] title = "circulations" sort_order = "reverse" +tag_filter = "electronics" [[column]] urls = ["https://post.lurk.org/tags/radio.rss"] diff --git a/parse_rss_feeds.py b/parse_rss_feeds.py index d57c9c4..b625694 100644 --- a/parse_rss_feeds.py +++ b/parse_rss_feeds.py @@ -3,10 +3,12 @@ import random from feedparser import parse -def parse_single_rss_feed(url, entries): +def parse_single_rss_feed(url, entries, tag_filter): feed = parse(url) entrylength = len(entries) for entrynumber, entry in enumerate(feed.entries): + if tag_filter and not contains_category(entry, tag_filter): + continue if entry.has_key("title"): entries[entry.title] = [] entrylist = entries[entry.title] @@ -18,9 +20,16 @@ def parse_single_rss_feed(url, entries): entrylist.append(entry.published_parsed) return entries +def contains_category(entry, tag_filter): + if not entry.has_key("tags"): + return False + for tag in entry.tags: + if tag.term.lower() == tag_filter.lower(): + return True + return False -def parse_rss_feeds(urls): +def parse_rss_feeds(urls, tag_filter): entries = {} for url in urls: - entries = parse_single_rss_feed(url, entries) + entries = parse_single_rss_feed(url, entries, tag_filter) return entries