Browse Source

Fix audio feature analysis missing bug

Added error checking for missing audio features in the Spotify database.
master
Chris Shyi 7 years ago
parent
commit
003c630dd4
  1. 12
      spotifyvis/utils.py

12
spotifyvis/utils.py

@ -27,12 +27,13 @@ def parse_library(headers, tracks, library_stats):
for track_dict in saved_tracks_response['items']: for track_dict in saved_tracks_response['items']:
# Track the number of samples for calculating # Track the number of samples for calculating
# audio feature averages and standard deviations on the fly # audio feature averages and standard deviations on the fly
num_samples += 1
get_track_info(track_dict['track'], library_stats, num_samples) get_track_info(track_dict['track'], library_stats, num_samples)
# get_genre(headers, track_dict['track']['album']['id']) # get_genre(headers, track_dict['track']['album']['id'])
audio_features_dict = get_audio_features(headers, track_dict['track']['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']: for artist_dict in track_dict['track']['artists']:
increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats) increase_artist_count(headers, artist_dict['name'], artist_dict['id'], library_stats)
# calculates num_songs with offset + songs retrieved # 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 track_id: the id of the soundtrack, needed to query the Spotify API
Returns: 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() response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
if 'error' in response:
return {}
features_dict = {} features_dict = {}
# Data that we don't need # Data that we don't need

Loading…
Cancel
Save