From 41eca1c891f282416312b762811bb6d41502e463 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Fri, 18 May 2018 21:20:13 -0400 Subject: [PATCH] Get soundtrack musical features Implemented get_features() to retrieve musical features for soundtracks --- spotifyvis/views.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/spotifyvis/views.py b/spotifyvis/views.py index 5a4ee80..b1154f9 100644 --- a/spotifyvis/views.py +++ b/spotifyvis/views.py @@ -116,4 +116,33 @@ def user_data(request): 'user_name': user_data_response['display_name'], 'id': user_data_response['id'], } - return render(request, 'spotifyvis/user_data.html', context) \ No newline at end of file + 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 \ No newline at end of file