diff --git a/app.py b/app.py index 10ec038..401b94d 100644 --- a/app.py +++ b/app.py @@ -1,13 +1,15 @@ import os from flask import Flask + # from flask_sqlalchemy import SQLAlchemy -#db = SQLAlchemy() -#migrate = Migrate() +# db = SQLAlchemy() +# migrate = Migrate() + def create_app(): APP = Flask(__name__) - #APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data/crunk_data.db" - #APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True + # APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data/crunk_data.db" + # APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True # db.init_app(APP) # migrate.init_app(APP, db, render_as_batch=True) diff --git a/column.py b/column.py index fe1e7e0..192a9be 100644 --- a/column.py +++ b/column.py @@ -1,7 +1,12 @@ +import random +from parse_rss_feeds import parse_rss_feeds + + class Column: - def __init__(self, title, entries): + def __init__(self, title, urls): self.title = title - self.entries = entries + self.urls = urls + self.entries = None self.sort_order = None self.limit = None @@ -10,3 +15,21 @@ class Column: def set_limit(self, limit): self.limit = limit + + def _sort_by_order(self): + if self.sort_order.lower() == "random": + entrylist = list(self.entries.items()) + random.shuffle(entrylist) + self.entries = dict(entrylist) + + def _enforce_limit(self): + while len(self.entries) > self.limit: + self.entries.popitem() + + def load_content_from_feeds(self): + entries = parse_rss_feeds(self.urls) + self.entries = entries + if self.sort_order: + self._sort_by_order() + if self.limit: + self._enforce_limit() diff --git a/columns.toml b/columns.toml index 27fd1e2..db26028 100644 --- a/columns.toml +++ b/columns.toml @@ -2,10 +2,10 @@ id = 1 urls = ["https://varia.zone/logs/x-y/feed.rss.xml","https://varia.zone/logs/x-y-protocols/feed.rss.xml"] title = "code" +limit = 20 +sort_order = "random" [[column]] id = 2 urls = ["https://post.lurk.org/@cmos4040.rss"] -sort_order = "chronological" title = "circulations" -limit = 100 diff --git a/parse_rss_feeds.py b/parse_rss_feeds.py index 20bc71b..f37602f 100644 --- a/parse_rss_feeds.py +++ b/parse_rss_feeds.py @@ -1,6 +1,7 @@ from feedparser import parse import random + def _parse_single_rss_feed(url): feed = parse(url) entries = {} @@ -15,6 +16,7 @@ def _parse_single_rss_feed(url): entrylist.append(entry.description) return entries + def parse_rss_feeds(urls): entries = {} for url in urls: diff --git a/quick_test.py b/quick_test.py index 1097dfa..53b18fc 100644 --- a/quick_test.py +++ b/quick_test.py @@ -4,17 +4,18 @@ from parse_rss_feeds import parse_rss_feeds with open("columns.toml", "rb") as f: - feeds_dict = tomli.load(f) - -feeds_file = feeds_dict["column"] + column_dict = tomli.load(f) +columns_file = column_dict["column"] columns = [] -for feed_from_file in feeds_file: - entries = parse_rss_feeds(feed_from_file["urls"]) - title = feed_from_file["title"] - column = Column(title=title, entries=entries) - if "limit" in feed_from_file: - print(feed_from_file["limit"]) - columns.append(column) +for column_from_file in columns_file: + urls = column_from_file["urls"] + title = column_from_file["title"] + column = Column(title=title, urls=urls) + + if "limit" in column_from_file: + column.set_limit(column_from_file["limit"]) + if "sort_order" in column_from_file: + column.set_sort_order(column_from_file["sort_order"]) -for column in columns: - print(column.title) + column.load_content_from_feeds() + print(len(column.entries)) diff --git a/start.py b/start.py index 4aa5682..e25e339 100644 --- a/start.py +++ b/start.py @@ -1,26 +1,14 @@ from flask import ( render_template, - redirect, ) import tomli -from parse_rss_feeds import parse_rss_feeds from app import create_app from column import Column APP = create_app() -@APP.route("/singlefeed") -def singlefeed(): - with open("columns.toml", "rb") as f: - feeds_dict = tomli.load(f) - feeds = feeds_dict["column"] - - feed = parse_rss_feeds(feeds[0]["urls"][0]) - return render_template("singlefeed.html", feed=feed) - - @APP.route("/") def index(): with open("columns.toml", "rb") as f: @@ -28,13 +16,16 @@ def index(): columns_file = column_dict["column"] columns = [] for column_from_file in columns_file: - entries = parse_rss_feeds(column_from_file["urls"]) + urls = column_from_file["urls"] title = column_from_file["title"] - column = Column(title=title, entries=entries) + column = Column(title=title, urls=urls) + if "limit" in column_from_file: column.set_limit(column_from_file["limit"]) if "sort_order" in column_from_file: column.set_sort_order(column_from_file["sort_order"]) + + column.load_content_from_feeds() columns.append(column) return render_template("index.html", columns=columns)