-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
110 lines (98 loc) · 3.42 KB
/
app.py
File metadata and controls
110 lines (98 loc) · 3.42 KB
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
from flask import Flask,render_template, Response, jsonify, redirect
import sys
import flask
from init import *
from auth import *
from debug import init_logs_formatting
from werkzeug.utils import secure_filename
from werkzeug.datastructures import FileStorage
import logging
import json
import os
init_logs_formatting()
app, db= create_app()
app.config['UPLOAD_PATH'] = 'music'
#app.config['MAX_CONTENT_PATH']
app.config['UPLOAD_EXTENSIONS'] = ['.mp3', '.wav'] #if file_ext not in current_app.config['UPLOAD_EXTENSIONS']:abort(400)
#add music to db
@app.route('/addmusic')
def template_addmusic():
return render_template('addmusic.html')
@app.route('/addmusic', methods=['POST'])
def test_music_db():
genre = request.form.get('genre')
title = request.form.get('title')
author = request.form.get('author')
uploaded_file = request.files['file']
filename = secure_filename(uploaded_file.filename)
if filename != '':
file_ext = os.path.splitext(filename)[1]
file_name = os.path.splitext(filename)[0]
if file_ext not in app.config['UPLOAD_EXTENSIONS']:
print("test")
else:
new_single = Musics(genre=genre, title=title, author=author, link="music/"+ file_name + file_ext)
db.session.add(new_single)
db.session.commit()
uploaded_file.save(os.path.join(app.config['UPLOAD_PATH'], filename))
return redirect("http://localhost:5000/addmusic", code=302)
def get_musics_dict():
x = 1
value = []
while(x <= Musics.query.count()):
new_single = Musics.query.get(x)
print (x)
value.append({'id': x, 'genre': new_single.genre, 'title': new_single.title, 'author': new_single.author, 'link': new_single.link})
x +=1
return (value)
#Route to render GUI
@app.route('/musics')
def get_musics_route():
x = 1
value = []
while(x <= Musics.query.count()):
new_single = Musics.query.get(x)
print (x)
value.append({'id': x, 'genre': new_single.genre, 'title': new_single.title, 'author': new_single.author, 'link': new_single.link})
x +=1
return (jsonify(value))
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def handle_login_data():
username = request.form.get('username')
password = request.form.get('password')
x = login_gest(username, password, db)
#print(x)
if x == "FAILURE":
return render_template('login.html')
else :
return redirect("http://localhost:5000/addmusic", code=302)
#Route to stream music
@app.route('/play/<int:stream_id>')
def streammp3(stream_id):
def generate():
data = get_musics_dict()
count = 1
for item in data:
print(item['id'])
if item['id'] == stream_id:
song = item['link']
with open(song, "rb") as fwav:
data = fwav.read(1024)
while data:
yield data
data = fwav.read(1024)
logging.debug('Music data fragment : ' + str(count))
count += 1
return Response(generate(), mimetype="audio/mp3")
@app.route('/')
def main_route():
return redirect("http://localhost:5000/login", code=302)
#launch a Tornado server with HTTPServer.
if __name__ == "__main__":
port = 5000
db.create_all()
logging.debug("Started Server on port : " + str(port))
app.run(host='0.0.0.0')