/
functions.py
296 lines (241 loc) · 10.4 KB
/
functions.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import json
import string
import random
import requests
from spotify import app
import logging
import base64
import spotipy as sp
import streamlit as st
import time
import spotipy
import spotipy.util as util
import json
import requests
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import requests
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import MinMaxScaler,LabelEncoder,OneHotEncoder
from sklearn.feature_selection import SelectKBest,f_classif
from sklearn.tree import DecisionTreeClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
# from xgboost import XGBClassifier
import time
from collections import Counter
from imblearn.over_sampling import SMOTE
import sys
def get_playlist_tracks(username, playlist_id, token):
sp = spotipy.Spotify(auth=token)
results = sp.user_playlist_tracks(username,playlist_id)
tracks = results['items']
while results['next']:
results = sp.next(results)
tracks.extend(results['items'])
return tracks
def get_features(track_id: str, token: str) -> dict:
sp = spotipy.Spotify(auth=token)
try:
features = sp.audio_features([track_id])
return features
except:
return None
def return_playlist(token, playlist_id, username):
tracks = get_playlist_tracks(username, playlist_id, token)
l = []
for i in tracks:
l.append(i['track']['uri'].split(':')[-1])
string_list = []
for i in range(len(tracks)//100 + 1):
string_list.append(','.join(l[i*100:(i+1)*100]))
json_list = []
for s in string_list:
headers = {'Accept': 'application/json','Content-Type': 'application/json','Authorization': f'Bearer ' + token}
params = [('ids', s)]
json = ''
try:
response = requests.get('https://api.spotify.com/v1/audio-features',
headers = headers, params = params, timeout = 5)
json = response.json()
json_list.append(json)
except:
print('None')
dataframe_list = []
for json in json_list:
try:
dataframe_list.append(pd.DataFrame(json['audio_features']))
except:
st.write('🚨🚨 Local files detected. Upload tracks from Spotify and press R to rerun.')
sys.exit()
df = pd.concat(dataframe_list)
return df
def clean_data(a_data,b_data,class2,class1='Saumya Mundra'):
a_data['class'] = class1
b_data['class'] = class2
feature_list = [ 'acousticness',
'danceability', 'duration_ms', 'energy', 'instrumentalness', 'key',
'liveness', 'loudness', 'mode', 'speechiness', 'tempo',
'time_signature', 'valence','class']
a_features = a_data[feature_list]
b_features = b_data[feature_list]
df = pd.concat([a_features,b_features])
X = df.iloc[:,:-1]
y = df.iloc[:,-1]
#print(Counter(y))
smote = SMOTE()
X,y = smote.fit_resample(X,y)
#print(Counter(y))
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=1)
scaler = MinMaxScaler(feature_range=(0,1))
scaler.fit(X_train[['tempo']])
X_train['tempo'] = scaler.transform(X_train[['tempo']])
X_test['tempo'] = scaler.transform(X_test[['tempo']])
return X_train,X_test,y_train,y_test
def train_model(X_train,X_test,y_train,y_test,k=13):
model = RandomForestClassifier(random_state=1)
# model = XGBClassifier()
fs = SelectKBest(score_func=f_classif,k=k)
X_train_final = pd.DataFrame(fs.fit_transform(X_train,y_train),columns = X_test.columns[fs.get_support(indices=True)])
X_test_final = X_test[X_test.columns[fs.get_support(indices=True)]]
model.fit(X_train_final,y_train)
return model
def make_prediction(song_id,feature_list,model, token):
song = pd.DataFrame(get_features(track_id=song_id,token=token))[feature_list]
final_prediction = model.predict(song)
if final_prediction:
return final_prediction
def friends_comparison(token, username, playlist_id1,playlist_id2,song_id='2yLa0QULdQr0qAIvVwN6B5',name1=0,name2=1):
streamlit1 = return_playlist(token, playlist_id1, username)
streamlit2 = return_playlist(token, playlist_id2, username)
a_data = streamlit1
b_data = streamlit2
a_data = a_data[:-1]
b_data = b_data[:-1]
X_train,X_test,y_train,y_test = clean_data(a_data,b_data,name2,name1)
feature_list = X_train.columns
model = train_model(X_train,X_test,y_train,y_test)
#song_id = '0ofHAoxe9vBkTCp2UQIavz'
prediction = make_prediction(song_id,feature_list,model, token)
return prediction
def createStateKey(n):
return ''.join(random.choices(string.ascii_uppercase + string.digits, k=n))
def getToken(code):
token_url = 'https://accounts.spotify.com/api/token'
# authorization = app.config['AUTHORIZATION']
redirect_uri = app.config['REDIRECT_URI']
auth_str = f"{app.config['CLIENT_ID']}:{ app.config['CLIENT_SECRET']}"
b64_auth_str = base64.b64encode(auth_str.encode()).decode()
headers = {'Authorization': f'Basic {b64_auth_str}', 'Accept': 'application/json', 'Content-Type': 'application/x-www-form-urlencoded'}
body_params = {'code': code, 'redirect_uri': redirect_uri, 'grant_type': 'authorization_code'}
post_response = requests.post(token_url, headers=headers, data=body_params)
if post_response.status_code == 200:
json = post_response.json()
return json['access_token'], json['refresh_token'], json['expires_in']
else:
logging.error('getToken:' + str(post_response.status_code))
return post_response
def getUserInformation(token):
api_url = ' https://api.spotify.com/v1/me'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
post_response = requests.get(api_url, headers=headers, params={})
if post_response.status_code == 200:
return post_response.json()['id']
else:
return post_response.json()
def getPlaylist(token):
url = 'https://api.spotify.com/v1/playlists/0Snll1WiPhWXFwcW1ydcKE'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
params = {'playlist_id' : '0Snll1WiPhWXFwcW1ydcKE'}
post_response = requests.get(url=url, headers=headers, params={})
if post_response.status_code == 200:
return post_response.json()
else:
return post_response.json()
def getPlaylistsLinks(token):
"""
Description : Returns playlist ids for the current user
Args:
token (iterable): the access token used to access private scopes through API call
Returns:
list containing playlist ids
"""
url = 'https://api.spotify.com/v1/me/playlists'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json ', 'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
response = response.json()
ids = []
for item in response['items']:
ids.append(item['id'])
return ids
else:
return 'Access Token Expired. Try to log in again.'
def getPlaylistsNames(token):
"""
Description : Returns playlist ids for the current user
Args:
token (iterable): the access token used to access private scopes through API call
Returns:
list containing playlist ids
"""
url = 'https://api.spotify.com/v1/me/playlists'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json ', 'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
if response.status_code == 200:
response = response.json()
ids = []
for item in response['items']:
ids.append(item['name'])
return ids
else:
return 'Access Token Expired. Try to log in again.'
def getTracks(token, playlist_id):
url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
response = requests.get(url, headers=headers)
response = response.json()
print(response)
tracks = []
for item in response['items']:
tracks.append(item['track']['uri'])
return tracks
def addtracks(token, playlist_id, tracks=["spotify:track:4iV5W9uYEdYUVa79Axb7Rh","spotify:track:1301WleyT98MSxVHPZCA6M"], limit=500):
url = f'https://api.spotify.com/v1/playlists/{playlist_id}/tracks?limit={limit}'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
# params = {'playlist_id' : '0Snll1WiPhWXFwcW1ydcKE'}
data = json.dumps(tracks)
post_response = requests.post(url=url, headers=headers, data=data)
return post_response.json()
def create_playlist(token, user_id, name='Generated Buddy Playlist', description='Playlist generated through Buddy Playlist Generator', public=False):
url = f'https://api.spotify.com/v1/users/{user_id}/playlists'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
data = {
"name":name,
"description":description,
"public":False
}
data = json.dumps(data)
response = requests.post(url, headers=headers, data=data)
print(response.json())
return response.json()['id']
def search(token, search):
#search = input()
url = 'https://api.spotify.com/v1/search'
headers = {'Authorization': 'Bearer ' + token, 'Accept': 'application/json', 'Content-Type': 'application/json'}
params = {
'q':search,
'type':'track'
}
#data = json.dumps(data)
response = requests.get(url, headers=headers, params=params)
return response.json()['tracks']['items']
# token = 'BQCjcGF3MczgM8miittdVWCUO6ppR232fmuKrGuYjoaJnIDiNght0mPQUp-ggRXAhqhdpl4v-8L6yohjvj2V6kW80rtk2smRps56VRJd0G1ls41u8edNUB8Up4Ww9g9aIIsTdT-7OLG8y16QywKW3BBpi9NJxP_4TjkW38pXBfNFJIekIZV_VjUyvgpS82qKeiRKAy5ZmDJUyFT3vJCuQUsqy_HFzRlgadKdfSKtbA'
# user_id = getUserInformation(token)
# playlist_id = create_playlist(token, user_id)
# tracks = getTracks(token, '2V4o0o9VLNf2Tl4e4Bnu1D')
# print(tracks)
# print(addtracks(token, playlist_id, tracks))