From ee7927ec9e73a4394aec6194e3d4136145f1d818 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 19 Jun 2019 12:52:28 -0700 Subject: [PATCH] Add tag support to server --- inquisitor/__main__.py | 9 +++++---- inquisitor/app.py | 35 ++++++++++++++++++++++++++-------- inquisitor/dungeon.py | 8 +++++++- inquisitor/item.py | 15 ++++++--------- inquisitor/templates/feed.html | 15 +++++++++++++-- 5 files changed, 58 insertions(+), 24 deletions(-) diff --git a/inquisitor/__main__.py b/inquisitor/__main__.py index 288817b..af2b57a 100644 --- a/inquisitor/__main__.py +++ b/inquisitor/__main__.py @@ -49,10 +49,11 @@ def main(): args = parse_args(commands) # Configure logging. - loglevel = getattr(logging, args.log.upper()) - if not isinstance(loglevel, int): - raise ValueError("Invalid log level: {}".format(args.log)) - logging.basicConfig(format='[%(levelname)s:%(filename)s:%(lineno)d] %(message)s', level=loglevel) + if args.command != 'run': + loglevel = getattr(logging, args.log.upper()) + if not isinstance(loglevel, int): + raise ValueError("Invalid log level: {}".format(args.log)) + logging.basicConfig(format='[%(levelname)s:%(filename)s:%(lineno)d] %(message)s', level=loglevel) # Execute command. if args.command: diff --git a/inquisitor/app.py b/inquisitor/app.py index e592076..aebb68e 100644 --- a/inquisitor/app.py +++ b/inquisitor/app.py @@ -1,5 +1,5 @@ # Standard library imports -from datetime import datetime +import datetime import logging # Third party imports @@ -18,27 +18,46 @@ console.setFormatter(formatter) logger.addHandler(console) app = Flask(__name__) +dungeon = dungeonlib.Dungeon("dungeon") +def list_filter(whitelist=None, blacklist=None): + if whitelist is not None: + return lambda item: any([tag in whitelist for tag in item['tags']]) + if blacklist is not None: + return lambda item: not any([tag in blacklist for tag in item['tags']]) + return lambda s: True + +@app.template_filter("datetimeformat") +def datetimeformat(value, formatstr="%Y-%m-%d %H:%M:%S"): + if value is None: + return "" + dt = datetime.datetime.fromtimestamp(value) + return dt.strftime(formatstr) + @app.route("/") def root(): dungeon = dungeonlib.Dungeon("dungeon") + filter_lambda = list_filter( + whitelist=request.args.get('wl'), + blacklist=request.args.get('bl')) active_items = [] - for cell in dungeon: - for item_id in dungeon[cell]: + for cell_name in dungeon: + cell = dungeon[cell_name] + for item_id in cell: try: - item = dungeon[cell][item_id] - if item['active']: - active_items.append(item) + item = cell[item_id] except: - logger.error("Exception reading {}/{}".format(cell, item_id)) + logger.error("Exception reading {}/{}".format(cell_name, item_id)) + continue + if item['active'] and filter_lambda(item): + active_items.append(item) logger.info("Found {} active items".format(len(active_items))) return render_template("feed.html", items=active_items[:100]) @app.route("/deactivate/", methods=['POST']) def deactivate(): - dungeon = dungeonlib.Dungeon("dungeon") params = request.get_json() if 'source' not in params and 'itemid' not in params: logger.error("Bad request params: {}".format(params)) diff --git a/inquisitor/dungeon.py b/inquisitor/dungeon.py index fed50c3..93a7912 100644 --- a/inquisitor/dungeon.py +++ b/inquisitor/dungeon.py @@ -35,7 +35,13 @@ class ReadableItem(): self.item = ast.literal_eval(f.read()) def __getitem__(self, key): - return self.item[key] + if key in self.item: + return self.item[key] + else: + if key == 'tags': + return [] + else: + return None def __setitem__(self, key, value): raise TypeError("ReadableItem is not writable") diff --git a/inquisitor/item.py b/inquisitor/item.py index b5f7f87..8665372 100644 --- a/inquisitor/item.py +++ b/inquisitor/item.py @@ -7,7 +7,7 @@ import logging logger = logging.getLogger("inquisitor.item") -def create_item(source, item_id, title, link=None, ts=None, author=None, body=None): +def create_item(source, item_id, title, link=None, ts=None, author=None, body=None, tags=None): import time item = { 'id': item_id, @@ -15,15 +15,12 @@ def create_item(source, item_id, title, link=None, ts=None, author=None, body=No 'active': True, 'created': time.time(), 'title': title, + 'link': link, + 'time': ts, + 'author': author, + 'body': body, + 'tags': [] if tags is None else tags, } - if link is not None: - item['link'] = link - if ts is not None: - item['time'] = ts - if author is not None: - item['author'] = author - if body is not None: - item['body'] = body return item import builtins diff --git a/inquisitor/templates/feed.html b/inquisitor/templates/feed.html index fd619d6..ff4ef44 100644 --- a/inquisitor/templates/feed.html +++ b/inquisitor/templates/feed.html @@ -38,8 +38,19 @@ {% else %} {{item.title}}
{% endif %} - {{item.time_readable}} {{item.author}}
- {{item.source}} {{item.id}} {{item.created}} + {% if item.author or item.time %} + + {% if item.author %} + {{item.author}} + {% endif %} + {% if item.time %} + {{item.time|datetimeformat}} + {% endif %} +
+ {% endif %} + + {{item.source}} {{item.id}} {{item.created|datetimeformat}} + {% endfor %} {% else %}