From 003c630dd4da143b731f4a5b596d9999ff278666 Mon Sep 17 00:00:00 2001 From: Chris Shyi Date: Sun, 3 Jun 2018 16:54:39 -0400 Subject: [PATCH] Fix audio feature analysis missing bug Added error checking for missing audio features in the Spotify database. --- spotifyvis/utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/spotifyvis/utils.py b/spotifyvis/utils.py index 279e4ed..9e537dc 100644 --- a/spotifyvis/utils.py +++ b/spotifyvis/utils.py @@ -27,12 +27,13 @@ def parse_library(headers, tracks, library_stats): for track_dict in saved_tracks_response['items']: # Track the number of samples for calculating # audio feature averages and standard deviations on the fly - num_samples += 1 get_track_info(track_dict['track'], library_stats, num_samples) # get_genre(headers, track_dict['track']['album']['id']) audio_features_dict = get_audio_features(headers, track_dict['track']['id']) - for feature, feature_data in audio_features_dict.items(): - update_audio_feature_stats(feature, feature_data, num_samples, library_stats) + if len(audio_features_dict) != 0: + num_samples += 1 + for feature, feature_data in audio_features_dict.items(): + update_audio_feature_stats(feature, feature_data, num_samples, library_stats) for artist_dict in track_dict['track']['artists']: increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats) # calculates num_songs with offset + songs retrieved @@ -51,10 +52,13 @@ def get_audio_features(headers, track_id): track_id: the id of the soundtrack, needed to query the Spotify API Returns: - A dictionary with the features as its keys + A dictionary with the features as its keys, if audio feature data is missing for the track, + an empty dictionary is returned. """ response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json() + if 'error' in response: + return {} features_dict = {} # Data that we don't need