-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemail_fetcher.py
295 lines (248 loc) · 10.6 KB
/
email_fetcher.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
'''
Kei Imada
20190627
The email fetcher for PureFreeFood
'''
from __future__ import print_function
import pickle
import os.path
import datetime as dt
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from apiclient import errors
from lxml import html
import base64
import shelve
import email
import pdb
import re
# If modifying these scopes, delete the file token.pickle.
SCOPES = [
'https://www.googleapis.com/auth/gmail.modify',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
'openid'
# Add other requested scopes.
]
# leeway around Noon on when eatclub gets hour food
EATCLUB_LEEWAY = dt.timedelta(hours=1)
# leeway after eatclub arrival for upforgrabber to forward us eatclubs
UPFORGRAB_LEEWAY = dt.timedelta(hours=6)
class EmailFetcher(object):
''' fetches emails '''
def __init__(self):
creds = None
# The file token.pickle stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
'credentials.json', SCOPES)
creds = flow.run_local_server()
# Save the credentials for the next run
with open('token.pickle', 'wb') as token:
pickle.dump(creds, token)
self.service = build('gmail', 'v1', credentials=creds)
self.sender_regex = re.compile('(.*) <(.*@purestorage\.com)>')
self.date_regex = re.compile('^Date: (.*)$')
# Fri, Jun 28, 2019 at 11:59 AM
self.eatclub_date_format = '%a, %b %d, %Y at %I:%M %p'
# Thu, 27 Jun 2019 11:43:27 -0700
self.forward_date_format1 = '%a, %d %b %Y %X %z (%Z)'
self.forward_date_format2 = '%a, %d %b %Y %X %z'
self.read_labels = {'removeLabelIds': ['UNREAD'],
'addLabelIds': ['INBOX']}
def get_unread_message_ids(self):
"""List all Messages of the user's mailbox matching the query.
Returns:
List of Message IDs that are unread
"""
unread_messages = ListMessagesMatchingQuery(
self.service, 'me', query='is:unread')
msg_ids = map(lambda m: m['id'], unread_messages)
return msg_ids
def mark_message_read(self, msg_id):
'''Marks a message as read in gmail account'''
return ModifyMessage(self.service, 'me', msg_id, self.read_labels)
def get_eatclub_message(self, msg_id):
"""Get a Message and returns a Mime Message if it's an EATClub message.
Args:
msg_id: The ID of the Message required.
Returns:
A dictionary if the email is from EATClub else None {
'sender_name' (str): The name of the sender
'sender_address' (str): The address of the sender
'html_content' (HTML str): The HTML content of the message in string format
}
"""
message = {} # to return
# get the mime message
mime_message = GetMimeMessage(self.service, 'me', msg_id)
# set send_name and send_address
sender = mime_message['From']
print(sender)
result = self.sender_regex.match(sender)
if not result:
print(
'EmailFetcher.get_eatclub_message: Unable to parse sender %s as Pure Storage account' % sender)
return None
message['sender_name'], message['sender_address'] = result.groups()
# set html_content
message['html_content'] = None
for part in mime_message.get_payload():
if part.get_content_type() == 'text/html':
if message['html_content'] is None:
message['html_content'] = ''
try:
forward_date = dt.datetime.strptime(
mime_message['Date'], self.forward_date_format1).replace(tzinfo=None)
except ValueError:
forward_date = dt.datetime.strptime(
mime_message['Date'], self.forward_date_format2).replace(tzinfo=None)
content = part.get_payload()
if self._is_valid_eatclub_content(content, forward_date):
print(
'EmailFetcher.get_eatclub_message: Found Valid Up for Grabs email!')
print(content[:300])
message['html_content'] += content
break
if message['html_content'] is None:
print('EmailFetcher.get_eatclub_message: Content not found')
return None
return message
def _is_valid_eatclub_content(self, content, forward_date):
''' Given xml content, determines whether or not it is eatclub content that is time relevant '''
# check if content is not empty
if len(content.strip()) <= 0:
return False
# check if it is an EAT Club content
tree = html.fromstring(content)
if len(tree.xpath('//div/strong[contains(text(), "EAT Club")]')) <= 0:
return False
# check if the dates fit the leeways we defined
dates = list(
filter(None, map(lambda s: self.date_regex.match(s), tree.xpath('//text()'))))
if len(dates) <= 0:
return False
date = dates[0].groups()[0] # first one because its in the header
eatclub_date = dt.datetime.strptime(date, self.eatclub_date_format)
noon = dt.datetime.combine(dt.datetime.now().date(), dt.time(12))
if eatclub_date.weekday() not in [0, 2, 4]:
# not Mon, Wed, or Fri
return False
if abs(eatclub_date - noon) > EATCLUB_LEEWAY:
# past eatclub leeway
return False
if forward_date < eatclub_date and forward_date - eatclub_date > UPFORGRAB_LEEWAY:
# past upforgrab leeway and forward before eatclub (time machine?)
return False
return True
def ListMessagesMatchingQuery(service, user_id, query=''):
"""List all Messages of the user's mailbox matching the query.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
query: String used to filter messages returned.
Eg.- 'from:user@some_domain.com' for Messages from a particular sender.
Returns:
List of Messages that match the criteria of the query. Note that the
returned list contains Message IDs, you must use get with the
appropriate ID to get the details of a Message.
"""
try:
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
except errors.HttpError as error:
print('An error occurred: %s' % error)
def GetHTMLMessage(service, user_id, msg_id):
"""Get a Message and use it to create a HTML Message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The ID of the Message required.
Returns:
A HTML Message, consisting of data from Message.
"""
try:
message = service.users().messages().get(userId=user_id, id=msg_id,
format='raw').execute()
msg_str = base64.urlsafe_b64decode(message['raw'].encode('ASCII'))
return msg_str
except errors.HttpError as error:
print('An error occurred: %s' % error)
def GetMimeMessage(service, user_id, msg_id):
"""Get a Message and use it to create a MIME Message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The ID of the Message required.
Returns:
A MIME Message, consisting of data from Message.
"""
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.parser.BytesParser().parsebytes(msg_str)
return mime_msg
except errors.HttpError as error:
print('An error occurred: %s' % error)
def ModifyMessage(service, user_id, msg_id, msg_labels):
"""Modify the Labels on the given Message.
Args:
service: Authorized Gmail API service instance.
user_id: User's email address. The special value "me"
can be used to indicate the authenticated user.
msg_id: The id of the message required.
msg_labels: The change in labels.
Returns:
Modified message, containing updated labelIds, id and threadId.
"""
try:
message = service.users().messages().modify(userId=user_id, id=msg_id,
body=msg_labels).execute()
label_ids = message['labelIds']
print('Message ID: %s - With Label IDs %s' % (msg_id, label_ids))
return message
except errors.HttpError as error:
print('An error occurred: %s' % error)
def CreateMsgLabels():
"""Create object to update labels.
Returns:
A label update object.
"""
return None
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
fetcher = EmailFetcher()
unread_ids = fetcher.get_unread_message_ids()
for msg_id in unread_ids:
msg = fetcher.get_eatclub_message(msg_id)
if msg:
print(msg.keys())
assert(fetcher.mark_message_read(msg_id))
if __name__ == '__main__':
main()