93 lines
3.7 KiB
Python
Executable File
93 lines
3.7 KiB
Python
Executable File
import os
|
|
import re
|
|
from datetime import datetime
|
|
|
|
# Chemin du dossier source
|
|
import argparse
|
|
|
|
from utils import find_first_level1_title, find_year_and_slug, find_extract_in_content_org
|
|
from website_config import configs_sites
|
|
|
|
# Configuration des arguments de la ligne de commande
|
|
parser = argparse.ArgumentParser(description="Générer un nouvel article en mode orgmode.")
|
|
parser.add_argument("blog_dir", help="Le nom du dossier de blog.")
|
|
|
|
args = parser.parse_args()
|
|
|
|
website_ndd = configs_sites[args.blog_dir]['NDD']
|
|
blog_dir = 'sources/'+args.blog_dir+'/lang_fr/'
|
|
|
|
# Expression régulière pour extraire la date du contenu de l'article
|
|
date_regex = re.compile(r"\b(\d{14})\b")
|
|
date_regex_org = re.compile(r"\b(\d{4}-\d{2}-\d{2})\b")
|
|
|
|
# Liste des fichiers org-mode trouvés
|
|
org_files = []
|
|
|
|
limit_articles_feed=1000
|
|
count_articles=0
|
|
print('atom generate: fichiers dans le dossier: ',len((blog_dir)))
|
|
# Parcourt le dossier source à la recherche de fichiers org-mode
|
|
for root, dirs, files in os.walk(blog_dir):
|
|
for file in files:
|
|
if file.endswith(".org"):
|
|
print(os.path.join(root, file))
|
|
date_str, annee, slug = find_year_and_slug(file)
|
|
# 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()
|
|
extract = find_extract_in_content_org(content)
|
|
count_articles+=1
|
|
match = date_regex_org.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), annee, slug,extract))
|
|
|
|
if count_articles > limit_articles_feed:
|
|
break
|
|
if count_articles > limit_articles_feed:
|
|
break
|
|
# 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 de "+args.blog_dir,
|
|
"link": f"{website_ndd}/feed",
|
|
# "updated": org_files[0][0].strftime("%Y-%m-%dT%H:%M:%SZ"),
|
|
"updated": org_files[0][0],
|
|
"entries": []}
|
|
|
|
for date, file, annee, slug, extract 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 = find_first_level1_title(content)
|
|
description = title
|
|
# published = date_str
|
|
# Ajoute l'article au flux Atom
|
|
atom_entry = {"title": title,
|
|
"summary": extract,
|
|
"link": f"{website_ndd}/{annee}/{slug}",
|
|
"published": date
|
|
}
|
|
atom_feed["entries"].append(atom_entry)
|
|
# if published > atom_feed["updated"]:
|
|
# atom_feed["updated"] = published
|
|
|
|
# Enregistrement du flux Atom dans un fichier
|
|
with open(f"index_{args.blog_dir}.xml", "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>')
|