rss feed generation

This commit is contained in:
veclav talica 2023-05-22 18:29:20 +05:00
parent fde6cfe644
commit 5cc4ff44a6
3 changed files with 70 additions and 0 deletions

View File

@ -9,3 +9,5 @@ for d in ./articles/*; do
./tools/article_wrapper.py "$d/page.mmd" | ./tools/mmd/build/multimarkdown > "./html/articles/$(basename -- $d).html"
fi
done
./tools/feed_generator.py ./articles/ https://mjestecko.neocities.org/ > ./html/feed.xml

64
tools/feed_generator.py Executable file
View File

@ -0,0 +1,64 @@
#!/usr/bin/python3
from sys import argv, exit
from os import walk, path
from random import choice, seed
from textwrap import indent
import time, urllib.parse, re, subprocess
from article_utils import parse_metadata
from page_shares import ADJECTIVES
from rfc822 import stringify_date
if len(argv) <= 1:
print("No directory was supplied")
exit(-1)
if len(argv) <= 2:
print("No address was supplied")
exit(-1)
seed()
address = argv[2]
# todo: Find the latest pubDate
feed = f"""<rss version="2.0">
<channel>
<title>mjestečko</title>
<link>{address}</link>
<description>Personal blog of one {choice(ADJECTIVES)} Veclav Talica</description>
<language>en</language>
<lastBuildDate>{stringify_date(time.gmtime(int(time.time())))}</lastBuildDate>
"""
body_taker = re.compile(r"<body>(.*)</body>", re.DOTALL)
for root, dirs, _ in walk(argv[1]):
for d in dirs:
metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd"))
feed += (
" <item>\n"
f""" <title>{metadata.get("Title", "No title!? ;-;")}</title>\n"""
)
body = subprocess.getoutput(f"./tools/mmd/build/multimarkdown {root}/{d}/page.mmd")
feed += (
" <description>\n"
" <![CDATA[\n"
f"{indent(body_taker.search(body)[1], ' ' * 12)}\n"
" ]]>\n"
" </description>\n"
)
if "Date" in metadata:
feed += \
f""" <pubDate>{stringify_date(metadata["Date"])}</pubDate>\n"""
feed += (
f""" <guid>/articles/{d}</guid>\n"""
f""" <link>{address}/articles/{urllib.parse.quote(d)}</link>\n"""
" </item>\n"
)
feed += """ </channel>
</rss>"""
print(feed)

4
tools/rfc822.py Normal file
View File

@ -0,0 +1,4 @@
from page_shares import MONTHS, WEEKDAYS
def stringify_date(date) -> str:
return f"{WEEKDAYS[date.tm_wday][:3]}, {date.tm_mday} {MONTHS[date.tm_mon][:3]} {date.tm_year} {date.tm_hour:02d}:{date.tm_min:02d}:{date.tm_sec:02d} GMT"