"""Teams: Teams, Kanäle, Kanalnachrichten mit Antworten und Chats je Benutzer. Nur GET. Braucht graph/users.json. Ausgabe nach teams/. Was nicht abrufbar ist, steht in teams/not_possible.json.""" import json, os, sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) import m365_auth as auth from m365_auth import get_all, safe_name, write_json def main(argv=None): p = auth.make_parser("Back up Teams, channels, messages and chats (read-only)") p.add_argument("--max-teams", type=int, default=0, help="limit the number of teams (0 = all)") args = p.parse_args(argv) out = args.out root = os.path.join(out, "teams") not_possible = {} def note(what, url, f): status = int(f.split()[0]) if f and f.split()[0].isdigit() else None not_possible[what] = {"url": url, "status": status, "message": f} auth.log(out, f"not possible {what}: {f[:150]}", "teams") write_json(os.path.join(root, "not_possible.json"), not_possible) def do_teams(): ts, f = get_all("/groups?$filter=resourceProvisioningOptions/Any(x:x eq 'Team')&$select=id,displayName,description,visibility,createdDateTime") if f: note("teams_list", "/groups(Team)", f); ts, f = get_all("/teams") if args.max_teams: ts = ts[:args.max_teams] write_json(os.path.join(root, "teams.json"), ts) n = {"teams": len(ts), "channels": 0, "messages": 0, "replies": 0} for t in ts: td = os.path.join(root, "teams", safe_name(t["displayName"]) + "_" + t["id"][:8]) if os.path.exists(os.path.join(td, "done")): continue r = auth.g("GET", f"/teams/{t['id']}") if r.status_code == 200: write_json(os.path.join(td, "team.json"), r.json()) else: note(f"team {t['displayName']}", f"/teams/{t['id']}", f"{r.status_code} {r.text[:200]}"); continue d, f = get_all(f"/teams/{t['id']}/members") if f: note(f"{t['displayName']} members", "members", f) else: write_json(os.path.join(td, "members.json"), d) chs, f = get_all(f"/teams/{t['id']}/channels") if f: note(f"{t['displayName']} channels", "channels", f); continue write_json(os.path.join(td, "channels.json"), chs); n["channels"] += len(chs) for c in chs: cd = os.path.join(td, "channel_" + safe_name(c["displayName"]) + "_" + c["id"][-8:]) for part in ("members", "tabs"): d, f = get_all(f"/teams/{t['id']}/channels/{c['id']}/{part}") if f: not_possible[f"{t['displayName']}/{c['displayName']} {part}"] = {"status": int(f.split()[0]), "message": f[:200]} else: write_json(os.path.join(cd, part + ".json"), d) ms, f = get_all(f"/teams/{t['id']}/channels/{c['id']}/messages?$top=50") if f: note(f"{t['displayName']}/{c['displayName']} messages", "messages", f) for m in ms: rp, f = get_all(f"/teams/{t['id']}/channels/{c['id']}/messages/{m['id']}/replies?$top=50") if f: note(f"replies {m['id']}", "replies", f) m["_replies"] = rp; n["replies"] += len(rp) n["messages"] += len(ms); write_json(os.path.join(cd, "messages.json"), ms) files, f = get_all(f"/teams/{t['id']}/channels/{c['id']}/filesFolder") if not f: write_json(os.path.join(cd, "filesFolder.json"), files) open(os.path.join(td, "done"), "w").write("1") return n def do_chats(): seen = {}; n = {"chats": 0, "messages": 0, "users_without_chats": []} for u in auth.read_users(out): d, f = get_all(f"/users/{u['id']}/chats?$top=50") if f: n["users_without_chats"].append({"upn": u["userPrincipalName"], "message": f[:150]}) if not f.startswith("404"): note(f"chats {u['userPrincipalName']}", "users/chats", f) continue for c in d: seen.setdefault(c["id"], c) write_json(os.path.join(root, "chats/chats.json"), list(seen.values())) for cid, c in seen.items(): cd = os.path.join(root, "chats", cid[:40].replace(":", "_").replace("@", "_")) mf = os.path.join(cd, "messages.json") if os.path.exists(mf): n["chats"] += 1; n["messages"] += len(json.load(open(mf))); continue write_json(os.path.join(cd, "chat.json"), c) mem, f = get_all(f"/chats/{cid}/members") if not f: write_json(os.path.join(cd, "members.json"), mem) ms, f = get_all(f"/chats/{cid}/messages?$top=50") if f: note(f"chat {cid[:20]} messages", "chats/messages", f); continue write_json(mf, ms); n["chats"] += 1; n["messages"] += len(ms) return n os.makedirs(root, exist_ok=True) a = do_teams(); b = do_chats() write_json(os.path.join(root, "_summary.json"), {"teams": a, "chats": b}) if not os.path.exists(os.path.join(root, "not_possible.json")): write_json(os.path.join(root, "not_possible.json"), {}) auth.log(out, f"done {a} {b['chats']} chats {b['messages']} msgs", "teams") return 0 if __name__ == "__main__": sys.exit(main())