tag filter
This commit is contained in:
parent
56e5e582c7
commit
85d5c134b5
3
app.py
3
app.py
@ -41,6 +41,9 @@ def update_feeds(json_file):
|
|||||||
column.set_limit(column_from_file["limit"])
|
column.set_limit(column_from_file["limit"])
|
||||||
if "sort_order" in column_from_file:
|
if "sort_order" in column_from_file:
|
||||||
column.set_sort_order(column_from_file["sort_order"])
|
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()
|
column.load_content_from_feeds()
|
||||||
columns[title] = column.__dict__
|
columns[title] = column.__dict__
|
||||||
json_file.update(columns)
|
json_file.update(columns)
|
||||||
|
@ -13,6 +13,10 @@ class Column(object):
|
|||||||
self.limit = None
|
self.limit = None
|
||||||
self.sort_order = None
|
self.sort_order = None
|
||||||
self.entries = 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):
|
def set_sort_order(self, sort_order):
|
||||||
self.sort_order = sort_order
|
self.sort_order = sort_order
|
||||||
@ -55,7 +59,7 @@ class Column(object):
|
|||||||
self.entries.popitem()
|
self.entries.popitem()
|
||||||
|
|
||||||
def load_content_from_feeds(self):
|
def load_content_from_feeds(self):
|
||||||
entries = parse_rss_feeds(self.urls)
|
entries = parse_rss_feeds(self.urls, self.tag_filter)
|
||||||
self.entries = entries
|
self.entries = entries
|
||||||
if self.sort_order:
|
if self.sort_order:
|
||||||
self._sort_by_order()
|
self._sort_by_order()
|
||||||
|
@ -8,6 +8,7 @@ sort_order = "random"
|
|||||||
urls = ["https://post.lurk.org/@cmos4040.rss"]
|
urls = ["https://post.lurk.org/@cmos4040.rss"]
|
||||||
title = "circulations"
|
title = "circulations"
|
||||||
sort_order = "reverse"
|
sort_order = "reverse"
|
||||||
|
tag_filter = "electronics"
|
||||||
|
|
||||||
[[column]]
|
[[column]]
|
||||||
urls = ["https://post.lurk.org/tags/radio.rss"]
|
urls = ["https://post.lurk.org/tags/radio.rss"]
|
||||||
|
@ -3,10 +3,12 @@ import random
|
|||||||
from feedparser import parse
|
from feedparser import parse
|
||||||
|
|
||||||
|
|
||||||
def parse_single_rss_feed(url, entries):
|
def parse_single_rss_feed(url, entries, tag_filter):
|
||||||
feed = parse(url)
|
feed = parse(url)
|
||||||
entrylength = len(entries)
|
entrylength = len(entries)
|
||||||
for entrynumber, entry in enumerate(feed.entries):
|
for entrynumber, entry in enumerate(feed.entries):
|
||||||
|
if tag_filter and not contains_category(entry, tag_filter):
|
||||||
|
continue
|
||||||
if entry.has_key("title"):
|
if entry.has_key("title"):
|
||||||
entries[entry.title] = []
|
entries[entry.title] = []
|
||||||
entrylist = entries[entry.title]
|
entrylist = entries[entry.title]
|
||||||
@ -18,9 +20,16 @@ def parse_single_rss_feed(url, entries):
|
|||||||
entrylist.append(entry.published_parsed)
|
entrylist.append(entry.published_parsed)
|
||||||
return entries
|
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 = {}
|
entries = {}
|
||||||
for url in urls:
|
for url in urls:
|
||||||
entries = parse_single_rss_feed(url, entries)
|
entries = parse_single_rss_feed(url, entries, tag_filter)
|
||||||
return entries
|
return entries
|
||||||
|
Loading…
Reference in New Issue
Block a user