From 2924172aaf1b43c8423e54b763cc5f2c113bda6d Mon Sep 17 00:00:00 2001 From: thomas Date: Tue, 16 Sep 2025 16:46:26 +0200 Subject: [PATCH] Dateien nach "/" hochladen --- create_rssfeed.py | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 create_rssfeed.py diff --git a/create_rssfeed.py b/create_rssfeed.py new file mode 100644 index 0000000..9d92710 --- /dev/null +++ b/create_rssfeed.py @@ -0,0 +1,75 @@ +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 ) +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}

Hier weiterlesen

' + 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}")