76 lines
2.7 KiB
Python
76 lines
2.7 KiB
Python
import os
|
||
import json
|
||
from pathlib import Path
|
||
from xml.etree.ElementTree import Element, SubElement, ElementTree
|
||
from datetime import datetime
|
||
import markdown
|
||
|
||
# Konfiguration
|
||
NAV_JSON = '/home/thomas/Documents/Webseite_Backup/blog/assets/nav.json'
|
||
BLOG_DIR = Path('/home/thomas/Documents/Webseite_Backup/blog')
|
||
BASE_URL = 'https://blog.sheepfreak.freeddns.org/'
|
||
|
||
# nav.json einlesen
|
||
with open(NAV_JSON, 'r', encoding='utf-8') as f:
|
||
nav_entries = json.load(f)
|
||
|
||
# RSS-Grundstruktur
|
||
rss = Element('rss', version='2.0')
|
||
channel = SubElement(rss, 'channel')
|
||
SubElement(channel, 'title').text = 'Sheepfreak Deep Dives'
|
||
SubElement(channel, 'link').text = BASE_URL
|
||
SubElement(channel, 'description').text = 'Neuigkeiten von meinem Blog'
|
||
|
||
# Markdown zu HTML (für <description>)
|
||
md = markdown.Markdown()
|
||
|
||
for entry in nav_entries:
|
||
file_path = BLOG_DIR / entry["file"]
|
||
if not file_path.exists():
|
||
print(f"❌ Datei nicht gefunden: {file_path}")
|
||
continue
|
||
|
||
with open(file_path, 'r', encoding='utf-8') as f:
|
||
lines = f.readlines()
|
||
|
||
# Titelbeschreibung aus erstem ## + folgendem Absatz
|
||
description = ""
|
||
for i, line in enumerate(lines):
|
||
if line.startswith("## "):
|
||
desc_lines = []
|
||
for desc_line in lines[i+1:]:
|
||
if desc_line.strip() == "":
|
||
continue
|
||
desc_lines.append(desc_line.strip())
|
||
if desc_line.strip().endswith((".", "!", "?")):
|
||
break
|
||
description = " ".join(desc_lines)
|
||
break
|
||
|
||
# Veröffentlichungsdatum aus nav.json (Feld "date", z. B. "2025-07-16")
|
||
try:
|
||
pub_date_obj = datetime.strptime(entry["date"], "%Y-%m-%d")
|
||
except (KeyError, ValueError):
|
||
print(f"⚠️ Kein gültiges Datum in nav.json für {entry['title']}")
|
||
continue
|
||
|
||
pub_date_str = pub_date_obj.strftime('%a, %d %b %Y 00:00:00 +0000')
|
||
|
||
# Eintrag zum Feed hinzufügen
|
||
item = SubElement(channel, 'item')
|
||
SubElement(item, 'title').text = entry["title"]
|
||
SubElement(item, 'link').text = BASE_URL + entry['hash'].lstrip('#')
|
||
SubElement(item, 'guid').text = BASE_URL + entry['hash'].lstrip('#')
|
||
SubElement(item, 'pubDate').text = pub_date_str
|
||
|
||
# HTML-Beschreibung + Link
|
||
description_html = md.convert(description)
|
||
link_url = BASE_URL + entry["hash"].lstrip('#')
|
||
description_with_link = f'{description_html}<p><a href="{link_url}">Hier weiterlesen</a></p>'
|
||
SubElement(item, 'description').text = description_with_link
|
||
|
||
# RSS-Feed speichern
|
||
rss_path = BLOG_DIR / 'assets' / 'rss.xml'
|
||
ElementTree(rss).write(rss_path, encoding='utf-8', xml_declaration=True)
|
||
print(f"✅ RSS-Feed gespeichert unter {rss_path}")
|