#!/usr/bin/env python3 import sys import json import os from collections import defaultdict CURRENT_DIR = os.path.dirname(os.path.abspath(__file__)) PARENT_DIR = os.path.abspath(os.path.join(CURRENT_DIR, os.pardir)) if CURRENT_DIR not in sys.path: sys.path.append(CURRENT_DIR) if PARENT_DIR not in sys.path: sys.path.append(PARENT_DIR) from common import load_articles, MONTH_LABELS, write_result # noqa: E402 def main(): try: payload = json.load(sys.stdin) except Exception as exc: # noqa: BLE001 print(f"Failed to read JSON: {exc}", file=sys.stderr) sys.exit(1) content_dir = payload.get("contentDir") or "content" output_path = payload.get("outputPath") public_path = payload.get("publicPath") articles = load_articles(content_dir) counts = defaultdict(int) years = set() first = None last = None for article in articles: date = article.get("date") if not date: continue year = date.year month = date.month years.add(year) counts[(year, month)] += 1 if not first or date < first: first = date if not last or date > last: last = date month_numbers = list(range(1, 13)) labels = [MONTH_LABELS[m - 1] for m in month_numbers] sorted_years = sorted(years) series = [] for year in sorted_years: values = [counts.get((year, m), 0) for m in month_numbers] series.append({"label": str(year), "values": values}) # Render via shared renderer try: from lib.render_stats_charts import render_articles_per_month, setup_rcparams except ImportError as exc: # noqa: BLE001 print(f"Failed to import renderer: {exc}", file=sys.stderr) sys.exit(1) setup_rcparams() render_articles_per_month({"labels": labels, "series": series, "title": "Articles par mois"}, output_path) write_result( { "image": public_path, "meta": { "from": first.isoformat() if first else None, "to": last.isoformat() if last else None, "months": len(labels), }, } ) if __name__ == "__main__": main()