Extract datestamps from filename if possible.

This commit is contained in:
Solderpunk 2020-05-20 19:16:45 +02:00
parent 0e76336905
commit 47033c9cd1
1 changed files with 20 additions and 2 deletions

View File

@ -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):
"""