29 lines
1.4 KiB
Python
29 lines
1.4 KiB
Python
import os, json, urllib.request, urllib.error, time
|
|
TOKEN=os.environ["GT"]; USER="bt@tedards.net"; BASE="https://graph.microsoft.com/v1.0"
|
|
def g(url):
|
|
for _ in range(6):
|
|
req=urllib.request.Request(url, headers={"Authorization":"Bearer "+TOKEN})
|
|
try:
|
|
with urllib.request.urlopen(req,timeout=60) as r: return json.loads(r.read())
|
|
except urllib.error.HTTPError as e:
|
|
if e.code in (429,503,504,500): time.sleep(int(e.headers.get("Retry-After","8"))); continue
|
|
raise
|
|
raise RuntimeError(url)
|
|
# BFS all folders
|
|
hits=[]; names={}
|
|
def walk(url, path):
|
|
while url:
|
|
d=g(url)
|
|
for f in d["value"]:
|
|
nm=f.get("displayName","?"); fid=f["id"]; tot=f.get("totalItemCount",0); ch=f.get("childFolderCount",0)
|
|
full=path+"/"+nm
|
|
names[fid]=full
|
|
low=nm.lower()
|
|
if "9000" in low or "duplicat" in low or "06-26" in low or "06/26" in low:
|
|
hits.append((full, tot, ch, fid))
|
|
if ch>0:
|
|
walk(BASE+"/users/%s/mailFolders/%s/childFolders?$top=100&$select=id,displayName,totalItemCount,childFolderCount"%(USER,fid), full)
|
|
url=d.get("@odata.nextLink")
|
|
walk(BASE+"/users/%s/mailFolders?$top=100&$select=id,displayName,totalItemCount,childFolderCount"%USER, "")
|
|
print(json.dumps([{"path":h[0],"total":h[1],"children":h[2],"id":h[3]} for h in hits], indent=2))
|