-
Notifications
You must be signed in to change notification settings - Fork 0
/
flask_app.py
321 lines (271 loc) · 12.2 KB
/
flask_app.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import httplib2
import re
import json
import redis
import base64
import quopri
import urllib
from premailer import transform
from apiclient import errors
from flask import Flask, redirect, url_for, Response, render_template, session, request
from googleapiclient import discovery
from googleapiclient.http import BatchHttpRequest
from oauth2client import client
from flask_kvsession import KVSessionExtension
from simplekv.memory.redisstore import RedisStore
from fixbadunicode import fix_bad_unicode
redis_url = 'redis://rediscloud:[email protected]:16921'
conn = redis.StrictRedis.from_url(redis_url)
store = RedisStore(conn)
app = Flask(__name__)
KVSessionExtension(store, app)
app.config['SECRET_KEY'] = 'super secret key'
@app.route('/')
def index():
session['contactsNextPageToken'] = 0
session['contacts'] = []
session['seen'] = []
session['mails'] = []
session['maillist'] = []
if 'credentials' not in session:
return redirect(url_for('oauth2callback'))
credentials = client.OAuth2Credentials.from_json(session['credentials'])
if credentials.access_token_expired:
return redirect(url_for('oauth2callback'))
else:
return render_template('index.html')
@app.route('/oauth2callback')
def oauth2callback():
flow = client.flow_from_clientsecrets('/home/Andersdm/emailody/client_secrets.json'
, scope='https://www.googleapis.com/auth/gmail.readonly',
redirect_uri=url_for('oauth2callback', _external=True))
if 'code' not in request.args:
auth_uri = flow.step1_get_authorize_url()
return redirect(auth_uri)
else:
auth_code = request.args.get('code')
credentials = flow.step2_exchange(auth_code)
session['credentials'] = credentials.to_json()
return redirect(url_for('index'))
@app.route('/v1/gmail/contacts/<int:pagenr>')
def contacts(pagenr):
# Authorize the httplib2.Http object with our credentials
credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http(cache='.cache'))
# Build the Gmail service from discovery
gmail_service = discovery.build('gmail', 'v1', http=http)
while len(session['seen']) + 1 < pagenr * 10:
# Retrieve a page of threads
# Print ID for each thread
# if threads['threads']:
# for thread in threads['threads']:
# print 'Thread ID: %s' % (thread['id'])
def contactscallbackfunc(result, results, moreresults):
if 'UNREAD' in results['labelIds']:
Unread = True
else:
Unread = False
for header in results['payload']['headers']:
if header['name'].lower() == 'date':
Date = header['value']
if header['name'].lower() == 'from':
From = header['value']
address = From.split()[-1]
address = re.sub(r'[<>]', '', address)
Address = address.strip()
Domain = Address.split('@')[1]
if Domain == 'facebookmail.com':
Domain = 'facebook.com'
Name = From.rsplit(' ', 1)[0]
From = From.replace('"', '')
Name = Name.replace('"', '')
if From not in session['seen'] and Address != session['user_address']:
Contact = {
'id': len(session['seen']),
'date': Date,
'name': Name,
'initial': Name[0].upper(),
'address': Address,
'domain': Domain,
'snippet': results['snippet'],
'unread': Unread,
}
session['contacts'].append(Contact)
session['seen'].append(From)
#query = ''
#if not session['seen']:
# pass
#else:
# query = 'from:('
# for From in session['seen']:
# query = query + "-'" + From + "', "
# query = query + ')'
# query = "\"to:'" + contact + "' AND from:me \" OR from:'" + contact + "'"
user_id = gmail_service.users().getProfile(userId='me').execute()
session['user_address'] = user_id['emailAddress']
message_ids = gmail_service.users().messages().list(userId='me'
, maxResults=50,
pageToken=session['contactsNextPageToken']).execute()
batchContacts = BatchHttpRequest(callback=contactscallbackfunc)
session['contactsNextPageToken'] = message_ids['nextPageToken']
for msg_id in message_ids['messages']:
batchContacts.add(gmail_service.users().messages().get(userId='me'
, id=msg_id['id'], format='metadata',
metadataHeaders=['from', 'date']))
batchContacts.execute()
if pagenr - 1 == 0:
start = 0
end = 10
else:
start = (pagenr - 1) * 10
end = pagenr * 10
response = {'nextPageToken': session['contactsNextPageToken'],
'contacts': (session['contacts'])[start:end]}
js = json.dumps(response)
resp = Response(js, status=200, mimetype='application/json')
return resp
@app.route('/v1/gmail/message/<msg_id>')
def get_message(msg_id):
credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http(cache='.cache'))
gmail_service = discovery.build('gmail', 'v1', http=http)
try:
message = gmail_service.users().messages().get(userId='me',
id=msg_id, format='full').execute()
message_payload = message['payload']
if 'parts' in message_payload:
for part in message_payload['parts']:
if (part['mimeType'] == 'text/html') or (part['mimeType'] == 'text/plain'):
print "#0"
message_body = part['body']
for header in part['headers']:
if header['name'] == 'Content-Type':
charset = header['value'].rpartition('charset=')[2]
charset = re.sub('"', '', charset, flags=re.DOTALL)
if header['name'] == 'Content-Transfer-Encoding':
transferencoding = header['value']
if 'parts' in part:
print "#1"
for part in part['parts']:
if (part['mimeType'] == 'text/html') or (part['mimeType'] == 'text/plain'):
message_body = part['body']
for header in part['headers']:
if header['name'] == 'Content-Type':
charset = header['value'].rpartition('charset=')[2]
charset = re.sub('"', '', charset, flags=re.DOTALL)
if header['name'] == 'Content-Transfer-Encoding':
transferencoding = header['value']
msg_str = base64.urlsafe_b64decode((message_body['data']).encode('ascii'))
# msg_str = msg_str.decode(charset)
# msg_str = msg_str.encode('utf-8')
else:
message_body = message_payload['body']
print "#3"
for header in message_payload['headers']:
if header['name'] == 'Content-Type':
charset = header['value'].rpartition('charset=')[2]
charset = re.sub('"', '', charset, flags=re.DOTALL)
if header['name'] == 'Content-Transfer-Encoding':
transferencoding = header['value']
msg_str = base64.urlsafe_b64decode((message_body['data']).encode('ascii'))
# msg_str = msg_str.encode('utf-8')
msg_str = '<pre>' + msg_str + '</pre>'
print transferencoding
#if transferencoding == 'quoted-printable':
# print "YEEEEEEEEP"
# msg_str = quopri.decodestring(msg_str)
#if charset != 'UTF-8':
print charset
#msg_str = msg_str.decode(charset, errors='ignore')
msg_str = unicode(msg_str)
msg_str = fix_bad_unicode(msg_str)
#msg_str = msg_str.encode('utf-8', errors='ignore')
#msg_str = msg_str.decode(charset)
#msg_str = msg_str.encode('utf-8')
#if charset.lower() is not 'utf-8':
msg_str = re.sub('!important', '', msg_str, flags=re.DOTALL)
transformed_msg = transform(msg_str)
clean_msg = re.sub('<html>', '', transformed_msg,
flags=re.DOTALL)
clean_msg = re.sub('</html>', '', clean_msg, flags=re.DOTALL)
clean_msg = re.sub('<head>.*?</head>', '', clean_msg,
flags=re.DOTALL)
clean_msg = re.sub('<head>.*?</head>', '', clean_msg,
flags=re.DOTALL)
clean_msg = clean_msg.replace('body', 'div', 1)
clean_msg = re.sub('<body>', '', clean_msg, flags=re.DOTALL)
clean_msg = re.sub('</body>', '', clean_msg, flags=re.DOTALL)
clean_msg = re.sub('<style type="text/css">.*?</style>', '',
clean_msg, flags=re.DOTALL)
clean_msg = re.sub('<style type="text/css">.*?</style>', '',
clean_msg, flags=re.DOTALL)
return clean_msg
except errors.HttpError, error:
# return message
print 'An error occurred: %s' % error
@app.route('/v1/gmail/messages/<int:contactId>', methods=['GET'])
def get_messages(contactId):
contact = session['seen'][contactId]
contact_address = contact.split()[-1]
contact_address = re.sub(r'[<>]', '', contact_address)
contact_address = contact_address.strip()
credentials = client.OAuth2Credentials.from_json(session['credentials'])
http = credentials.authorize(httplib2.Http(cache='.cache'))
# Build the Gmail service from discovery
gmail_service = discovery.build('gmail', 'v1', http=http)
def mailscallbackfunc(result, results, moreresults):
if 'labelIds' in results:
if 'UNREAD' in results['labelIds']:
Unread = True
else:
Unread = False
else:
Unread = None
for header in results['payload']['headers']:
if header['name'].lower() == 'date':
Date = header['value']
if header['name'].lower() == 'from':
From = header['value']
from_address = From.split()[-1]
from_address = re.sub(r'[<>]', '', from_address)
from_address = from_address.strip()
if from_address.lower() == session['user_address']:
Sent = True
else:
Sent = False
if header['name'].lower() == 'subject':
Subject = header['value']
try:
Subject # does a exist in the current namespace
except NameError:
Subject = 'Unknown Subject' # nope
Contact = {
'messageId': results['id'],
'date': Date,
'subject': Subject,
'snippet': results['snippet'],
'unread': Unread,
'sent': Sent,
}
session['mails'].append(Contact)
query = "(from:me to:" + contact_address + ") OR (from:" + contact + ")"
print "#######query############"
print query
message_ids = gmail_service.users().messages().list(userId='me',
maxResults=10, q=query).execute()
#labelIds='INBOX',
batchMails = BatchHttpRequest(callback=mailscallbackfunc)
for msg_id in message_ids['messages']:
batchMails.add(gmail_service.users().messages().get(userId='me'
, id=msg_id['id'], format='metadata',
metadataHeaders=['from', 'date', 'subject']))
batchMails.execute()
response = {'messages': session['mails']}
js = json.dumps(response)
resp = Response(js, status=200, mimetype='application/json')
return resp