-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
151 lines (117 loc) · 4.93 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
from flask import Flask
from flask import request
from flask import render_template
import pandas as pd
import numpy as np
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import time
# importing functions
from getTrackFeatures import getTrackFeatures
from getRecommendations import getRecommendation
from filterRecommendations import getNeighbors
app = Flask(__name__)
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
global cid, secret, client_credentials_manager, sp, new_df
cid = '7f2ab65404b64bbc99e22b2efcb17983'
secret = 'aa855c61f03f4077ac50a78fa35c71c6'
client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=secret)
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
new_df = pd.read_csv('data.csv', encoding='latin-1')
# POST the name of the song
@app.route("/rec/collab", methods=["POST"])
def rec():
starttime = time.time()
song = request.form.get("song")
track_id = sp.search(q='track:' + song, type='track')
id = track_id['tracks']['items'][0]['id']
song_name = sp.track(id)['name']
track1, track2 = getTrackFeatures(id, sp)
#CONTENT BASED ------------------------------------------------------------------
# #COLLABORATIVE FILTERING BASED ------------------------------------------------------------------
tracks = []
track2.insert(0, id)
tracks.append(track2)
record = pd.DataFrame(tracks, columns = ['id', 'popularity', 'danceability', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'year'])
data = new_df[['id', 'popularity', 'danceability', 'acousticness', 'danceability', 'energy', 'instrumentalness', 'liveness', 'loudness', 'speechiness', 'tempo', 'year']]
k=10
nid = getNeighbors(record, data,k )
nid.insert(0,id)
neighbours = []
prev = ""
# print("Recommendations based on", col[i])
for j in nid:
temp = []
track = sp.track(j)
time.sleep(0.2)
if(track['name']==prev):
continue
minutes = str(int((track['duration_ms'] / 1000) / 60))
seconds = str(int((track['duration_ms']/ 1000) % 60))
if(len(minutes)==1):
minutes= '0'+minutes
if(len(seconds)==1):
seconds= '0'+seconds
duration = minutes+":"+seconds
prev = track['name']
temp.append(str(track['album']['images'][0]['url']))
temp.append(track['name'])
temp.append(duration)
temp.append(track['album']['artists'][0]['name'])
temp.append(str(track['preview_url']))
neighbours.append(temp)
return render_template("index.html", recommendations=neighbours)
@app.route("/rec/content", methods=["POST"])
def content():
starttime = time.time()
song = request.form.get("song")
track_id = sp.search(q='track:' + song, type='track')
id = track_id['tracks']['items'][0]['id']
song_name = sp.track(id)['name']
track1, track2 = getTrackFeatures(id, sp)
tracks = []
print(time.time() - starttime)
track1.insert(0, id)
tracks.append(track1)
# create record
record = pd.DataFrame(tracks, columns = ['id', 'name', 'album', 'artist', 'release_date'])
tid = getRecommendation(new_df, record)
print(time.time() - starttime)
recommendations = [] #4 list (categories)
prev = song_name
col = ['name', 'album', 'artist', 'release_date']
for i in range(4):
rec_col = []
# print("Recommendations based on", col[i])
for j in tid[i]:
temp = []
track = sp.track(j)
time.sleep(0.2)
if(track['name']==prev):
continue
minutes = str(int((track['duration_ms'] / 1000) / 60))
seconds = str(int((track['duration_ms']/ 1000) % 60))
if(len(minutes)==1):
minutes= '0'+minutes
if(len(seconds)==1):
seconds= '0'+seconds
duration = minutes+":"+seconds
prev = track['name']
temp.append(track['name'])
temp.append(duration)
temp.append(track['album']['artists'][0]['name'])
temp.append(str(track['preview_url']))
temp.append(str(track['album']['images'][0]['url']))
rec_col.append(temp)
recommendations.append(rec_col)
print(time.time() - starttime)
return render_template("index.html", recommend = recommendations , col=col)
# MAIN ROUTE ------------------------------------------------------------------------------------------------
@app.route("/", methods=['GET'])
def index():
return render_template("index.html", msg="NRDY")
if __name__ == "__main__":
app.run(debug=True)