-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
151 lines (113 loc) · 4.3 KB
/
application.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from flask import Flask
from flask import render_template
from flask import jsonify
from flask_bootstrap import Bootstrap
from filters import date_time_format
from filters import file_type
from flask import request
from libs.db import get_conn
from libs.parse_audio import parse_bytes
from libs.find_match import find_matches
application = app = Flask(__name__)
Bootstrap(app)
app.secret_key = 'secret'
app.jinja_env.filters['date_time_format'] = date_time_format
app.jinja_env.filters['file_type'] = file_type
@app.route('/', methods=['GET'])
def index():
"""Generates index page of website.
Generates output from a index.html template file based on the Jinja2 engine.
Returns:
render_template: Renders the template file.
"""
conn, cur = get_conn('remote')
print("checking data base")
check = cur.execute("SELECT COUNT(id) FROM songs")
rows = ()
if check > 0:
rows = cur.fetchall()
print(rows)
print("setting params")
params = {
"songs_count": rows[0][0]
}
return render_template('index.html', params=params)
@app.route('/songs', methods=['GET'])
def browse_songs():
"""Generates songs list page of website.
Fetches songs title from database and generates output from a songs.html template file based on the Jinja2 engine.
Returns:
render_template: Renders the template file. If some error occurred while fetching, then a json object is
returned.
"""
conn, cur = get_conn('remote')
check = cur.execute("SELECT title, artist, album FROM songs")
if check > 0:
rows = cur.fetchall()
return render_template('songs.html', songs=rows)
return jsonify({"message": "Songs page"})
@app.route('/albums', methods=['GET'])
def browse_album():
"""Generates albums list page of website.
Fetches albums from database and generates output from a albums.html template file based on the Jinja2
engine.
Returns:
render_template: Renders the template file. If some error occurred while fetching, then a json object is
returned.
"""
conn, cur = get_conn('remote')
check = cur.execute("SELECT DISTINCT ALBUM FROM songs")
if check > 0:
rows = cur.fetchall()
print(rows)
return render_template('albums.html', albums=rows)
return jsonify({"message": "Albums page"})
@app.route('/artists', methods=['GET'])
def browse_artist():
# TODO: Prepare a template file for artists page and fetch artists from database and render it.
return jsonify({"message": "Artists page"})
@app.route('/identify_song', methods=['POST'])
def identify_song():
"""Identifies the song.
Processes the file sent in the request to identify the title, album and artist of song with certain confidence.
Returns:
json: Containing song title, album, artist and confidence.
"""
file = request.files['file']
mime_type = "wave"
if file.mimetype == "audio/mpeg":
mime_type = "mp3"
elif file.mimetype == "audio/wav":
mime_type = "wave"
song = parse_bytes(file.stream.read(), format=mime_type)
matches = []
for channeln, channel in enumerate(song['channels']):
matches.extend(find_matches(channel, sampling_rate=song['frame_rate']))
identified_songs = {}
song_details = {}
if len(matches) > 0:
for item in matches:
title = str(item[0])
if title in identified_songs.keys():
identified_songs[title] += 1
else:
identified_songs[title] = 1
song_id = max(identified_songs, key=identified_songs.get)
conn, cur = get_conn('remote')
cur.execute("SELECT TITLE, ARTIST, ALBUM FROM songs WHERE ID=%s", song_id)
song_details = cur.fetchall()
prob = (identified_songs[song_id] / len(matches)) * 100
print("\n\nTotal Identified songs = {}".format(len(identified_songs)))
if file is not None:
if len(matches) == 0:
return jsonify({"message": "No Match Found"})
else:
return jsonify({
"title": song_details[0][0],
"artist": song_details[0][1],
"album": song_details[0][2],
"confidence": prob
})
return jsonify({"message": "error"})
if __name__ == '__main__':
app.run(debug=True)