Count number of threads instead of unread messages in gmail example
This commit is contained in:
parent
5690307c63
commit
7cd668af14
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue
Block a user