From 47033c9cd15fd446b4369f798a6e1fa100cd27c5 Mon Sep 17 00:00:00 2001 From: Solderpunk Date: Wed, 20 May 2020 19:16:45 +0200 Subject: Extract datestamps from filename if possible. --- gemfeed.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/gemfeed.py b/gemfeed.py index 1200ada..7143263 100755 --- a/gemfeed.py +++ b/gemfeed.py @@ -4,6 +4,7 @@ import datetime import glob import os import os.path +import re import stat import urllib.parse @@ -93,13 +94,30 @@ def populate_entry_from_file(filename, base_url, entry): url = urljoin(base_url, os.path.basename(filename)) entry.guid(url) entry.link(href=url, rel="alternate") - updated = os.path.getctime(filename) - updated = datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc) + updated = get_update_time(filename) entry.updated(updated) default_title = os.path.splitext(os.path.basename(filename))[0] title = extract_first_heading(filename, default_title) entry.title(title) +def get_update_time(filename): + """ + Return an update time for a Gemini file. + + If the filename begins with an ISO8601 date stamp, that date + (with a time of midnight) will be used. Otherwise, the file + "creation time" (which in unix is actually the time of last + metadata update) will be used instead as a best estimate. + """ + # Check for leading YYYY-MM-DD + basename = os.path.basename(filename) + if re.search("^[0-9]{4}-[01][0-9]-[0-3][0-9]", basename): + date = basename[0:10] + " Z" # Add UTC marker + return datetime.datetime.strptime(date, "%Y-%m-%d %z") + else: + updated = os.path.getctime(filename) + return datetime.datetime.fromtimestamp(updated, tz=datetime.timezone.utc) + def build_feed(directory, base_url, output="atom.xml", n=10, title="", subtitle="", author="", email="", verbose=False): """ -- cgit v1.2.3-54-g00ecf