# 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'' xmlbuf += f'' xmlbuf += f'Notekins' xmlbuf += f'{datetime.now(timezone.utc).replace(microsecond=0).isoformat()}' xmlbuf += f'' xmlbuf += f'' xmlbuf += f'{conf.ATOM_ROOT}/atom.xml' xmlbuf += f'{conf.ATOM_TITLE}' xmlbuf += f'{conf.ATOM_SUBTITLE}' xmlbuf += f'{conf.ATOM_ICON}' xmlbuf += f'{conf.ATOM_ICON}' 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'' xmlbuf += f'{title}' xmlbuf += f'' xmlbuf += f'{date}' xmlbuf += f'{date}' xmlbuf += f'{link}' xmlbuf += f'{conf.ATOM_AUTHOR}' xmlbuf += f'' xmlbuf += f'' xmlbuf += f'' atomfn = os.path.join(conf.LOCAL_DATA_ROOT, "atom.xml") with open(atomfn, "w") as f: f.write(xmlbuf)