aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--python/download_emails.py34
1 files changed, 4 insertions, 30 deletions
diff --git a/python/download_emails.py b/python/download_emails.py
index 1453b51c..aa0e33d0 100644
--- a/python/download_emails.py
+++ b/python/download_emails.py
@@ -11,7 +11,7 @@ from oauth2client import tools
import json
import base64
import binascii
-
+from send_email import get_gmail_service
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
@@ -20,31 +20,6 @@ CLIENT_SECRET_FILE = 'secret.json'
APPLICATION_NAME = 'Swaptions'
-def get_credentials():
- """Gets valid user credentials from storage.
-
- If nothing has been stored, or if the stored credentials are invalid,
- the OAuth2 flow is completed to obtain the new credentials.
-
- Returns:
- Credentials, the obtained credential.
- """
- home_dir = os.path.expanduser('~')
- credential_dir = os.path.join(home_dir, '.credentials')
- if not os.path.exists(credential_dir):
- os.makedirs(credential_dir)
- credential_path = os.path.join(credential_dir,
- 'gmail-quickstart.json')
-
- store = oauth2client.file.Storage(credential_path)
- credentials = store.get()
- if not credentials or credentials.invalid:
- flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
- flow.user_agent = APPLICATION_NAME
- credentials = tools.run_flow(flow, store, flags)
- print('Storing credentials to ' + credential_path)
- return credentials
-
def ListMessagesWithLabels(service, user_id, label_ids=[]):
"""List all Messages of the user's mailbox with label_ids applied.
@@ -102,10 +77,9 @@ def main():
Creates a Gmail API service object and outputs a list of label names
of the user's Gmail account.
"""
- credentials = get_credentials()
- service = build('gmail', 'v1', http=credentials.authorize(Http()))
+ service = get_gmail_service()
labelsdict = getListLabels(service, 'me')
- p = Path('quotes')
+ p = Path('../../data/swaptions/')
current_msgs = set([f.name for f in p.iterdir() if f.is_file()])
for msg in ListMessagesWithLabels(service, 'me', labelsdict['swaptions']):
if msg['id'] not in current_msgs:
@@ -115,7 +89,7 @@ def main():
print("error decoding {0}".format(msg['id']))
continue
else:
- email = Path("../../data/swaptions/{0}".format(msg['id']))
+ email = p / msg['id']
with email.open("w") as fh:
fh.write(subject + "\r\n")
fh.write(content)