random sort order and limit on rss feed

This commit is contained in:
crunk 2023-06-03 19:27:41 +02:00
parent 6d894cc33d
commit a4bfcc04e1
6 changed files with 53 additions and 34 deletions

10
app.py
View File

@ -1,13 +1,15 @@
import os import os
from flask import Flask from flask import Flask
# from flask_sqlalchemy import SQLAlchemy # from flask_sqlalchemy import SQLAlchemy
#db = SQLAlchemy() # db = SQLAlchemy()
#migrate = Migrate() # migrate = Migrate()
def create_app(): def create_app():
APP = Flask(__name__) APP = Flask(__name__)
#APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data/crunk_data.db" # APP.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///data/crunk_data.db"
#APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True # APP.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True
# db.init_app(APP) # db.init_app(APP)
# migrate.init_app(APP, db, render_as_batch=True) # migrate.init_app(APP, db, render_as_batch=True)

View File

@ -1,7 +1,12 @@
import random
from parse_rss_feeds import parse_rss_feeds
class Column: class Column:
def __init__(self, title, entries): def __init__(self, title, urls):
self.title = title self.title = title
self.entries = entries self.urls = urls
self.entries = None
self.sort_order = None self.sort_order = None
self.limit = None self.limit = None
@ -10,3 +15,21 @@ class Column:
def set_limit(self, limit): def set_limit(self, limit):
self.limit = 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()

View File

@ -2,10 +2,10 @@
id = 1 id = 1
urls = ["https://varia.zone/logs/x-y/feed.rss.xml","https://varia.zone/logs/x-y-protocols/feed.rss.xml"] urls = ["https://varia.zone/logs/x-y/feed.rss.xml","https://varia.zone/logs/x-y-protocols/feed.rss.xml"]
title = "code" title = "code"
limit = 20
sort_order = "random"
[[column]] [[column]]
id = 2 id = 2
urls = ["https://post.lurk.org/@cmos4040.rss"] urls = ["https://post.lurk.org/@cmos4040.rss"]
sort_order = "chronological"
title = "circulations" title = "circulations"
limit = 100

View File

@ -1,6 +1,7 @@
from feedparser import parse from feedparser import parse
import random import random
def _parse_single_rss_feed(url): def _parse_single_rss_feed(url):
feed = parse(url) feed = parse(url)
entries = {} entries = {}
@ -15,6 +16,7 @@ def _parse_single_rss_feed(url):
entrylist.append(entry.description) entrylist.append(entry.description)
return entries return entries
def parse_rss_feeds(urls): def parse_rss_feeds(urls):
entries = {} entries = {}
for url in urls: for url in urls:

View File

@ -4,17 +4,18 @@ from parse_rss_feeds import parse_rss_feeds
with open("columns.toml", "rb") as f: with open("columns.toml", "rb") as f:
feeds_dict = tomli.load(f) column_dict = tomli.load(f)
columns_file = column_dict["column"]
feeds_file = feeds_dict["column"]
columns = [] columns = []
for feed_from_file in feeds_file: for column_from_file in columns_file:
entries = parse_rss_feeds(feed_from_file["urls"]) urls = column_from_file["urls"]
title = feed_from_file["title"] title = column_from_file["title"]
column = Column(title=title, entries=entries) column = Column(title=title, urls=urls)
if "limit" in feed_from_file:
print(feed_from_file["limit"])
columns.append(column)
for column in columns: if "limit" in column_from_file:
print(column.title) 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()
print(len(column.entries))

View File

@ -1,26 +1,14 @@
from flask import ( from flask import (
render_template, render_template,
redirect,
) )
import tomli import tomli
from parse_rss_feeds import parse_rss_feeds
from app import create_app from app import create_app
from column import Column from column import Column
APP = create_app() 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("/") @APP.route("/")
def index(): def index():
with open("columns.toml", "rb") as f: with open("columns.toml", "rb") as f:
@ -28,13 +16,16 @@ def index():
columns_file = column_dict["column"] columns_file = column_dict["column"]
columns = [] columns = []
for column_from_file in columns_file: 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"] title = column_from_file["title"]
column = Column(title=title, entries=entries) column = Column(title=title, urls=urls)
if "limit" in column_from_file: if "limit" in column_from_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"])
column.load_content_from_feeds()
columns.append(column) columns.append(column)
return render_template("index.html", columns=columns) return render_template("index.html", columns=columns)