diff options
author | Solderpunk <solderpunk@sdf.org> | 2020-05-20 19:16:45 +0200 |
---|---|---|
committer | Solderpunk <solderpunk@sdf.org> | 2020-05-20 19:16:45 +0200 |
commit | 47033c9cd15fd446b4369f798a6e1fa100cd27c5 (patch) | |
tree | f679604310e295c687b370ab61405817caac8fc5 | |
parent | 0e76336905c9df1b8365d8867027da1eab91720f (diff) |
Extract datestamps from filename if possible.
-rwxr-xr-x | gemfeed.py | 22 |
1 files changed, 20 insertions, 2 deletions
@@ -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): """ |