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

View File

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

View File

@ -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

View File

@ -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:

View File

@ -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)
for column in columns:
print(column.title)
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()
print(len(column.entries))

View File

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