aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/download_emails.py23
1 files changed, 15 insertions, 8 deletions
diff --git a/python/download_emails.py b/python/download_emails.py
index aa0e33d0..5e0eb95b 100644
--- a/python/download_emails.py
+++ b/python/download_emails.py
@@ -51,7 +51,16 @@ def ListMessagesWithLabels(service, user_id, label_ids=[]):
except errors.HttpError as error:
print(json.loads(error.content.decode('utf-8'))['error']['message'])
-def getListLabels(service, user_id):
+def labels_dict(service, user_id):
+ """Returns a dictionary mapping labels to labelids.
+
+ Args:
+ service: Authorized Gmail API service instance.
+ user_id: User's email address. The special value "me"
+
+ Returns:
+ dictionary mapping labels to labelids.
+ """
try:
response = service.users().labels().list(userId=user_id).execute()
labels = response['labels']
@@ -67,18 +76,16 @@ def get_msg(service, user_id, msg_id):
print(json.loads(error.content.decode('utf-8'))['error']['message'])
def msg_content(msg):
+ """Extract subject and body from a gmail message"""
subject = [x['value'] for x in msg['payload']['headers'] if x['name']=='Subject'][0]
content = base64.b64decode(msg['payload']['body']['data']).decode('utf-8')
return subject, content
-def main():
- """Shows basic usage of the Gmail API.
+def update_emails():
+ """Download new emails that were labeled swaptions."""
- Creates a Gmail API service object and outputs a list of label names
- of the user's Gmail account.
- """
service = get_gmail_service()
- labelsdict = getListLabels(service, 'me')
+ labelsdict = labels_dict(service, 'me')
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']):
@@ -95,4 +102,4 @@ def main():
fh.write(content)
if __name__ == '__main__':
- main()
+ update_emails()