mjestecko/tools/main_page_generator.py

50 lines
1.3 KiB
Python
Raw Normal View History

2023-05-21 14:24:54 +00:00
#!/usr/bin/python3
from sys import argv, exit
from os import walk, path
2023-05-22 11:18:54 +00:00
from random import choice, seed
2023-05-21 14:24:54 +00:00
import time, urllib.parse
from article_utils import parse_metadata
from page_shares import wrap_page, ADJECTIVES, MONTHS
2023-05-21 14:24:54 +00:00
if len(argv) <= 1:
print("No directory was supplied")
exit(-1)
2023-05-22 11:18:54 +00:00
seed()
page_metadata = """Title: mjestečko
2023-05-21 14:24:54 +00:00
CSS: /style.css
"""
2023-05-22 11:21:00 +00:00
page = f"""Personal blog of one {choice(ADJECTIVES)} Veclav Talica.
2023-05-21 14:24:54 +00:00
### Articles ###
"""
2024-02-10 15:58:08 +00:00
artciles = {}
2023-05-21 14:24:54 +00:00
for root, dirs, _ in walk(argv[1]):
for d in dirs:
metadata = parse_metadata(path.abspath(root + '/' + d + "/page.mmd"))
article = urllib.parse.quote(d)
2024-02-10 15:58:08 +00:00
artciles[article] = {
"metadata": metadata
}
2023-05-25 14:06:05 +00:00
break
2023-05-21 14:24:54 +00:00
2024-02-10 15:58:08 +00:00
for title in sorted(artciles.keys(), key=lambda a: artciles[a]["metadata"].get("Date", time.gmtime(0)), reverse=True):
article = artciles[title]
metadata = article["metadata"]
page += (
f"""[{metadata.get("Title", "No title given! What a clusterfuck!")}](/articles/{article}.html)\n\n"""
f""">{metadata.get("Brief", "")}\n\n"""
)
2023-05-21 14:24:54 +00:00
curtime = time.gmtime(int(time.time()))
2023-05-22 19:03:16 +00:00
page += f"Last compiled: *{MONTHS[curtime.tm_mon]} {curtime.tm_mday}, {curtime.tm_year} {curtime.tm_hour}:{curtime.tm_min:02d} UTC*\n\n"
2023-05-21 14:24:54 +00:00
print(page_metadata + wrap_page(page))