From 08ccc9ae481ebc2b21c49c41d2577ab9b7ef4e99 Mon Sep 17 00:00:00 2001 From: Richard Dern Date: Wed, 3 Dec 2025 16:38:24 +0100 Subject: [PATCH] =?UTF-8?q?Lister=20les=20sets=20filtr=C3=A9s=20par=20ann?= =?UTF-8?q?=C3=A9e=20en=20Markdown?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/list_sets_by_year.py | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 scripts/list_sets_by_year.py diff --git a/scripts/list_sets_by_year.py b/scripts/list_sets_by_year.py new file mode 100644 index 0000000..a98ce47 --- /dev/null +++ b/scripts/list_sets_by_year.py @@ -0,0 +1,59 @@ +"""Génère une liste Markdown des sets filtrés regroupés par année.""" + +import csv +from pathlib import Path + +from lib.filesystem import ensure_parent_dir + + +SETS_PATH = Path("data/intermediate/sets_enriched.csv") +DESTINATION_PATH = Path("data/final/sets_by_year.md") + + +def load_sets(path: Path) -> list[dict]: + """Charge les sets filtrés.""" + rows: list[dict] = [] + with path.open() as csv_file: + reader = csv.DictReader(csv_file) + for row in reader: + rows.append(row) + return rows + + +def group_sets_by_year(rows: list[dict]) -> dict[str, list[dict]]: + """Regroupe les sets par année.""" + grouped: dict[str, list[dict]] = {} + for row in rows: + year_rows = grouped.get(row["year"]) + if year_rows is None: + year_rows = [] + grouped[row["year"]] = year_rows + year_rows.append(row) + for year, items in grouped.items(): + items.sort(key=lambda r: r["set_num"]) + return grouped + + +def write_markdown(path: Path, grouped: dict[str, list[dict]]) -> None: + """Écrit la liste Markdown des sets regroupés par année.""" + ensure_parent_dir(path) + years = sorted(grouped.keys(), key=int) + lines: list[str] = ["## Liste des sets par année", ""] + for year in years: + lines.append(f"### {year}") + lines.append("") + for row in grouped[year]: + lines.append(f"- [{row['set_num']}]({row['rebrickable_url']}) - {row['name']}") + lines.append("") + path.write_text("\n".join(lines)) + + +def main() -> None: + """Construit le Markdown des sets filtrés classés par année.""" + sets_rows = load_sets(SETS_PATH) + grouped = group_sets_by_year(sets_rows) + write_markdown(DESTINATION_PATH, grouped) + + +if __name__ == "__main__": + main()