summaryrefslogtreecommitdiff
path: root/trees/blog/build_feed.py
blob: 2fd067d0c5e77eba4f09df49f480e401353ec268 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72

#!/usr/bin/env python3
import os, re, time, sys
from datetime import datetime, timezone

# IN = "./src/feed.html"
# OUT = "./src/feed.rss"
IN = sys.argv[1]
OUT = sys.argv[2]

def fmt_rfc2822(ts: float) -> str:
    return datetime.fromtimestamp(ts, timezone.utc).strftime("%a, %d %b %Y %H:%M:%S %z")

# Read Typst-generated HTML
with open(IN, encoding="utf-8") as f:
    html = f.read()

# Extract <item> blocks
blocks = re.findall(r"<item>(.*?)</item>", html, re.DOTALL)

posts = []
for block in blocks:
    url_match = re.search(r"<h1>(.*?)</h1>", block)
    title_match = re.search(r"<title>(.*?)</title>", block)
    if not (url_match and title_match):
        continue

    url = url_match.group(1).strip()
    title = title_match.group(1).strip()

    # Determine file mod time (fallback = now)
    filename = "./src/" + os.path.basename(url.replace("html", "typ"))
    print(f"adding {filename}")
    ts = os.path.getmtime(filename) if os.path.exists(filename) else time.time()

    posts.append({
        "title": title,
        "url": url,
        "ts": ts,
        "pubDate": fmt_rfc2822(ts)
    })

# Sort newest first
posts.sort(key=lambda p: p["ts"], reverse=True)

rss_items = "\n".join(
    f"""  <item>
    <title>{p["title"]}</title>
    <link>{p["url"]}</link>
    <pubDate>{p["pubDate"]}</pubDate>
  </item>"""
    for p in posts
)

# Build RSS
rss = f"""<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
  <title>0x4200.cafe</title>
  <link>https://0x4200.cafe/</link>
  <description>Latest posts from 0x4200.cafe</description>
  <lastBuildDate>{fmt_rfc2822(time.time())}</lastBuildDate>
{rss_items}
</channel>
</rss>
"""

# Write out
with open(OUT, "w", encoding="utf-8") as f:
    f.write(rss)

print(f"✅ Built {OUT} with {len(posts)} items (sorted newest first)")