-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
86 lines (65 loc) · 2.06 KB
/
api.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
# Import env vars
from dotenv import load_dotenv
load_dotenv()
import os
import uuid
import flask
from flask import jsonify, request
from flask.helpers import send_file, send_from_directory
from flask_cors import CORS
from algorithms import all
from simulation import map
app = flask.Flask(__name__, static_url_path='')
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'
app.config['UPLOAD_FOLDER'] = os.path.join(os.getenv('DATA_DIR'), 'uploads')
# Make folders if they don't exist
generated_dir = os.path.join(os.getenv('DATA_DIR'), 'generated');
if not os.path.exists(generated_dir):
os.makedirs(generated_dir)
if not os.path.exists(app.config['UPLOAD_FOLDER']):
os.makedirs(app.config['UPLOAD_FOLDER'])
@app.route('/map', methods=['GET'])
def get_map():
x = float(request.args.get('x'))
y = float(request.args.get('y'))
alpha = float(request.args.get('alpha'))
beta = float(request.args.get('beta'))
gamma = float(request.args.get('gamma'))
video_path = map.simulate(x, y, alpha, beta, gamma)
return send_file(video_path, mimetype='video/mp4')
@app.route('/static/<path:path>')
def send_static(path):
return send_from_directory('public', path)
@app.route('/algorithms', methods=['GET'])
def algorithms():
algos = []
for algo in all.algorithms:
algos.append({"id": algo.id, "name": algo.name})
return jsonify(algos)
@app.route('/algorithms/<algo_id>', methods=['POST'])
def process_video(algo_id):
if 'file' not in request.files:
return 'missing file', 400
file = request.files['file']
if file.filename == '':
return 'missing file', 400
location = None
if file:
filename = uuid.uuid4().hex
location = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(location)
# Find algo
algo = None
for a in all.algorithms:
if a.id == algo_id:
algo = a
break
if algo is None:
return 'algorithm doesn\'t exist', 400
res = algo.process(location)
# Delete uploaded video
os.remove(location)
return jsonify(res)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0')