diff --git a/pyproject.toml b/pyproject.toml index 13f802c..18d727f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pymicrocosm" -version = "0.1.0" +version = "0.2.0" description = "A tiny python-based micropub endpoint that supports a Gitea + drone static website" authors = ["James Ravenscroft "] license = "AGPL-3.0" diff --git a/src/microcosm/__init__.py b/src/microcosm/__init__.py index e710181..52a1a94 100644 --- a/src/microcosm/__init__.py +++ b/src/microcosm/__init__.py @@ -7,6 +7,8 @@ import giteapy import giteapy.rest import time import base64 + + from werkzeug.datastructures import FileStorage import yaml @@ -27,7 +29,7 @@ PERMITTED_DOMAIN = os.environ.get( ENTITY_TYPE_PLURAL_MAP = { "reply": "replies", - "watch":"watches" + "watch": "watches" } 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""" - now_ts = int(time.mktime(now.timetuple())) + now_ts = int(time.mktime(created_at.timetuple())) photo_urls = [] @@ -103,9 +105,9 @@ def process_photo_url(now: datetime, doc: Dict[str, List[str]], suffix: str = "" # generate local filename 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( - '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) @@ -123,10 +125,10 @@ def process_photo_url(now: datetime, doc: Dict[str, List[str]], suffix: str = "" 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""" - now_ts = int(time.mktime(now.timetuple())) + now_ts = int(time.mktime(created_at.timetuple())) 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 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( - '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 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 -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 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"), - now.strftime("%Y/%m/%d"), slug) + created_at.strftime("%Y/%m/%d"), slug) print(os.environ.get( 'CONTENT_PREFIX')) 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 = { "url": url, "type": post_type, - "date": now.isoformat(sep='T'), + "date": created_at.isoformat(sep='T'), } return frontmatter, file_path @@ -321,7 +323,13 @@ def process_json_post(): props = body['properties'] 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'))