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())
|
token.write(creds.to_json())
|
||||||
|
|
||||||
def getUnreadMails():
|
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()
|
_initCreds()
|
||||||
|
|
||||||
service = build('gmail', 'v1', credentials=creds)
|
service = build('gmail', 'v1', credentials=creds)
|
||||||
nbUnreadMessages = 0
|
|
||||||
pageToken = ''
|
pageToken = ''
|
||||||
|
threads = set()
|
||||||
while True:
|
while True:
|
||||||
results = service.users().messages().list(userId='me', labelIds=['UNREAD'],\
|
results = service.users().messages().list(userId='me', labelIds=['UNREAD'],\
|
||||||
includeSpamTrash=False, pageToken=pageToken)\
|
includeSpamTrash=False, pageToken=pageToken)\
|
||||||
.execute()
|
.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', '')
|
pageToken = results.get('nextPageToken', '')
|
||||||
if not pageToken: break
|
if not pageToken: break
|
||||||
|
|
||||||
return nbUnreadMessages
|
return len(threads)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user