Files
janssen/KPC/_read_v0.py
T
2026-06-18 09:31:33 +02:00

52 lines
2.3 KiB
Python

"""_read_v0.py — DOČASNÉ: přečte ForKPCGeneration e-maily z mailboxu (argv[1])."""
import sys
sys.stdout.reconfigure(encoding="utf-8")
import msal, requests
TENANT_ID = "7d269944-37a4-43a1-8140-c7517dc426e9"
CLIENT_ID = "4b222bfd-78c9-4239-a53f-43006b3ed07f"
CLIENT_SECRET = "Txg8Q~MjhocuopxsJyJBhPmDfMxZ2r5WpTFj1dfk"
AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
SCOPE = ["https://graph.microsoft.com/.default"]
CATEGORY = "ForKPCGeneration"
mailbox = sys.argv[1] if len(sys.argv) > 1 else "vladimir.buzalka@buzalka.cz"
CATEGORY = sys.argv[2] if len(sys.argv) > 2 else CATEGORY
BASE = f"https://graph.microsoft.com/v1.0/users/{mailbox}"
app = msal.ConfidentialClientApplication(CLIENT_ID, authority=AUTHORITY, client_credential=CLIENT_SECRET)
tok = app.acquire_token_for_client(scopes=SCOPE)
assert "access_token" in tok, tok
H = {"Authorization": f"Bearer {tok['access_token']}"}
params = {
"$filter": f"categories/any(c:c eq '{CATEGORY}')",
"$select": "id,subject,from,receivedDateTime,categories,hasAttachments,body,bodyPreview",
"$top": 25,
}
r = requests.get(f"{BASE}/messages", headers=H, params=params, timeout=30)
if not r.ok:
print(f"CHYBA [{r.status_code}] pro {mailbox}: {r.text[:400]}")
sys.exit(1)
msgs = r.json().get("value", [])
print(f"[{mailbox}] e-mailů s kategorií '{CATEGORY}': {len(msgs)}\n")
for i, m in enumerate(msgs, 1):
frm = m.get("from", {}).get("emailAddress", {})
print("=" * 78)
print(f"[{i}] {m.get('subject')}")
print(f" od: {frm.get('name')} <{frm.get('address')}>")
print(f" datum: {m.get('receivedDateTime')}")
print(f" přílohy: {m.get('hasAttachments')}")
print(f" id: {m.get('id')}")
body = m.get("body", {})
print(f" --- TĚLO ({body.get('contentType')}) ---")
print(body.get("content", ""))
if m.get("hasAttachments"):
ra = requests.get(f"{BASE}/messages/{m['id']}/attachments",
headers=H, params={"$select": "id,name,size,contentType,isInline"}, timeout=30)
if ra.ok:
print(" --- PŘÍLOHY ---")
for a in ra.json().get("value", []):
print(f"{a.get('name')} ({a.get('size')} B, {a.get('contentType')}, inline={a.get('isInline')})")