From 27e04e601a187155ff3c8c5f46b9318f597db708 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Mon, 28 Dec 2020 17:46:57 -0800 Subject: [PATCH] Add subfeed routes --- inquisitor/app.py | 19 +++++++++++--- inquisitor/cli.py | 2 +- inquisitor/configs/__init__.py | 2 +- inquisitor/loader.py | 45 ++++++++++++++++++++-------------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/inquisitor/app.py b/inquisitor/app.py index 40d7925..041e86e 100644 --- a/inquisitor/app.py +++ b/inquisitor/app.py @@ -4,10 +4,10 @@ import os import traceback # 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 -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 # Globals @@ -27,6 +27,19 @@ def datetimeformat(value): @app.route("/") def root(): + return redirect(url_for('feed')) + +@app.route("/feed/") +def feed(): + return feed_for_sources(source_names=None) + +@app.route("/feed//") +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 filters = [] wl_param = request.args.get('only') @@ -40,7 +53,7 @@ def root(): # Get all active+filtered items and all active tags total = 0 - items, errors = loader.load_active_items() + items, errors = loader.load_active_items(source_names) active_items = [] active_tags = {} for item in items: diff --git a/inquisitor/cli.py b/inquisitor/cli.py index 29d9880..d34b3fb 100644 --- a/inquisitor/cli.py +++ b/inquisitor/cli.py @@ -173,7 +173,7 @@ def command_feed(args): from inquisitor import loader 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: print("Feed is empty") return 0 diff --git a/inquisitor/configs/__init__.py b/inquisitor/configs/__init__.py index f261c22..eabdaa5 100644 --- a/inquisitor/configs/__init__.py +++ b/inquisitor/configs/__init__.py @@ -1,4 +1,4 @@ from .resolver import data_path as DUNGEON_PATH from .resolver import source_path as SOURCES_PATH from .resolver import cache_path as CACHE_PATH -from .resolver import logger, add_logging_handler, init_default_logging \ No newline at end of file +from .resolver import logger, add_logging_handler, init_default_logging, subfeeds \ No newline at end of file diff --git a/inquisitor/loader.py b/inquisitor/loader.py index e475851..414620c 100644 --- a/inquisitor/loader.py +++ b/inquisitor/loader.py @@ -2,7 +2,7 @@ import os import json -from inquisitor.configs import DUNGEON_PATH +from inquisitor.configs import DUNGEON_PATH, logger from inquisitor import error from inquisitor import timestamp @@ -152,28 +152,35 @@ def load_items(source_name): errors.append(filename) 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 = [] errors = [] now = timestamp.now() - for cell_name in os.listdir(DUNGEON_PATH): - cell_path = os.path.join(DUNGEON_PATH, cell_name) - for filename in os.listdir(cell_path): - if filename.endswith('.item'): - try: - item = load_item(cell_name, filename[:-5]) - # The time-to-show field hides items until an expiry date. - if 'tts' in item: - tts_date = item['created'] + item['tts'] - if now < tts_date: - continue - # Don't show inactive items - if not item['active']: + check_list = source_names or os.listdir(DUNGEON_PATH) + for source_name in check_list: + source_path = os.path.join(DUNGEON_PATH, source_name) + if not os.path.isdir(source_path): + logger.warning(f'Skipping nonexistent source {source_name}') + continue + for filename in os.listdir(source_path): + if not filename.endswith('.item'): + continue + try: + item = load_item(source_name, filename[:-5]) + # The time-to-show field hides items until an expiry date. + if 'tts' in item: + tts_date = item['created'] + item['tts'] + if now < tts_date: continue - items.append(item) - except Exception: - errors.append(filename) + # Don't show inactive items + if not item['active']: + continue + items.append(item) + except Exception: + errors.append(filename) return items, errors