Adapted to new audio_features response

This commit is contained in:
Paul Lamere 2016-02-17 09:18:06 -05:00
parent 38ea4b6910
commit 9c0b872e86
2 changed files with 25 additions and 1 deletions

View File

@ -633,7 +633,13 @@ class Spotify(object):
- tracks - a list of track URIs, URLs or IDs, maximum: 50 ids
'''
tlist = [self._get_id('track', t) for t in tracks]
return self._get('audio-features?ids=' + ','.join(tlist))
results = self._get('audio-features?ids=' + ','.join(tlist))
# the response has changed, look for the new style first, and if
# its not there, fallback on the old style
if 'audio_attributes' in results:
return results['audio_attributes']
else:
return results
def _get_id(self, type, id):
fields = id.split(':')

View File

@ -173,6 +173,24 @@ class AuthTestSpotipy(unittest.TestCase):
self.assertTrue(playlist['tracks']['total'] == 3)
self.assertTrue(len(playlist['tracks']['items']) == 3)
def test_audio_features(self):
results = spotify.audio_features(self.four_tracks)
self.assertTrue(len(results) == len(self.four_tracks))
for attr in results:
assert('speechiness' in attr)
def test_audio_features_with_bad_track(self):
bad_tracks = ['spotify:track:bad']
input = self.four_tracks + bad_tracks
results = spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
none_count = 0
for attr in results:
if attr == None:
none_count +=1
else:
assert('speechiness' in attr)
self.assertTrue(none_count == len(bad_tracks))
if __name__ == '__main__':
if len(sys.argv) > 1: