Add feed command

This commit is contained in:
Tim Van Baak 2019-12-17 00:11:53 -08:00
parent 69627d3fda
commit 1278d917bb
2 changed files with 74 additions and 7 deletions

View File

@ -116,6 +116,53 @@ def command_add(args):
logger.info(item)
def command_feed(args):
"""Print the current feed."""
if not os.path.isdir(DUNGEON_PATH):
logger.error("Couldn't find dungeon. Set INQUISITOR_DUNGEON or cd to parent folder of ./dungeon")
return -1
import shutil
import loader
import timestamp
items, errors = loader.load_active_items()
if not items and not errors:
print("Feed is empty")
return 0
if errors:
items.insert(0, {
'id': 'read-errors',
'title': '{} read errors'.format(len(errors)),
'body': "\n".join(errors)
})
size = shutil.get_terminal_size((80, 20))
width = min(80, size.columns)
for item in items:
titles = [item['title']]
while len(titles[-1]) > width - 4:
i = titles[-1][:width - 4].rfind(' ')
titles = titles[:-1] + [titles[-1][:i].strip(), titles[-1][i:].strip()]
print('+' + (width - 2) * '-' + '+')
for title in titles:
print("| {0:<{1}} |".format(title, width - 4))
print("|{0:<{1}}|".format("", width - 2))
info1 = ""
if item['author']:
info1 += item['author'] + " "
if item['time']:
info1 += timestamp.stamp_to_readable(item['time'])
print("| {0:<{1}} |".format(info1, width - 4))
info2 = "{0} {1} {2}".format(
item['source'], item['id'], timestamp.stamp_to_readable(item['created']))
print("| {0:<{1}} |".format(info2, width - 4))
print('+' + (width - 2) * '-' + '+')
print()
# def command_run(args):
# """Run the default Flask server."""
# pass

View File

@ -52,10 +52,30 @@ def load_items(source_name):
items = {}
errors = []
for filename in os.listdir(cell_path):
try:
path = os.path.join(cell_path, filename)
item = WritethroughDict(path)
items[item['id']] = item
except Exception as e:
errors.append(filename)
return items, errors
if filename.endswith('.item'):
try:
path = os.path.join(cell_path, filename)
item = WritethroughDict(path)
items[item['id']] = item
except Exception as e:
errors.append(filename)
return items, errors
def load_active_items():
"""
Returns a list of active items and a list of unreadable items.
"""
items = []
errors = []
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:
path = os.path.join(cell_path, filename)
item = WritethroughDict(path)
if item['active']:
items.append(item)
except Exception as e:
errors.append(filename)
return items, errors