Skip to content

Commit 8aa555d

Browse files
committed
Add command line argument for maxResults; format with black
1 parent c21e7a5 commit 8aa555d

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

gmail/gmail_dump.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4-
from pathlib import Path
54
from base64 import urlsafe_b64decode as b64d
5+
from pathlib import Path
6+
import sys
7+
8+
from google.oauth2.credentials import Credentials
69
from google_auth_oauthlib.flow import InstalledAppFlow
710
from googleapiclient.discovery import build
8-
from google.oauth2.credentials import Credentials
911

1012
SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]
1113
CREDS = "client_secret.json"
1214
TOKEN = Path("token.json")
13-
OUT = Path("mail_dump"); OUT.mkdir(exist_ok=True)
15+
OUT = Path("mail_dump")
16+
OUT.mkdir(exist_ok=True)
17+
1418

1519
def get_creds():
1620
if TOKEN.exists():
@@ -20,10 +24,20 @@ def get_creds():
2024
TOKEN.write_text(creds.to_json())
2125
return creds
2226

27+
2328
svc = build("gmail", "v1", credentials=get_creds())
2429

25-
resp = svc.users().messages().list(userId="me", maxResults=50, q="").execute()
30+
maxResults = 50
31+
if sys.argv[1:]:
32+
maxResults = int(sys.argv[1])
33+
34+
resp = svc.users().messages().list(userId="me", maxResults=maxResults, q="").execute()
2635
for m in resp.get("messages", []):
27-
raw = svc.users().messages().get(userId="me", id=m["id"], format="raw").execute()["raw"]
36+
raw = (
37+
svc.users()
38+
.messages()
39+
.get(userId="me", id=m["id"], format="raw")
40+
.execute()["raw"]
41+
)
2842
Path(OUT / f"{m['id']}.eml").write_bytes(b64d(raw.encode()))
2943
print("Done.")

0 commit comments

Comments
 (0)