-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitor.py
More file actions
73 lines (71 loc) · 2.6 KB
/
monitor.py
File metadata and controls
73 lines (71 loc) · 2.6 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
import time, base64
from remote import *
from user import User
from helpers import send_to_queue
import email.utils, oauth2client, datetime
def get_header(message, name):
for header in message['payload']['headers']:
if header['name'] == name:
return header['value']
return None
while True:
for u in users.find({'disabled': True}):
user = User(u['email'])
expiry_date = datetime.datetime.utcnow() - datetime.timedelta(hours=1)
if user.get('disabled_at', expiry_date) <= expiry_date:
user.set('disabled', False)
for u in users.find({'disabled': {'$ne': True}}):
user = User(u['email'])
try:
messages, history_token = user.get_new_messages()
except oauth2client.client.AccessTokenRefreshError:
print "Disabled User {}".format(user.get('_id'))
user.update({'disabled': True, 'disabled_at': datetime.datetime.utcnow()})
continue
user.set('history_token', history_token)
for message in messages:
if 'TRASH' in message.get('labelIds', []):
continue
if processed_data.find_one({'email_id': message['id']}):
continue
if message['payload']['mimeType'] in ['text/plain', 'text/html']:
body = message['payload']['body'].get('data')
mime_type = message['payload']['mimeType']
else:
parts = message['payload']['parts']
body = None
mime_type = None
order = ['text/plain', 'text/html']
from_email = email.utils.parseaddr(get_header(message, 'From'))[1]
if any([x in from_email for x in ['eventbrite.com']]):
order.reverse()
for part in parts:
if part['mimeType'] == order[0]:
body = part['body'].get('data')
mime_type = part['mimeType']
break
if body is None:
for part in parts:
if part['mimeType'] == order[1]:
body = part['body'].get('data')
mime_type = part['mimeType']
break
if body is None:
continue
del message['payload']['parts']
body = str(body)
try:
message['payload']['body'] = base64.urlsafe_b64decode(body)
except TypeError:
continue
message['userid'] = u['_id']
message['payload']['headers'].append({"name": "X-Time-Zone", "value": user.get_timezone()})
message['payload']['mimeType'] = mime_type
try:
object_id = raw_data.insert(message)
send_to_queue({'object_id': str(object_id)})
print "Processed {}".format(message['id'])
except:
print "Ignored duplicate {}".format(message['id'])
continue
time.sleep(10)