-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
82 lines (50 loc) · 2.27 KB
/
webapp.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
#!flask/bin/python
import os
from flask import Flask, request, redirect, url_for, send_from_directory, jsonify, render_template, make_response, send_file
from werkzeug.utils import secure_filename
from helpers import allowed_file, generate_click
import json
import random
import string
CONVERT_FOLDER = './convert/'
CLICKS_FOLDER = './clicks/'
app = Flask(__name__)
app.config['CONVERT_FOLDER'] = CONVERT_FOLDER
app.config['CLICKS_FOLDER'] = CLICKS_FOLDER
# ---- Endpoints for Web App ----
@app.route('/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
if 'file' not in request.files:
flash('No file part')
return redirect(request.url)
file = request.files['file']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
freq = float(request.form['freq'])
duration = float(request.form['duration'])
vol_song = (int(request.form['vol_song']))/100
vol_click = (int(request.form['vol_click']))/100
retFile = generate_click(file, filename, freq, duration, vol_song, vol_click, app.config['CONVERT_FOLDER'])
return redirect(url_for('return_file', filename = retFile))
return render_template('home.html')
@app.route('/generated/<filename>')
def return_file(filename):
return send_from_directory(app.config['CONVERT_FOLDER'], filename)
# ---- Endpoint for Android App ----
@app.route('/generateFull', methods = ['POST'])
def generateFull():
if 'audioFile' not in request.files:
return render_template("noAudioFile.html"), 400
file = request.files['audioFile']
if file and allowed_file(file.filename):
click_freq = request.args.get("click_freq")
click_dur = request.args.get("click_dur")
if click_freq is None or click_dur is None:
return render_template('error.html'), 400
saveName = generate_click(file, file.filename, float(click_freq), float(click_dur), 1, 1, app.config['CONVERT_FOLDER'])
response = make_response(send_file(os.path.join(app.config['CONVERT_FOLDER'], saveName), attachment_filename = "converted.wav", as_attachment = True))
return response, 200
else:
return render_template('badFileType.html'), 400
app.run(host = "0.0.0.0")