bin/rss.py (view raw)
1#!/usr/bin/env python3
2# generate an rss item
3
4import html
5from markdown2 import markdown
6import sys
7import os
8from collections import namedtuple
9import re
10import arrow
11import operator
12import warnings
13
14warnings.simplefilter("ignore", arrow.factory.ArrowParseWarning)
15items_raw = []
16from lxml import etree as ET
17
18
19def convert_date(d):
20 return arrow.get(d, "YYYY-MM-DD").format("ddd, DD MMM YYYY HH:mm:ss Z")
21
22
23PREFIX_URL = "https://icyphox.sh/blog/"
24link_extractor = re.compile("\/([^\/]*)\.md$")
25
26def generate_node(rendered, path):
27
28 item = ET.Element("item")
29 title = ET.SubElement(item, "title")
30 title.text = rendered.metadata["title"]
31 description = ET.SubElement(item, "description")
32 description.text = ET.CDATA(str(rendered))
33 link = ET.SubElement(item, "link")
34 link.text = PREFIX_URL + link_extractor.search(path).group(1)
35 pubData = ET.SubElement(item, "pubDate")
36 pubData.text = convert_date(rendered.metadata["date"])
37 guid = ET.SubElement(item, "guid")
38 guid.text = PREFIX_URL + link_extractor.search(path).group(1)
39
40 return item
41
42
43def parse_article(path):
44 with open(path) as f:
45 rendered = markdown(
46 f.read(),
47 extras=[
48 "metadata",
49 "fenced-code-blocks",
50 "header-ids",
51 "footnotes",
52 "smarty-pants",
53 ],
54 )
55 return (arrow.get(rendered.metadata["date"]), rendered, path)
56
57
58tree = ET.parse(os.path.join("templates", "feed.xml"))
59articles = []
60
61for f in os.listdir("pages/blog/"):
62 if f not in ["_index.md", "feed.xml"]:
63 articles.append(parse_article(os.path.join("pages/blog", f)))
64
65articles.sort(key=operator.itemgetter(0), reverse=True)
66chan = tree.find("channel")
67
68for article in articles:
69 chan.append(generate_node(article[1], article[2]))
70
71out = ET.tostring(tree, encoding="unicode")
72with open("pages/blog/feed.xml", "w") as f:
73 f.write(out)