Count number of threads instead of unread messages in gmail example

This commit is contained in:
Grégory Soutadé 2022-11-20 21:01:41 +01:00
parent 5690307c63
commit 7cd668af14

View File

@ -56,21 +56,22 @@ def _initCreds():
token.write(creds.to_json())
def getUnreadMails():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
Get number of unread threads (that may contain multiple messages)
"""
_initCreds()
service = build('gmail', 'v1', credentials=creds)
nbUnreadMessages = 0
pageToken = ''
threads = set()
while True:
results = service.users().messages().list(userId='me', labelIds=['UNREAD'],\
includeSpamTrash=False, pageToken=pageToken)\
.execute()
nbUnreadMessages += len(results.get('messages', []))
threads = threads.union(set([k['threadId'] for k in results['messages']]))
# Loop over all result pages (100 results per page by default)
pageToken = results.get('nextPageToken', '')
if not pageToken: break
return nbUnreadMessages
return len(threads)