-
Notifications
You must be signed in to change notification settings - Fork 0
/
getsongdetails.py
94 lines (63 loc) · 1.96 KB
/
getsongdetails.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
import json
import requests
import creds
import numpy as np
"""
Wrapper class to access the API and get Song Details based on ID
"""
class SongData:
def __init__(self,CLIENT_ID,CLIENT_SECRET):
self._CLIENT_ID = CLIENT_ID
self._CLIENT_SECRET = CLIENT_SECRET
"""
Get API URL
"""
@staticmethod
def getAPIUrl(song_id):
return 'https://api.spotify.com/v1/audio-features/'+song_id
def getJSONDetails(self,song_id):
"""
Set up Authentication
"""
AUTH_URL = 'https://accounts.spotify.com/api/token'
auth_response = requests.post(AUTH_URL, {
'grant_type': 'client_credentials',
'client_id': self._CLIENT_ID,
'client_secret': self._CLIENT_SECRET,
})
auth_response_data = auth_response.json()
self._ACCESS_TOKEN = auth_response_data['access_token']
"""
Request Bearer Token
"""
headers = {
'Authorization': 'Bearer {token}'.format(token=self._ACCESS_TOKEN)
}
"""
Get Song Details by calling the API
"""
url = self.getAPIUrl(song_id)
return requests.get(url,headers=headers).json()
def getSongFeatures(self,song_id):
"""
Get All Features as JSON
"""
json_dict = self.getJSONDetails(song_id)
"""
Extract Relevant Features
"""
features_list = [
json_dict["danceability"],
json_dict["energy"],
json_dict["instrumentalness"],
json_dict["key"],
json_dict["liveness"],
json_dict["speechiness"],
json_dict["tempo"],
json_dict["valence"]
]
features_list = [float(x) for x in features_list]
"""
Reshape the data such that it can be fed into the model
"""
return np.array(features_list).reshape(1,-1)