#!/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, write_result # noqa: E402 def month_key(dt): return f"{dt.year}-{dt.month:02d}" 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) monthly_articles = defaultdict(int) monthly_words = defaultdict(int) months_set = set() for article in articles: date = article.get("date") if not date: continue key = month_key(date) monthly_articles[key] += 1 monthly_words[key] += article.get("wordCount") or 0 months_set.add(key) sorted_months = sorted(months_set) cum_articles = [] cum_words = [] labels = [] total_a = 0 total_w = 0 for key in sorted_months: total_a += monthly_articles.get(key, 0) total_w += monthly_words.get(key, 0) labels.append(key) cum_articles.append(total_a) cum_words.append(total_w) try: from render_stats_charts import render_cumulative, setup_rcparams except ImportError as exc: # noqa: BLE001 print(f"Failed to import renderer: {exc}", file=sys.stderr) sys.exit(1) setup_rcparams() render_cumulative( { "labels": labels, "articles": cum_articles, "words": cum_words, "title": "Cumul articles / mots", }, output_path, ) write_result( { "image": public_path, "meta": { "months": len(labels), "articles": total_a, "words": total_w, }, } ) if __name__ == "__main__": main()