Browse Source

Fix program crash when AudioFeatures is missing

Fixes #32. AudioFeatures object query is now wrapped in a try/except
block to account for the scenario where the object doesn't exist.
master
Chris Shyi 7 years ago
parent
commit
0714586e07
  1. 2
      spotifyvis/utils.py
  2. 7
      spotifyvis/views.py

2
spotifyvis/utils.py

@ -127,7 +127,7 @@ def save_audio_features(headers, track_id, track):
response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
if 'error' in response:
return {}
return
# Data that we don't need
useless_keys = [

7
spotifyvis/views.py

@ -204,8 +204,11 @@ def get_audio_feature_data(request, audio_feature, client_secret):
'data_points': [],
}
for track in user_tracks:
audio_feature_obj = AudioFeatures.objects.get(track=track)
response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature))
try:
audio_feature_obj = AudioFeatures.objects.get(track=track)
response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature))
except AudioFeatures.DoesNotExist:
continue
return JsonResponse(response_payload)
# }}} get_audio_feature_data #

Loading…
Cancel
Save