"""Les contacts de LibreDesk (lecture seule) : leurs e-mails, pour les comparer aux utilisateurs du dashboard (Auth0). Lancé par Maxime (Claude ne lit pas le .env) : set -a && . ./.env && set +a && python3 acces/contacts.py Clé admin (LIBREDESK_ADMIN_KEY / _SECRET) : lister tous les contacts demande `contacts:read_all`, que la clé agent n'a pas. Seulement GET /api/v1/contacts. La liste complète va dans cases/ (hors de git) ; à l'écran, des totaux seulement. """ import collections import json import os import urllib.request from datetime import date from pathlib import Path URL = os.environ.get("LIBREDESK_URL", "https://helpdesk.sensima.ch").rstrip("/") SORTIE = Path(__file__).resolve().parents[1] / "cases" / f"contacts-libredesk-{date.today()}.json" def page(n: int) -> dict: cle = f"token {os.environ['LIBREDESK_ADMIN_KEY']}:{os.environ['LIBREDESK_ADMIN_SECRET']}" requete = urllib.request.Request(f"{URL}/api/v1/contacts?page={n}&page_size=100", headers={"Authorization": cle}) with urllib.request.urlopen(requete, timeout=30) as r: return json.load(r)["data"] if __name__ == "__main__": contacts, n = [], 1 while True: d = page(n) contacts += d["results"] if n >= d["total_pages"]: break n += 1 SORTIE.parent.mkdir(exist_ok=True) SORTIE.write_text(json.dumps(contacts, ensure_ascii=False, indent=1), encoding="utf-8") emails = [(c.get("email") or "").strip() for c in contacts] domaines = collections.Counter(e.rsplit("@", 1)[-1].lower() for e in emails if "@" in e) print(f"{URL} : {len(contacts)} contacts, {sum(map(bool, emails))} avec e-mail, " f"{sum(e != e.lower() for e in emails)} avec des majuscules, " f"{sum(bool(c.get('external_user_id')) for c in contacts)} avec external_user_id, " f"{len(domaines)} domaines") print(f"liste complète : {SORTIE}")