Files
janssen/KPC/TRASH/_explore_email_v0.py
T
2026-06-17 15:05:10 +02:00

55 lines
2.3 KiB
Python

"""
_explore_email_v0.py — DOČASNÝ read-only průzkum
Najde e-maily s kategorií 'ForKPCGeneration' ve schránce vladimir.buzalka@buzalka.cz
přes Graph API a vypíše subject + tělo + seznam příloh. Nic nemění.
"""
import sys
sys.stdout.reconfigure(encoding="utf-8")
import msal
import requests
TENANT_ID = "7d269944-37a4-43a1-8140-c7517dc426e9"
CLIENT_ID = "4b222bfd-78c9-4239-a53f-43006b3ed07f"
CLIENT_SECRET = "Txg8Q~MjhocuopxsJyJBhPmDfMxZ2r5WpTFj1dfk"
MAILBOX = "vladimir.buzalka@buzalka.cz"
AUTHORITY = f"https://login.microsoftonline.com/{TENANT_ID}"
SCOPE = ["https://graph.microsoft.com/.default"]
BASE = f"https://graph.microsoft.com/v1.0/users/{MAILBOX}"
CATEGORY = "ForKPCGeneration"
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)
r.raise_for_status()
msgs = r.json().get("value", [])
print(f"Nalezeno 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" kateg.: {m.get('categories')}")
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')})")