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
committed by Kevin Mok
parent
commit
e614b373df
  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() response = requests.get("https://api.spotify.com/v1/audio-features/{}".format(track_id), headers = headers).json()
if 'error' in response: if 'error' in response:
return {}
return
# Data that we don't need # Data that we don't need
useless_keys = [ useless_keys = [

7
spotifyvis/views.py

@ -203,8 +203,11 @@ def get_audio_feature_data(request, audio_feature, client_secret):
'data_points': [], 'data_points': [],
} }
for track in user_tracks: 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) return JsonResponse(response_payload)
# }}} get_audio_feature_data # # }}} get_audio_feature_data #

Loading…
Cancel
Save