blob: 7b37cfbd7f85f9328778b894d1200352cc07b0af (
plain) (
tree)
|
|
# Chris Xiong 2024
# License: Expat (MIT)
import os
import re
from datetime import datetime, timezone
from monolith import Monolith
from config import conf
tagrex = re.compile(r'<[^>]+>')
def remove_tags(s):
return tagrex.sub('', s)
def ellide(s, l):
return s if len(s) <= l else s[:l] + " ..."
def gen_atom():
xmlbuf = f'<?xml version="1.0" encoding="utf-8"?>'
xmlbuf += f'<feed xmlns="http://www.w3.org/2005/Atom">'
xmlbuf += f'<generator uri="https://cgit.chrisoft.org/notekins.git" version="{conf.VERSION_STRING}">Notekins</generator>'
xmlbuf += f'<updated>{datetime.now(timezone.utc).replace(microsecond=0).isoformat()}</updated>'
xmlbuf += f'<link href="{conf.ATOM_ROOT}/atom.xml" rel="self" type="application/atom+xml" title="Atom"/>'
xmlbuf += f'<link href="{conf.ATOM_ROOT}/" rel="alternate" type="text/html"/>'
xmlbuf += f'<id>{conf.ATOM_ROOT}/atom.xml</id>'
xmlbuf += f'<title>{conf.ATOM_TITLE}</title>'
xmlbuf += f'<subtitle>{conf.ATOM_SUBTITLE}</subtitle>'
xmlbuf += f'<icon>{conf.ATOM_ICON}</icon>'
xmlbuf += f'<logo>{conf.ATOM_ICON}</logo>'
m = Monolith(os.path.join(conf.LOCAL_DATA_ROOT, "posts.monolith"))
m.load_index()
dates = list(reversed(m.get_all_dates()[-conf.ATOM_NPOSTS:]))
for d in dates:
p = m.get_post(d)
link = f"{conf.ATOM_ROOT}/?post={p.date}"
title = ellide(remove_tags(p.content), 32)
date = datetime.fromtimestamp(p.date, timezone.utc).replace(microsecond=0).isoformat()
xmlbuf += f'<entry>'
xmlbuf += f'<title>{title}</title>'
xmlbuf += f'<link href="{link}" title="{title}"/>'
xmlbuf += f'<published>{date}</published>'
xmlbuf += f'<updated>{date}</updated>'
xmlbuf += f'<id>{link}</id>'
xmlbuf += f'<author><name>{conf.ATOM_AUTHOR}</name></author>'
xmlbuf += f'<content type="html" xml:base="{link}"><![CDATA[{p.content}]]></content>'
xmlbuf += f'</entry>'
xmlbuf += f'</feed>'
atomfn = os.path.join(conf.LOCAL_DATA_ROOT, "atom.xml")
with open(atomfn, "w") as f:
f.write(xmlbuf)
|