This repository has been archived by the owner on Dec 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtsm_flask.py
55 lines (40 loc) · 1.52 KB
/
rtsm_flask.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
import json, os, zipfile, pathlib
from io import BytesIO
from flask import Flask, render_template, request, redirect, url_for, send_file, Response
from utils import get_results, get_recording_files
app = Flask(__name__)
@app.route('/', methods=['GET', 'POST'])
def index():
if request.method == 'GET':
return render_template('index.html.j2')
if request.method == 'POST':
# form url from request body
query = request.form['query']
# render results page
return redirect(url_for('results', query=query))
@app.route('/results', methods=['GET'])
def results():
# get query
query = request.args['query']
# get results
response = get_results(query)
recordings = response['recordings']
recordings = sorted(recordings, key=lambda rec: rec['q'])
# render response
return render_template('results.html.j2', recordings=recordings, query=query)
@app.route('/download', methods=['POST'])
def download():
file_ids_json = request.form['fileIds']
file_ids = json.loads(file_ids_json)
query = request.form['query']
tmp_folder_path = pathlib.Path(get_recording_files(file_ids, query))
data = BytesIO()
with zipfile.ZipFile(data, mode='w') as z:
for filename in tmp_folder_path.iterdir():
z.write(filename)
data.seek(0)
return Response(data,
mimetype='application/zip',
headers={'Content-Disposition': 'attachment;filename=data.zip'})
if __name__ == '__main__':
app.run()