#!/usr/bin/env python # vim: set ts=4 sw=4 sts=4 et : import markdown import collections from feedgen.feed import FeedGenerator from dateutil import tz import os import datetime import stat fg = FeedGenerator() url = "https://ouferr.at" fg.id(url) fg.description("Fansub per Wonder Egg Priority") fg.title("Ou ferrat subs") fg.author(name="Ou Ferrat") fg.link(href=url+'/', rel='alternate') fg.link(href=url+'/atom.xml', rel='self') fg.language("ca") fg.updated(datetime.datetime.now(tz.tzlocal())) doc = "" with open("header.html") as f: header = f.read() with open("postfooter.html") as f: postfooter = f.read() doc += header for i in reversed(range(100)): fn = f"{str(i).zfill(2)}.md" #print(fn) try: with open(fn) as f: entry = f.read() except FileNotFoundError: continue head, body = entry.split("\n---\n\n", 1) head = head.split("\n") head = {k.strip(): v.strip() for k, v in [l.split(":", 1) for l in head]} head = collections.defaultdict(lambda: "", head) body = markdown.markdown(body) html_fn = f"{str(i).zfill(2)}.html" html = ("
\n" + "
\n" + f"

{head['title']}

\n" + f"\n" + "
\n" + body + "\n
\n") doc += html with open(html_fn, "w") as f: f.write(header+html+postfooter) fe = fg.add_entry(order='append') fe.id(url+'/'+html_fn) fe.title(head['title'].replace('
', '—')) fe.content(html, type="html") try: # crashing because of an invalid date would be sad dt = datetime.datetime.fromisoformat(head['date']+":00+01:00") fe.published(dt) stats = os.stat(fn) updated = datetime.datetime.fromtimestamp(stats[stat.ST_MTIME], tz.tzlocal()) fe.updated(updated) except ValueError as e: print("error with date", e, head['date']) pass fg.rss_file('rss.xml') fg.atom_file("atom.xml") with open("index.html", "w") as f: f.write(doc)