Add subfeed routes

This commit is contained in:
Tim Van Baak 2020-12-28 17:46:57 -08:00
parent 9843fd93c4
commit 27e04e601a
4 changed files with 44 additions and 24 deletions

View File

@ -4,10 +4,10 @@ import os
import traceback import traceback
# Third party imports # Third party imports
from flask import Flask, render_template, request, jsonify, abort from flask import Flask, render_template, request, jsonify, abort, redirect, url_for
# Application imports # Application imports
from inquisitor.configs import logger, DUNGEON_PATH, CACHE_PATH from inquisitor.configs import logger, DUNGEON_PATH, CACHE_PATH, subfeeds
from inquisitor import sources, loader, timestamp from inquisitor import sources, loader, timestamp
# Globals # Globals
@ -27,6 +27,19 @@ def datetimeformat(value):
@app.route("/") @app.route("/")
def root(): def root():
return redirect(url_for('feed'))
@app.route("/feed/")
def feed():
return feed_for_sources(source_names=None)
@app.route("/feed/<string:feed_name>/")
def subfeed(feed_name):
if feed_name not in subfeeds:
return abort(404)
return feed_for_sources(subfeeds[feed_name])
def feed_for_sources(source_names):
# Determine exclusion filters # Determine exclusion filters
filters = [] filters = []
wl_param = request.args.get('only') wl_param = request.args.get('only')
@ -40,7 +53,7 @@ def root():
# Get all active+filtered items and all active tags # Get all active+filtered items and all active tags
total = 0 total = 0
items, errors = loader.load_active_items() items, errors = loader.load_active_items(source_names)
active_items = [] active_items = []
active_tags = {} active_tags = {}
for item in items: for item in items:

View File

@ -173,7 +173,7 @@ def command_feed(args):
from inquisitor import loader from inquisitor import loader
from inquisitor import timestamp from inquisitor import timestamp
items, errors = loader.load_active_items() items, errors = loader.load_active_items(source_names=None)
if not items and not errors: if not items and not errors:
print("Feed is empty") print("Feed is empty")
return 0 return 0

View File

@ -1,4 +1,4 @@
from .resolver import data_path as DUNGEON_PATH from .resolver import data_path as DUNGEON_PATH
from .resolver import source_path as SOURCES_PATH from .resolver import source_path as SOURCES_PATH
from .resolver import cache_path as CACHE_PATH from .resolver import cache_path as CACHE_PATH
from .resolver import logger, add_logging_handler, init_default_logging from .resolver import logger, add_logging_handler, init_default_logging, subfeeds

View File

@ -2,7 +2,7 @@ import os
import json import json
from inquisitor.configs import DUNGEON_PATH from inquisitor.configs import DUNGEON_PATH, logger
from inquisitor import error from inquisitor import error
from inquisitor import timestamp from inquisitor import timestamp
@ -152,28 +152,35 @@ def load_items(source_name):
errors.append(filename) errors.append(filename)
return items, errors return items, errors
def load_active_items():
def load_active_items(source_names):
""" """
Returns a list of active items and a list of unreadable items. Returns a list of active items and a list of unreadable items. If
`source_names` is defined, load only from sources in that list.
""" """
items = [] items = []
errors = [] errors = []
now = timestamp.now() now = timestamp.now()
for cell_name in os.listdir(DUNGEON_PATH): check_list = source_names or os.listdir(DUNGEON_PATH)
cell_path = os.path.join(DUNGEON_PATH, cell_name) for source_name in check_list:
for filename in os.listdir(cell_path): source_path = os.path.join(DUNGEON_PATH, source_name)
if filename.endswith('.item'): if not os.path.isdir(source_path):
try: logger.warning(f'Skipping nonexistent source {source_name}')
item = load_item(cell_name, filename[:-5]) continue
# The time-to-show field hides items until an expiry date. for filename in os.listdir(source_path):
if 'tts' in item: if not filename.endswith('.item'):
tts_date = item['created'] + item['tts'] continue
if now < tts_date: try:
continue item = load_item(source_name, filename[:-5])
# Don't show inactive items # The time-to-show field hides items until an expiry date.
if not item['active']: if 'tts' in item:
tts_date = item['created'] + item['tts']
if now < tts_date:
continue continue
items.append(item) # Don't show inactive items
except Exception: if not item['active']:
errors.append(filename) continue
items.append(item)
except Exception:
errors.append(filename)
return items, errors return items, errors