-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgmail_client.py
More file actions
91 lines (84 loc) · 3.26 KB
/
Copy pathgmail_client.py
File metadata and controls
91 lines (84 loc) · 3.26 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
class GmailApp:
"""
Class for using Gmail API
https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/index.html
"""
def __init__(self, scopes=None, stored_credentials='credentials.json', key_file='client_secret.json'):
"""
Init the Gmail service
:param scopes: Authorized scopes
:param stored_credentials: Credentials used for the refresh, after first init
:param key_file: Main credentials from https://console.developers.google.com/apis/credentials
"""
if scopes is None:
scopes = []
store = file.Storage(stored_credentials)
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets(key_file, scopes)
creds = tools.run_flow(flow, store)
self.__service = build('gmail', 'v1', http=creds.authorize(Http()), cache_discovery=False)
def get_gmail_connection(self):
"""
:return: The current service
"""
return self.__service
def searchMessages(
self,
userId='me',
labelIds=None,
q=None,
pageToken=None,
maxResults=None,
includeSpamTrash=None,
fields=''
):
"""
Search for messages https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.messages.html#list
:param userId:
:param labelIds:
:param q:
:param pageToken:
:param maxResults:
:param includeSpamTrash:
:param fields:
:return:
"""
params = {}
params['userId'] = userId
params['labelIds'] = labelIds
params['q'] = q
params['pageToken'] = pageToken
params['maxResults'] = maxResults
params['includeSpamTrash'] = includeSpamTrash
params['fields'] = fields
return self.__service.users().messages().list(**params).execute()
def deleteMessages(self, ids, userId='me'):
"""
Delete messages https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.messages.html#batchDelete
:param ids: List if messages id
:param userId: User
:return:
"""
if not isinstance(ids, list):
raise ValueError('Ids should be a list of id')
else:
body = {}
body['ids'] = ids
return self.__service.users().messages().batchDelete(userId=userId, body=body).execute()
def trashMessages(self, messageId, userId='me'):
"""
Trash messages https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.messages.html#trash
:param messageId: list or one message id
:param userId: User
:return:
"""
if isinstance(messageId, list):
for m in messageId:
self.__service.users().messages().trash(userId=userId, id=m).execute()
return True
else:
return self.__service.users().messages().trash(userId=userId, id=messageId).execute()