-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_inbox.py
More file actions
72 lines (58 loc) · 2.29 KB
/
Copy pathcheck_inbox.py
File metadata and controls
72 lines (58 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
Check Gmail for Google Takeout completion email.
Usage: python check_inbox.py
"""
from pathlib import Path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build
import base64
CONFIG_DIR = Path(__file__).parent / "config"
CREDENTIALS_FILE = CONFIG_DIR / "credentials.json"
TOKEN_FILE = CONFIG_DIR / "token.json"
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
def get_credentials():
creds = None
if TOKEN_FILE.exists():
creds = Credentials.from_authorized_user_file(str(TOKEN_FILE), SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(str(CREDENTIALS_FILE), SCOPES)
creds = flow.run_local_server(port=0)
with open(TOKEN_FILE, "w") as f:
f.write(creds.to_json())
return creds
def get_body(payload):
"""Extract plain-text body from message payload."""
if payload.get("mimeType") == "text/plain":
data = payload.get("body", {}).get("data")
if data:
return base64.urlsafe_b64decode(data).decode("utf-8")
for part in payload.get("parts", []):
text = get_body(part)
if text:
return text
return ""
def main():
service = build("gmail", "v1", credentials=get_credentials())
query = 'from:noreply@google.com subject:"ready to download" newer_than:7d'
results = service.users().messages().list(userId="me", q=query, maxResults=5).execute()
messages = results.get("messages", [])
if not messages:
print("No Takeout emails found yet.")
return
for ref in messages:
msg = service.users().messages().get(userId="me", id=ref["id"], format="full").execute()
headers = {h["name"]: h["value"] for h in msg["payload"]["headers"]}
subject = headers.get("Subject", "(no subject)")
date = headers.get("Date", "")
body = get_body(msg["payload"])
print(f"Date: {date}")
print(f"Subject: {subject}")
print(f"Body preview: {body[:400].strip()}")
print()
if __name__ == "__main__":
main()