/
app.py
63 lines (54 loc) · 2.21 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
import os
import random
import tempfile
from flask import Flask, render_template, send_from_directory, request, jsonify, flash
from werkzeug import secure_filename
# Configuration
SECRET_KEY = 'hin6bab8ge25*r=x&+5$0kn=-#log$pt^#@vrqjld!^2ci@g*b'
DEBUG = True
dirname, filename = os.path.split(os.path.abspath(__file__))
UPLOAD_FOLDER = '{0}/media/audio'.format(os.path.abspath(dirname))
ALLOWED_EXTENSIONS = set( ['wav', 'mp3'])
app = Flask(__name__)
app.config.from_object(__name__)
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS
@app.route('/', methods=['GET', 'POST'])
def home():
# if uploading
if request.method == 'POST':
# get the audio file
audio = request.files['audio_file']
# if the audio is of an allowed filetype
if audio and allowed_file(audio.filename):
# secure the filename
filename = secure_filename(audio.filename)
temp_directory = tempfile.mkdtemp(dir=app.config['UPLOAD_FOLDER'])
temp_folder = os.path.split(temp_directory)[1]
full_path = os.path.join(temp_directory,
filename)
print temp_directory
print full_path
audio.save(full_path)
return jsonify(filename=filename,
filepath=os.path.join('media','audio', temp_folder, filename),
)
return render_template('home.html')
@app.route('/iknow')
def iknow():
flash(u'Turn on your speakers.', 'info')
return render_template('iknow.html')
@app.route('/media/audio/<temp_directory>/<filename>')
def serve_temp_audio(temp_directory, filename):
return send_from_directory(
os.path.join(app.config['UPLOAD_FOLDER'],
temp_directory),
filename
)
@app.route('/media/audio/<filename>')
def serve_audio(filename):
return send_from_directory(app.config['UPLOAD_FOLDER'],
filename)
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)