Skip to content

Commit

Permalink
Fixed audio_features API wrapper function return structure (#653)
Browse files Browse the repository at this point in the history
Co-authored-by: Stéphane Bruckert <[email protected]>
  • Loading branch information
badfarmerjohn and stephanebruckert authored Mar 5, 2021
1 parent f3d1192 commit f7ae328
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
10 changes: 7 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## Unreleased [3.0.0-alpha]

While this is unreleased, please only add v3 features here.
Rebasing master onto v3 doesn't require a changelog update.

### Added

- ...
### Changed
Modified the return structure of the `audio_features` function (wrapping the [Get Audio Features for Several Tracks](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-several-audio-features) API) to conform to the return structure of similar APIs, such as:
- [Get Several Tracks](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-several-tracks)
- [Get Multiple Artists](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-multiple-artists)
- [Get Multiple Albums](https://developer.spotify.com/documentation/web-api/reference/#endpoint-get-multiple-albums)
The functions wrapping these APIs do not unwrap the single key JSON response, and this is currently the only function that does this.

## Unreleased [2.x.x]

Expand Down
7 changes: 1 addition & 6 deletions spotipy/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1623,12 +1623,7 @@ def audio_features(self, tracks=[]):
else:
tlist = [self._get_id("track", t) for t in tracks]
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_features" in results:
return results["audio_features"]
else:
return results
return results

def devices(self):
""" Get a list of user's available devices.
Expand Down
10 changes: 5 additions & 5 deletions tests/integration/test_non_user_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ def test_audio_analysis(self):

def test_audio_features(self):
results = self.spotify.audio_features(self.four_tracks)
self.assertTrue(len(results) == len(self.four_tracks))
for track in results:
self.assertTrue(len(results['audio_features']) == len(self.four_tracks))
for track in results['audio_features']:
assert('speechiness' in track)

def test_audio_features_with_bad_track(self):
bad_tracks = ['spotify:track:bad']
input = self.four_tracks + bad_tracks
results = self.spotify.audio_features(input)
self.assertTrue(len(results) == len(input))
for track in results[:-1]:
self.assertTrue(len(results['audio_features']) == len(input))
for track in results['audio_features'][:-1]:
if track is not None:
assert('speechiness' in track)
self.assertTrue(results[-1] is None)
self.assertTrue(results['audio_features'][-1] is None)

def test_recommendations(self):
results = self.spotify.recommendations(
Expand Down

0 comments on commit f7ae328

Please sign in to comment.