From b736068ee7b82e05c2ede8bc48ace7ffa4709e29 Mon Sep 17 00:00:00 2001 From: Chris Xiong Date: Wed, 24 Jul 2024 23:40:11 -0400 Subject: Initial commit. --- utils/atomgen.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 utils/atomgen.py (limited to 'utils/atomgen.py') diff --git a/utils/atomgen.py b/utils/atomgen.py new file mode 100644 index 0000000..453465d --- /dev/null +++ b/utils/atomgen.py @@ -0,0 +1,51 @@ +# 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.SERVED_DATA_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) -- cgit v1.2.3