-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuth.py
More file actions
125 lines (113 loc) · 5.09 KB
/
Copy pathAuth.py
File metadata and controls
125 lines (113 loc) · 5.09 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
from oauth2client.client import flow_from_clientsecrets
from oauth2client.client import FlowExchangeError
from googleapiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools
import base64
import logging
import json
class Auth:
"""
* Class Auth Constructor
"""
def __init__(self):
pass
"""
* Auth_user authenticates the user in case it is a new user it will pop up
* in the browser to login and give permission to the user in case its a not
* new user it will load the already exising token
"""
def Authenticate (self):
#The following scope allows to all read/write operations
SCOPES = 'https://mail.google.com/'
store = file.Storage('token.json')
creds = store.get()
if not creds or creds.invalid:
flow = client.flow_from_clientsecrets('credentials.json', SCOPES)
creds = tools.run_flow(flow, store)
http = creds.authorize(Http())
self.GMAIL = build('gmail', 'v1', http=http)
"""
* getInbox retrieves all the messages in the inbox that have the inputed
* labels on it
* @labels {list} list of strings with the labels to look for
* Returns a list with dictionaries containing the information from the mails
* like subject,date,sender and body of the mail
"""
def getInbox(self,labels):
mails = []
messages= self.GMAIL.users().messages().list(userId='me',labelIds=labels).execute()
if messages['resultSizeEstimate'] != 0:
messages = messages['messages']
print("Mensajes totales: ",str(len(messages)))
for message in messages:
temp = {}
message_id = message['id']
message = self.GMAIL.users().messages().get(userId='me',id=message_id).execute()
temp['ID'] = message_id
payload = message['payload'] #This is where is contained the information of the message
headers = payload['headers'] #Contained attributes like subject,sender and text
#Retrieves main information of the message
subject = date = sender = None
for header in headers:
if header['name'] == 'Subject':
subject = header['value']
temp['Subject'] = subject
else:
pass
if header['name'] == 'Date':
date = header['value']
temp['Date'] = str(date)
else:
pass
if header['name'] == 'From':
sender = header['value']
temp['Sender'] = sender
else:
pass
#Snippet is the summary of the body of the mail
temp['Snippet'] = message['snippet']
#Fetch the body of the message
if subject == '[PlainText]':
try:
message_parts = payload['parts']
first_part = message_parts[0]
body_part = first_part['body']
data_part = body_part['data']
first_filter = data_part.replace('-','+')
first_filter = first_filter.replace('_','/')
second_filter = base64.b64decode(first_filter)
message_body = second_filter.decode('UTF-8')
temp["Body"] = message_body.replace("\r\n","")
except:
print("An error ocurred retrieving the body the plain text message!")
else:
try:
body_part = payload["body"]
body = body_part["data"]
except:
message_parts = payload['parts']
first_part = message_parts[0]
body_part = first_part['body']
body = body_part['data']
#Decodes de the message
first_filter = body.replace('-','+')
first_filter = first_filter.replace('_','/')
second_filter = base64.b64decode(first_filter)
body = second_filter.decode('UTF-8')
temp["Body"] = body
mails.append(temp)
#Removes UNREAD label from the message NOT WORKING YET
self.GMAIL.users().messages().modify(userId='me',id=message_id,body={'removeLabelIds':['UNREAD']}).execute()
return mails
"""
* sendMessage sends the message created by the user
* @message {object} Is a json object which contains the message to be sent
* encoded in a base64 format
"""
def sendMessage(self,body):
try:
message = self.GMAIL.users().messages().send(userId='me',body=body).execute()
print("Message Sent")
except errors.HttpError as error:
print ("An error ocurred: ",error)