Implement timestamps #6

Merged
ravenscroftj merged 3 commits from james/feature/timestamps into master 2022-10-30 15:48:29 +00:00
2 changed files with 24 additions and 16 deletions

View File

@ -1,6 +1,6 @@
[tool.poetry] [tool.poetry]
name = "pymicrocosm" name = "pymicrocosm"
version = "0.1.0" version = "0.2.0"
description = "A tiny python-based micropub endpoint that supports a Gitea + drone static website" description = "A tiny python-based micropub endpoint that supports a Gitea + drone static website"
authors = ["James Ravenscroft <ravenscroftj@gmail.com>"] authors = ["James Ravenscroft <ravenscroftj@gmail.com>"]
license = "AGPL-3.0" license = "AGPL-3.0"

View File

@ -7,6 +7,8 @@ import giteapy
import giteapy.rest import giteapy.rest
import time import time
import base64 import base64
from werkzeug.datastructures import FileStorage from werkzeug.datastructures import FileStorage
import yaml import yaml
@ -27,7 +29,7 @@ PERMITTED_DOMAIN = os.environ.get(
ENTITY_TYPE_PLURAL_MAP = { ENTITY_TYPE_PLURAL_MAP = {
"reply": "replies", "reply": "replies",
"watch":"watches" "watch": "watches"
} }
core_bp = Blueprint("core", __name__) core_bp = Blueprint("core", __name__)
@ -81,10 +83,10 @@ _api_client = None
def process_photo_url(now: datetime, doc: Dict[str, List[str]], suffix: str = ""): def process_photo_url(created_at: datetime, doc: Dict[str, List[str]], suffix: str = ""):
"""Process photo submitted via URL""" """Process photo submitted via URL"""
now_ts = int(time.mktime(now.timetuple())) now_ts = int(time.mktime(created_at.timetuple()))
photo_urls = [] photo_urls = []
@ -103,9 +105,9 @@ def process_photo_url(now: datetime, doc: Dict[str, List[str]], suffix: str = ""
# generate local filename # generate local filename
filename = os.path.join(os.environ.get( filename = os.path.join(os.environ.get(
'MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), str(now_ts) + f"{now_ts}_{suffix}_{i}_{ext}") 'MICROPUB_MEDIA_PATH'), created_at.strftime("%Y/%m/%d"), str(now_ts) + f"{now_ts}_{suffix}_{i}_{ext}")
photo_url = 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) + f"{now_ts}_{suffix}_{i}_{ext}") 'MICROPUB_MEDIA_URL_PREFIX'), created_at.strftime("%Y/%m/%d"), str(now_ts) + f"{now_ts}_{suffix}_{i}_{ext}")
photo_urls.append(photo_url) photo_urls.append(photo_url)
@ -123,10 +125,10 @@ def process_photo_url(now: datetime, doc: Dict[str, List[str]], suffix: str = ""
return photo_urls return photo_urls
def process_photo_upload(now: datetime, file: FileStorage, suffix: str=""): def process_photo_upload(created_at: datetime, file: FileStorage, suffix: str=""):
"""Process photo directly uploaded to micropub""" """Process photo directly uploaded to micropub"""
now_ts = int(time.mktime(now.timetuple())) now_ts = int(time.mktime(created_at.timetuple()))
if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy': if os.environ.get('MICROPUB_IMAGE_STRATEGY') == 'copy':
@ -134,9 +136,9 @@ def process_photo_upload(now: datetime, file: FileStorage, suffix: str=""):
# generate local filename # generate local filename
filename = os.path.join(os.environ.get( filename = os.path.join(os.environ.get(
'MICROPUB_MEDIA_PATH'), now.strftime("%Y/%m/%d"), f"{now_ts}_{suffix}{ext}") 'MICROPUB_MEDIA_PATH'), created_at.strftime("%Y/%m/%d"), f"{now_ts}_{suffix}{ext}")
photo_url = os.path.join(os.environ.get( photo_url = os.path.join(os.environ.get(
'MICROPUB_MEDIA_URL_PREFIX'), now.strftime("%Y/%m/%d"), f"{now_ts}_{suffix}{ext}") 'MICROPUB_MEDIA_URL_PREFIX'), created_at.strftime("%Y/%m/%d"), f"{now_ts}_{suffix}{ext}")
# 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)):
@ -150,9 +152,9 @@ def process_photo_upload(now: datetime, file: FileStorage, suffix: str=""):
return None return None
def init_frontmatter(now: datetime, post_type: str, name: Optional[str]=None): def init_frontmatter(created_at: datetime, post_type: str, name: Optional[str]=None):
now_ts = int(time.mktime(now.timetuple())) now_ts = int(time.mktime(created_at.timetuple()))
if name: if name:
if isinstance(name, list): if isinstance(name, list):
@ -164,18 +166,18 @@ def init_frontmatter(now: datetime, post_type: str, name: Optional[str]=None):
url = os.path.join("/", ENTITY_TYPE_PLURAL_MAP.get(post_type, post_type + "s"), url = os.path.join("/", ENTITY_TYPE_PLURAL_MAP.get(post_type, post_type + "s"),
now.strftime("%Y/%m/%d"), slug) created_at.strftime("%Y/%m/%d"), slug)
print(os.environ.get( print(os.environ.get(
'CONTENT_PREFIX')) 'CONTENT_PREFIX'))
file_path = os.path.join(os.environ.get( file_path = os.path.join(os.environ.get(
'CONTENT_PREFIX'), ENTITY_TYPE_PLURAL_MAP.get(post_type, post_type + "s"), now.strftime("%Y/%m/%d"), slug + ".md") 'CONTENT_PREFIX'), ENTITY_TYPE_PLURAL_MAP.get(post_type, post_type + "s"), created_at.strftime("%Y/%m/%d"), slug + ".md")
frontmatter = { frontmatter = {
"url": url, "url": url,
"type": post_type, "type": post_type,
"date": now.isoformat(sep='T'), "date": created_at.isoformat(sep='T'),
} }
return frontmatter, file_path return frontmatter, file_path
@ -321,7 +323,13 @@ def process_json_post():
props = body['properties'] props = body['properties']
entry_type = detect_entry_type(props) entry_type = detect_entry_type(props)
now = datetime.now() if 'published' in props:
from dateutil import parser
now = parser.parse(props['published'][0])
else:
now = datetime.now()
frontmatter, file_path = init_frontmatter(now, entry_type, props.get('name')) frontmatter, file_path = init_frontmatter(now, entry_type, props.get('name'))