diff options
Diffstat (limited to 'python/download_emails.py')
| -rw-r--r-- | python/download_emails.py | 27 |
1 files changed, 15 insertions, 12 deletions
diff --git a/python/download_emails.py b/python/download_emails.py index a9139ea3..8c2e980e 100644 --- a/python/download_emails.py +++ b/python/download_emails.py @@ -1,23 +1,18 @@ -import argparse import base64 import email import json import logging -import oauth2client import os import sys -from apiclient.discovery import build from apiclient import errors -from datetime import datetime -from httplib2 import Http -from oauth2client import client from pathlib import Path from pytz import timezone from send_email import get_gmail_service from email.message import EmailMessage from email.utils import parsedate_to_datetime + def ListMessagesWithLabels(service, user_id, label_ids=[]): """List all Messages of the user's mailbox with label_ids applied. @@ -49,6 +44,7 @@ def ListMessagesWithLabels(service, user_id, label_ids=[]): except errors.HttpError as error: print(json.loads(error.content.decode('utf-8'))['error']['message']) + def ListHistory(service, user_id, label_id=None, start_history_id=10000): """List History of all changes to the user's mailbox. @@ -137,20 +133,27 @@ def save_emails(update=True): labelsdict = labels_dict(GmailMessage._service, 'me') p = Path(os.getenv("DATA_DIR")) / Path('swaptions') - try: - with open(os.path.join(os.environ['DATA_DIR'], '.lastHistoryId')) as fh: - last_history_id = int(fh.read()) - except FileNotFoundError: - sys.exit() + if update: + try: + with open(os.path.join(os.environ['DATA_DIR'], '.lastHistoryId')) as fh: + last_history_id = int(fh.read()) + except FileNotFoundError: + sys.exit() email_list = ListHistory(GmailMessage._service, 'me', label_id=labelsdict['swaptions'], start_history_id=last_history_id) + existing_msgs = [] else: - email_list = ListMessagesWithLabels(GmailMessage._service, 'me', labelsdict['swaptions']) + email_list = ListMessagesWithLabels(GmailMessage._service, + 'me', + labelsdict['swaptions']) + existing_msgs = set(str(x).split("_")[1] for x in p.iterdir() if x.is_file()) for msg in email_list: + if msg['id'] in existing_msgs: + continue try: message = GmailMessage.from_id(msg['id']) print(message.history_id) |
