-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
68 lines (50 loc) · 2.25 KB
/
utils.py
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
59
60
61
62
63
64
65
66
67
68
from apiclient import errors
import base64
import email
from apiclient import errors
def ListMessagesMatchingQuery(service, user_id, query=''):
response = service.users().messages().list(userId=user_id, q=query).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(
userId=user_id, q=query, pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
def ListMessagesWithLabels(service, user_id, label_ids=[]):
response = service.users().messages().list(
userId=user_id, labelIds=label_ids).execute()
messages = []
if 'messages' in response:
messages.extend(response['messages'])
while 'nextPageToken' in response:
page_token = response['nextPageToken']
response = service.users().messages().list(
userId=user_id, labelIds=label_ids, pageToken=page_token).execute()
messages.extend(response['messages'])
return messages
def DeleteMessage(service, user_id, msg_id):
try:
service.users().messages().delete(userId=user_id, id=msg_id).execute()
print('Message with id: %s deleted successfully.' % msg_id)
except (errors.HttpError, error):
print('An error occurred: %s' % error)
def GetMessage(service, user_id, msg_id):
# try:
message = service.users().messages().get(userId=user_id, id=msg_id).execute()
# print('Message snippet: %s' % message['snippet'])
return message
# except (errors.HttpError, error):
# print('An error occurred: %s' % error)
def GetMimeMessage(service, user_id, msg_id):
try:
message = service.users().messages().get(userId=user_id, id=msg_id,
format='raw').execute()
# print('Message snippet: %s' % message['snippet'])
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
mime_msg = email.message_from_string(msg_str)
return mime_msg
except (errors.HttpError, error):
print('An error occurred: %s' % error)