Browse Source

random sort order and limit on rss feed

main
crunk 1 year ago
parent
commit
a4bfcc04e1
  1. 10
      app.py
  2. 27
      column.py
  3. 4
      columns.toml
  4. 2
      parse_rss_feeds.py
  5. 25
      quick_test.py
  6. 19
      start.py

10
app.py

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

27
column.py

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

4
columns.toml

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

2
parse_rss_feeds.py

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

25
quick_test.py

@ -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"]) if "limit" in column_from_file:
columns.append(column) 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: column.load_content_from_feeds()
print(column.title) print(len(column.entries))

19
start.py

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

Loading…
Cancel
Save