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 slugify import slugify
from datetime import datetime from datetime import datetime
from xml.etree import ElementTree
from flask import Flask, request, url_for, Response from flask import Flask, request, url_for, Response
from requests import api from requests import api
from flask_micropub import MicropubClient from flask_micropub import MicropubClient
@ -110,26 +110,27 @@ def req():
if 'photo' in doc: if 'photo' in doc:
if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy': if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy':
# download the photo # download the photo
r = requests.get(doc['photo']) r = requests.get(doc['photo'])
# generate local filename # generate local filename
filename = os.path.join(os.environ.get('MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg") filename = os.path.join(os.environ.get(
photo_url = os.path.join(os.environ.get('MICROPUB_MEDIA_URL_PREFIX'), now.strftime("%Y/%m/%d"), str(now_ts) + ".jpg") '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 # make directory if needed
if not os.path.exists(os.path.dirname(filename)): if not os.path.exists(os.path.dirname(filename)):
os.makedirs(os.path.dirname(filename)) os.makedirs(os.path.dirname(filename))
with open(filename, 'wb') as f: with open(filename, 'wb') as f:
f.write(r.content) f.write(r.content)
# elif os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'gitea': # elif os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'gitea':
else: else:
photo_url = doc['photo'] photo_url = doc['photo']
docstr = f"![image]({photo_url}) \n\n {doc['content']}" docstr = f"![image]({photo_url}) \n\n {doc['content']}"
else: else:
docstr = doc['content'] docstr = doc['content']
@ -155,42 +156,61 @@ def req():
# return {"hello": "world"} # return {"hello": "world"}
def parse_categories():
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": [
{
"uid": "mastodon",
"name": "Mastodon"
}
],
"post-types": [
{
"type": "note",
"name": "Note"
},
{
"type": "article",
"name": "Blog Post"
},
{
"type": "photo",
"name": "Photo"
},
{
"type": "reply",
"name": "Reply"
},
{
"type": "bookmark",
"name": "Bookmark"
}
]
}
@app.route("/", methods=['GET']) @app.route("/", methods=['GET'])
@authed_endpoint @authed_endpoint
def index(): def index():
if request.args.get('q') == 'config': if request.args.get('q') == 'config':
return { return generate_config_json()
"media-endpoint": "/micropub/media",
"syndicate-to": [ elif request.args.get('q') == 'category':
{ return parse_categories()
"uid": "mastodon",
"name": "Mastodon"
}
],
"post-types": [
{
"type": "note",
"name": "Note"
},
{
"type": "article",
"name": "Blog Post"
},
{
"type": "photo",
"name": "Photo"
},
{
"type": "reply",
"name": "Reply"
},
{
"type": "bookmark",
"name": "Bookmark"
}
]
}
@app.route('/form', methods=['GET']) @app.route('/form', methods=['GET'])