1
Files
2025/tools/stats/weekday_activity.py
2025-11-28 01:47:10 +01:00

66 lines
1.6 KiB
Python

#!/usr/bin/env python3
import sys
import json
import os
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
WEEKDAY_LABELS = ["Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"]
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 = [0] * 7
words = [0] * 7
for article in articles:
dt = article.get("date")
if not dt:
continue
weekday = dt.weekday() # Monday=0
counts[weekday] += 1
words[weekday] += article.get("wordCount") or 0
try:
from render_stats_charts import render_weekday_activity, setup_rcparams
except ImportError as exc: # noqa: BLE001
print(f"Failed to import renderer: {exc}", file=sys.stderr)
sys.exit(1)
setup_rcparams()
render_weekday_activity(
{
"labels": WEEKDAY_LABELS,
"articles": counts,
"words": words,
"title": "Articles et mots par jour de semaine",
},
output_path,
)
write_result({"image": public_path})
if __name__ == "__main__":
main()