Restore basic web feed functionality

This commit is contained in:
Tim Van Baak 2019-12-17 00:39:04 -08:00
parent 1278d917bb
commit 1a75c45049
3 changed files with 29 additions and 60 deletions

View File

@ -1,25 +1,17 @@
# Standard library imports # Standard library imports
import datetime import os
import logging
import traceback import traceback
# Third party imports # Third party imports
from flask import Flask, render_template, request, jsonify from flask import Flask, render_template, request, jsonify
# Application imports # Application imports
import dungeon as dungeonlib from configs import logger, DUNGEON_PATH
import loader
import timestamp
# Globals # Globals
logger = logging.getLogger("inquisitor.app")
logger.setLevel(logging.INFO)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter('[%(asctime)s %(levelname)s:%(filename)s:%(lineno)d] %(message)s')
console.setFormatter(formatter)
logger.addHandler(console)
app = Flask(__name__) app = Flask(__name__)
dungeon = dungeonlib.Dungeon("dungeon")
def make_query_link(text, wl, bl): def make_query_link(text, wl, bl):
@ -30,16 +22,11 @@ def make_query_link(text, wl, bl):
return '<a href="{1}">{0}</a>'.format(text, query) return '<a href="{1}">{0}</a>'.format(text, query)
@app.template_filter("datetimeformat") @app.template_filter("datetimeformat")
def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"): def datetimeformat(value):
if value is None: return timestamp.stamp_to_readable(value) if value is not None else ""
return ""
dt = datetime.datetime.fromtimestamp(value)
return dt.strftime(formatstr)
@app.route("/") @app.route("/")
def root(): def root():
dungeon = dungeonlib.Dungeon("dungeon")
# Determine exclusion filters # Determine exclusion filters
filters = [] filters = []
wl_param = request.args.get('only') wl_param = request.args.get('only')
@ -53,19 +40,10 @@ 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()
active_items = [] active_items = []
active_tags = set() active_tags = set()
item_read_exceptions = [] for item in items:
for cell in dungeon:
for item_id in dungeon[cell]:
try:
item = dungeon[cell][item_id]
except:
msg = "Exception reading {}/{}".format(cell, item_id)
logger.error(msg)
item_read_exceptions.append(msg)
item_read_exceptions.append(traceback.format_exc())
continue
if item['active']: if item['active']:
active_tags |= set(item['tags']) active_tags |= set(item['tags'])
total += 1 total += 1
@ -75,13 +53,13 @@ def root():
active_items.sort(key=lambda i: i['time'] if 'time' in i and i['time'] else 0) active_items.sort(key=lambda i: i['time'] if 'time' in i and i['time'] else 0)
logger.info("Returning {} of {} items".format(len(active_items), total)) logger.info("Returning {} of {} items".format(len(active_items), total))
if item_read_exceptions: if errors:
read_ex = { read_ex = {
'title': 'Read errors', 'title': 'Read errors',
'body': "<pre>{}</pre>".format("\n\n".join(item_read_exceptions)), 'body': "<pre>{}</pre>".format("\n\n".join(errors)),
'created': None, 'created': None,
} }
active_items = [read_ex] + active_items active_items.insert(0, read_ex)
if total > 0: if total > 0:
# Create the feed control item # Create the feed control item
@ -96,18 +74,17 @@ def root():
'body': body, 'body': body,
'created': None, 'created': None,
} }
active_items = [feed_control] + active_items active_items.insert(0, feed_control)
return render_template("feed.html", items=active_items[:100]) return render_template("feed.html", items=active_items[:100])
@app.route("/deactivate/", methods=['POST']) @app.route("/deactivate/", methods=['POST'])
def deactivate(): def deactivate():
dungeon = dungeonlib.Dungeon("dungeon")
params = request.get_json() params = request.get_json()
if 'source' not in params and 'itemid' not in params: if 'source' not in params and 'itemid' not in params:
logger.error("Bad request params: {}".format(params)) logger.error("Bad request params: {}".format(params))
item = dungeon[params['source']][params['itemid']] item = loader.WritethroughDict(os.path.join(DUNGEON_PATH, params['source'], params['itemid'] + '.item'))
item.deactivate() item['active'] = False
return jsonify({'active': item['active']}) return jsonify({'active': item['active']})

View File

@ -163,16 +163,12 @@ def command_feed(args):
print() print()
# def command_run(args): def command_run(args):
# """Run the default Flask server.""" """Run the default Flask server."""
# pass try:
from app import app
# def run_flask_server(args): app.run()
# """Run the default flask server serving from the specified dungeon.""" return 0
# try: except Exception as e:
# from app import app logger.error(e)
# app.run() return -1
# return 0
# except Exception as e:
# logger.error(e)
# return (-1)

View File

@ -1,21 +1,17 @@
""" """
An example itemsource that produces an item with the current date. An example itemsource that produces an item with the current date.
ANy args provided will be added to the item body.
Fetch new items with `python inquisitor update --sources example` Fetch new items with `python inquisitor update --sources example`
or `--sources example:argument`.
""" """
# Standard library imports # Standard library imports
from datetime import date from datetime import date
import time import time
def fetch_new(state, args): def fetch_new(state):
now = date.today() now = date.today()
item = create_item( item = {
"example", 'source': "example",
'{}-{}-{}'.format(now.year, now.month, now.day), 'id': '{}-{}-{}'.format(now.year, now.month, now.day),
"Today is {}-{}-{}".format(now.year, now.month, now.day), 'title': "Today is {}-{}-{}".format(now.year, now.month, now.day),
ts=time.time(), }
body=args
)
return [item] return [item]