11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
33
4- from pathlib import Path
54from base64 import urlsafe_b64decode as b64d
5+ from pathlib import Path
6+ import sys
7+
8+ from google .oauth2 .credentials import Credentials
69from google_auth_oauthlib .flow import InstalledAppFlow
710from googleapiclient .discovery import build
8- from google .oauth2 .credentials import Credentials
911
1012SCOPES = ["https://www.googleapis.com/auth/gmail.readonly" ]
1113CREDS = "client_secret.json"
1214TOKEN = 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
1519def 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+
2328svc = 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 ()
2635for 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 ()))
2943print ("Done." )
0 commit comments