-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
128 lines (89 loc) · 3.27 KB
/
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
import os
from flask import Flask, flash, request, redirect, render_template
from werkzeug.utils import secure_filename
from src.formator import main as formator
from src.analysis import main as analysis
import time # testing speed
import src.db as db # database
from shutil import copy
app=Flask(__name__)
app.secret_key = "secret key"
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
# Get current path
path = os.getcwd()
# file Upload
UPLOAD_FOLDER = os.path.join(path, 'uploads')
# Make directory if uploads is not exists
if not os.path.isdir(UPLOAD_FOLDER):
os.mkdir(UPLOAD_FOLDER)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
# Allowed extension you can set your own
ALLOWED_EXTENSIONS = {'json'}
def allowed_file(filename):
""" Return True if file is appropriate type in ALLOWED_EXTENSIONS
"""
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/', methods=['POST'])
def upload_file():
# Uploading Multiple File
if request.method == 'POST':
if 'files[]' not in request.files:
flash('No file part')
return redirect(request.url)
files = request.files.getlist('files[]')
for i, f in enumerate(files):
if f and allowed_file(f.filename):
# fname = secure_filename(f.filename)
fname = "675676476587585856567v{}.json"
f.save(os.path.join(app.config['UPLOAD_FOLDER'], fname.format(i)))
# Format the Uploaded Files -> Combine, Store and Delete -src/formator.py
start = time.time()
data = formator()
end = time.time() - start
# debug
print('Formator time:', end)
# TODO: upload data to SQLite Database
start = time.time()
# create tables, and insert data into database
db._init('messages.db', data)
end = time.time() - start
print("Database init(create table, insert):", end)
# Run the analysis -src/analysis.py
start = time.time()
report = analysis(data)
end = time.time() - start
# delete database tables
db.delete_database('messages.db')
# debug
print('Analysis time:', end)
return render_template('index.html', data = report)
def example(path: str):
""" return the page given an example path to a file
"""
# collect test file
f = os.path.join('.', 'static', 'test_file', path)
# copy test file to uploads folder
copy(f, os.path.join('.', 'uploads'))
# # Format the Uploaded Files -> Combine, Store and Delete -src/formator.py
data = formator()
db._init('messages.db', data)
# # Run the analysis -src/analysis.py
report = analysis(data)
# delete database
db.delete_database('messages.db')
return render_template('index.html', data = report)
@app.route('/romeo_juliet')
def example_file_1():
return example('shakesphere_romeo_juliet.json')
@app.route('/theoffice')
def example_file_2():
return example('theoffice.json')
@app.route('/realconvo')
def example_file_3():
return example('Anthony_Abhi.json')
if __name__ == "__main__":
app.run(host='127.0.0.1',port=5000,debug=False,threaded=True)