-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_app.py
More file actions
executable file
·66 lines (53 loc) · 1.52 KB
/
main_app.py
File metadata and controls
executable file
·66 lines (53 loc) · 1.52 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
#!/usr/bin/env python
import re
from jinja2 import Markup
from flask import Flask, request
import gmail_api
import logging
app = Flask(__name__)
logger = logging.getLogger('main_app')
app.gmail_api = gmail_api.gapi()
def linebreaks(value):
"""Converts newlines into <p> and <br />s."""
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
paras = re.split('\n{2,}', value)
try:
paras = [u'<p>%s</p>' % p.encode('ascii', errors='ignore').replace('\n', '<br />') for p in paras]
except Exception, e:
logger.error('Exception: {}'.format(e))
pass
try:
paras = u'\n\n'.join(paras)
except Exception, e:
logger.error('Exception: {}'.format(e))
pass
return Markup(paras)
@app.route('/')
def read_log_file():
with open('logfile.txt', 'r+') as f:
file_data = f.read()
file_data = linebreaks(file_data)
return """
<html>
<header><title>Email Attachment</title></header>
<body>
{}
</body>
</html>
""".format(file_data)
@app.route('/get_mail', methods=['POST'])
def get_mail():
logger.debug('request.data: {}'.format(request.data))
app.gmail_api.get_mail(request.data)
return str(200)
@app.route('/start')
def start():
logger.info('Initializing API')
gmail_api.start()
return str(200)
app.gmail_api.sub_to_topic()
app.gmail_api.stop()
app.gmail_api.watch()
if __name__ == '__main__':
logger.info('Starting App Manually')
app.run(host='0.0.0.0', port=1337)