forked from googleworkspace/python-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreads.py
More file actions
58 lines (47 loc) · 2.03 KB
/
Copy paththreads.py
File metadata and controls
58 lines (47 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Copyright 2018 Google LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
# [START gmail_show_chatty_threads]
from __future__ import print_function
import google.auth
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
def show_chatty_threads():
"""Display threads with long conversations(>= 3 messages)
Return: None
Load pre-authorized user credentials from the environment.
TODO(developer) - See https://developers.google.com/identity
for guides on implementing OAuth2 for the application.
"""
creds, _ = google.auth.default()
try:
# create gmail api client
service = build('gmail', 'v1', credentials=creds)
# pylint: disable=maybe-no-member
threads = service.users().threads().list(userId='me').execute().get('threads', [])
for thread in threads:
tdata = service.users().threads().get(userId='me', id=thread['id']).execute()
nmsgs = len(tdata['messages'])
# skip if <3 msgs in thread
if nmsgs > 2:
msg = tdata['messages'][0]['payload']
subject = ''
for header in msg['headers']:
if header['name'] == 'Subject':
subject = header['value']
break
if subject: # skip if no Subject line
print(F'- {subject}, {nmsgs}')
except HttpError as error:
print(F'An error occurred: {error}')
if __name__ == '__main__':
show_chatty_threads()
# [END gmail_show_chatty_threads]