89 lines
2.5 KiB
Python
89 lines
2.5 KiB
Python
#!/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)
|
|
|
|
buckets = defaultdict(lambda: {"words": 0, "count": 0})
|
|
years = set()
|
|
total_words = 0
|
|
total_articles = 0
|
|
|
|
for article in articles:
|
|
date = article.get("date")
|
|
if not date:
|
|
continue
|
|
year = date.year
|
|
month = date.month
|
|
key = (year, month)
|
|
years.add(year)
|
|
buckets[key]["words"] += article.get("wordCount") or 0
|
|
buckets[key]["count"] += 1
|
|
total_words += article.get("wordCount") or 0
|
|
total_articles += 1
|
|
|
|
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 = []
|
|
for month in month_numbers:
|
|
entry = buckets.get((year, month))
|
|
if not entry or entry["count"] == 0:
|
|
values.append(0)
|
|
else:
|
|
values.append(round(entry["words"] / entry["count"]))
|
|
series.append({"label": str(year), "values": values})
|
|
|
|
average = round(total_words / total_articles) if total_articles > 0 else 0
|
|
|
|
try:
|
|
from lib.render_stats_charts import render_words_per_article, setup_rcparams
|
|
except ImportError as exc: # noqa: BLE001
|
|
print(f"Failed to import renderer: {exc}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
setup_rcparams()
|
|
render_words_per_article({"labels": labels, "series": series, "title": "Moyenne de mots par article (par mois)"}, output_path)
|
|
|
|
write_result(
|
|
{
|
|
"image": public_path,
|
|
"meta": {
|
|
"average": average,
|
|
"articles": total_articles,
|
|
},
|
|
}
|
|
)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|
|
|