diff options
author | Chris Xiong <chirs241097@gmail.com> | 2024-07-24 23:40:11 -0400 |
---|---|---|
committer | Chris Xiong <chirs241097@gmail.com> | 2024-07-24 23:40:11 -0400 |
commit | b736068ee7b82e05c2ede8bc48ace7ffa4709e29 (patch) | |
tree | eaa3cce9fdd9973043a7a55613584f90f6598a20 /utils/atomgen.py | |
download | notekins-b736068ee7b82e05c2ede8bc48ace7ffa4709e29.tar.xz |
Initial commit.
Diffstat (limited to 'utils/atomgen.py')
-rw-r--r-- | utils/atomgen.py | 51 |
1 files changed, 51 insertions, 0 deletions
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'<?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.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'<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) |