60 lines
2.6 KiB
Python
Executable File
60 lines
2.6 KiB
Python
Executable File
import os
|
|
import re
|
|
from datetime import datetime
|
|
|
|
# Chemin du dossier source
|
|
source_dir = "source"
|
|
|
|
# Expression régulière pour extraire la date du contenu de l'article
|
|
date_regex = re.compile(r"\b(\d{4}-\d{2}-\d{2})\b")
|
|
|
|
# Liste des fichiers org-mode trouvés
|
|
org_files = []
|
|
|
|
# Parcourt le dossier source à la recherche de fichiers org-mode
|
|
for root, dirs, files in os.walk(source_dir):
|
|
for file in files:
|
|
if file.endswith(".org"):
|
|
# Ouvre le fichier et recherche la première date dans le contenu de l'article
|
|
with open(os.path.join(root, file), "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
match = date_regex.search(content)
|
|
if match:
|
|
date = datetime.strptime(match.group(1), "%Y-%m-%d")
|
|
# Ajoute le fichier à la liste avec sa date correspondante
|
|
org_files.append((date, os.path.join(root, file)))
|
|
|
|
# Tri des fichiers par ordre décroissant de date
|
|
org_files.sort(reverse=True)
|
|
|
|
# Génération du flux Atom
|
|
atom_feed = {"title": "Flux Atom des articles GMI", "link": "http://www.example.com/atom", "updated": org_files[0][0].strftime("%Y-%m-%dT%H:%M:%SZ"), "entries": []}
|
|
for date, file in org_files:
|
|
# Parse le fichier org-mode pour extraire le titre, la description et la date de publication
|
|
with open(file, "r", encoding="utf-8") as f:
|
|
content = f.read()
|
|
title = re.search(r"\*+ (.+)\n", content).group(1)
|
|
description = re.search(r"\n+ (.+)\n", content, re.DOTALL).group(1)
|
|
published = date.strftime("%Y-%m-%dT%H:%M:%SZ")
|
|
# Ajoute l'article au flux Atom
|
|
atom_entry = {"title": title, "link": file, "summary": description, "published": published}
|
|
atom_feed["entries"].append(atom_entry)
|
|
if date > datetime.strptime(atom_feed["updated"], "%Y-%m-%dT%H:%M:%SZ"):
|
|
atom_feed["updated"] = published
|
|
|
|
# Enregistrement du flux Atom dans un fichier
|
|
with open("articles.atom", "w", encoding="utf-8") as f:
|
|
f.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
|
|
f.write("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n")
|
|
f.write(f" <title>{atom_feed['title']}</title>\n")
|
|
f.write(f" <link href=\"{atom_feed['link']}\"/>\n")
|
|
f.write(f" <updated>{atom_feed['updated']}</updated>\n")
|
|
for entry in atom_feed["entries"]:
|
|
f.write(" <entry>\n")
|
|
f.write(f" <title>{entry['title']}</title>\n")
|
|
f.write(f" <link href=\"{entry['link']}\"/>\n")
|
|
f.write(f" <summary>{entry['summary']}</summary>\n")
|
|
f.write(f" <published>{entry['published']}</published>\n")
|
|
f.write(" </entry>\n")
|
|
f.write("</feed>\n")
|