-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathapp.py
More file actions
69 lines (58 loc) · 2.04 KB
/
app.py
File metadata and controls
69 lines (58 loc) · 2.04 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
from flask import Flask,render_template, Response
import sys
# Tornado web server
from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
#Debug logger
import logging
root = logging.getLogger()
root.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
ch.setLevel(logging.DEBUG)
formatter = logging.Formatter(
'%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
root.addHandler(ch)
def return_dict():
#Dictionary to store music file information
dict_here = [
{'id': 1, 'name': 'Acoustic Breeze', 'link': 'music/acousticbreeze.mp3', 'genre': 'General', 'chill out': 5},
{'id': 2, 'name': 'Happy Rock','link': 'music/happyrock.mp3', 'genre': 'Bollywood', 'rating': 4},
{'id': 3, 'name': 'Ukulele', 'link': 'music/ukulele.mp3', 'genre': 'Bollywood', 'rating': 4}
]
return dict_here
# Initialize Flask.
app = Flask(__name__)
#Route to render GUI
@app.route('/')
def show_entries():
general_Data = {
'title': 'Music Player'}
print(return_dict())
stream_entries = return_dict()
return render_template('simple.html', entries=stream_entries, **general_Data)
#Route to stream music
@app.route('/<int:stream_id>')
def streammp3(stream_id):
def generate():
data = return_dict()
count = 1
for item in data:
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")
#launch a Tornado server with HTTPServer.
if __name__ == "__main__":
port = 5000
http_server = HTTPServer(WSGIContainer(app))
logging.debug("Started Server, Kindly visit http://localhost:" + str(port))
http_server.listen(port)
IOLoop.instance().start()