Get soundtrack musical features
Implemented get_features() to retrieve musical features for soundtracks
This commit is contained in:
@@ -116,4 +116,33 @@ def user_data(request):
|
|||||||
'user_name': user_data_response['display_name'],
|
'user_name': user_data_response['display_name'],
|
||||||
'id': user_data_response['id'],
|
'id': user_data_response['id'],
|
||||||
}
|
}
|
||||||
return render(request, 'spotifyvis/user_data.html', context)
|
return render(request, 'spotifyvis/user_data.html', context)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def get_features(track_id, token):
|
||||||
|
"""Returns the features of a soundtrack
|
||||||
|
|
||||||
|
Args:
|
||||||
|
track_id: the id of the soundtrack, needed to query the Spotify API
|
||||||
|
token: an access token for the Spotify API
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
A dictionary with the features as its keys
|
||||||
|
"""
|
||||||
|
|
||||||
|
headers = {
|
||||||
|
'Authorization': token,
|
||||||
|
}
|
||||||
|
response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
|
||||||
|
features_dict = {}
|
||||||
|
|
||||||
|
# Data that we don't need
|
||||||
|
useless_keys = [
|
||||||
|
"key", "mode", "type", "liveness", "id", "uri", "track_href", "analysis_url", "time_signature",
|
||||||
|
]
|
||||||
|
for key, val in response.items():
|
||||||
|
if key not in useless_keys:
|
||||||
|
features_dict[key] = val
|
||||||
|
|
||||||
|
return features_dict
|
||||||
Reference in New Issue
Block a user