implement categories/tags listing

This commit is contained in:
James Ravenscroft 2021-12-24 11:03:28 +00:00
parent c7ea1bc1c8
commit 1fdfbe54af
1 changed files with 65 additions and 45 deletions

View File

@ -10,7 +10,7 @@ import yaml
from slugify import slugify
from datetime import datetime
from xml.etree import ElementTree
from flask import Flask, request, url_for, Response
from requests import api
from flask_micropub import MicropubClient
@ -114,8 +114,10 @@ def req():
r = requests.get(doc['photo'])
# generate local filename
filename = os.path.join(os.environ.get('MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
photo_url = os.path.join(os.environ.get('MICROPUB_MEDIA_URL_PREFIX'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
filename = os.path.join(os.environ.get(
'MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
photo_url = os.path.join(os.environ.get(
'MICROPUB_MEDIA_URL_PREFIX'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg")
# make directory if needed
if not os.path.exists(os.path.dirname(filename)):
@ -129,7 +131,6 @@ def req():
else:
photo_url = doc['photo']
docstr = f"![image]({photo_url}) \n\n {doc['content']}"
else:
docstr = doc['content']
@ -155,11 +156,19 @@ def req():
# return {"hello": "world"}
@app.route("/", methods=['GET'])
@authed_endpoint
def index():
def parse_categories():
if request.args.get('q') == 'config':
strategy = os.environ.get('MICROPUB_CATEGORY_LIST_STRATEGY')
if strategy == 'feed':
tree = ElementTree.parse(os.environ.get('MICROPUB_CATEGORY_LIST_FILE'))
tags = tree.findall('.//item/title')
return {"categories": [tag.text for tag in tags] }
def generate_config_json():
return {
"media-endpoint": "/micropub/media",
"syndicate-to": [
@ -193,6 +202,17 @@ def index():
}
@app.route("/", methods=['GET'])
@authed_endpoint
def index():
if request.args.get('q') == 'config':
return generate_config_json()
elif request.args.get('q') == 'category':
return parse_categories()
@app.route('/form', methods=['GET'])
def authform():
return """