-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
134 lines (92 loc) · 2.82 KB
/
server.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
import web
import views
from views import render
from config import default_config as config
import json
import os
import email
web.config.debug = False
urls = (
'/', 'index',
'/messages', 'messages',
'/messages/(.+)', 'messages'
)
render = web.template.render('templates/')
base_path = 'email_dataset/_miguel/gmail-mbox-'
class index:
def GET(self):
return render.base(views.home(session), title='Email Ranker')
class messages:
def POST(self):
x = web.input(filename={})
print "POST REQUEST"
print x
filename = x['filename']
new_ranking = x['ranking']
msg_path = x['msg_path']
print filename
print new_ranking
print msg_path
with open(msg_path + filename, 'r+') as f:
content = f.read()
msg_email = email.message_from_string(content)
msg_email.replace_header('X-Mailcube-Ranking', str(new_ranking))
f.seek(0, 0)
f.write(msg_email.as_string(True))
f.truncate()
def GET(self, page_num):
path = base_path + page_num + "/"
print path
msg_filenames = []
for (dirpath, dirnames, filenames) in os.walk(path):
msg_filenames.extend(filenames)
break
msgs = []
print "Len: " + str(len(msg_filenames))
for msg_filename in msg_filenames:
msg = {
'filename': msg_filename,
'path': path
}
with open(path + "/" + msg_filename, 'r') as content_file:
email_content = content_file.read()
msg['email'] = email.message_from_string(email_content)
# if msg['email'].is_multipart():
#
# for part in msg['email'].walk():
#
# ctype = part.get_content_type()
# cdispo = str(part.get('Content-Disposition'))
#
# if ctype == 'text/plain' and 'attachment' not in cdispo:
# msg['body'] = part.get_payload(decode=True)
# break
#
#
# else:
# msg['body'] = msg['email'].get_payload(decode=True)
# if msg.get('body', None) is None:
# msg['body'] = ''
#
# msg['body'] = unicode(msg['body'], 'utf-8')
current_ranking = msg['email'].get("X-Mailcube-Ranking")
msg['ranking'] = current_ranking
labels = msg['email'].get('X-Gmail-Labels', None)
if labels is not None and "Chat" in labels:
continue
if msg['email'].get('From') is not None and 'miguel.gazela@gmail.com' in msg['email'].get('From'):
continue
msgs.append(msg)
print "Len: " + str(len(msgs))
session.msgs = msgs
return render.base(views.messages(session), title='Messages')
class MyApplication(web.application):
def run (self, port=3030, *middleware):
func = self.wsgifunc(*middleware)
return web.httpserver.runsimple(func, ('0.0.0.0', port))
if __name__ == "__main__":
app = MyApplication(urls, globals())
app.internalerror = web.debugerror
session = web.session.Session(app, web.session.DiskStore('sessions_m3'))
port = int(os.environ.get('PORT', 3030))
app.run(port=port)