💾 Archived View for gemini.sh0.xyz › development › atom.xml.py captured on 2023-11-14 at 07:32:56.

View Raw

More Information

⬅️ Previous capture (2023-09-08)

-=-=-=-=-=-=-

#!/usr/bin/env python3

import re
import os
import time
from datetime import timezone
from datetime import datetime
import uuid

log_dir = "/srv/gemini/log"
#log_dir = "/home/jeff/gemini/log"
url = "gemini://gemini.sh0.xyz/log"
author = "jecxjo"
title = "sh0.xyz captain's log"

updated = datetime.strptime("1970-01-01 00:00:00.0000", "%Y-%m-%d %H:%M:%S.%f")
feed_urn = uuid.uuid5(uuid.NAMESPACE_DNS, url)
body = ""

files = sorted(filter(lambda x:
    x.endswith(".gmi") and not x.endswith("index.gmi"),
    os.listdir(log_dir)))

for f in files:
    with open(os.path.join(log_dir, f)) as file:
        contents = file.read()
        entry_title = re.findall("^# .*|$", contents)[0][1:].strip()
        entry_link = "{}/{}".format(url,f)
        entry_urn = uuid.uuid5(uuid.NAMESPACE_URL, entry_link)
        entry_published = re.findall("\$\s?published:\s?(\d\d\d\d-\d\d-\d\d \d\d:\d\d)\s?\$|$", contents)[0]
        entry_updated = re.findall("\$\s?updated:\s?(\d\d\d\d-\d\d-\d\d \d\d:\d\d)\s?\$|$", contents)[0]
        entry_tags = re.findall("\$\s?tags:\s?([- ,A-Za-z0-9]+)+\s?\$|$", contents)[0]

        if not entry_published:
            continue

        if not entry_updated:
            entry_updated = entry_published

        entry_published = datetime.strptime(entry_published, '%Y-%m-%d %H:%M')
        entry_updated = datetime.strptime(entry_updated, '%Y-%m-%d %H:%M')

        body += "  <entry>\n"
        body += "    <title>{}</title>\n".format(entry_title)
        body += "    <link href=\"{}\"/>\n".format(entry_link)
        body += "    <id>urn:uuid:{}</id>\n".format(entry_urn)
        body += "    <published>{}</published>\n".format(entry_published.isoformat('T'))
        body += "    <updated>{}</updated>\n".format(entry_updated.isoformat('T'))

        if entry_tags:
          tags = [s.strip() for s in re.split('[,]', entry_tags)]
          for t in tags:
            body += "    <category term=\"{}\"/>\n".format(t)

        body += "  </entry>\n\n"

        if entry_updated > updated:
            updated = entry_updated

# Printing

print("20 text/xml\r")

print("<?xml version=\"1.0\" encoding=\"utf-8\"?>")
print("<feed xmlns=\"http://www.w3.org/2005/Atom\">")
print("")
print("  <title>{}</title>".format(title))
print("  <link href=\"{}\"/>".format(url))
print("  <updated>{}</updated>".format(updated.isoformat('T')))
print("  <author>")
print("    <name>{}</name>".format(author))
print("  </author>")
print("  <id>urn:uuid:{}</id>".format(feed_urn))

print("")
print(body)
print("</feed>")