Browse Source

tag filter

main
crunk 9 months ago
parent
commit
85d5c134b5
  1. 3
      app.py
  2. 6
      column.py
  3. 1
      columns.toml
  4. 15
      parse_rss_feeds.py

3
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)

6
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()

1
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"]

15
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

Loading…
Cancel
Save