From f7ae3285018b8cdf6733e059c3f9cb3656195b16 Mon Sep 17 00:00:00 2001 From: badfarmerjohn <31398895+badfarmerjohn@users.noreply.github.com> Date: Fri, 5 Mar 2021 01:36:07 -0800 Subject: [PATCH] Fixed audio_features API wrapper function return structure (#653) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Stéphane Bruckert --- CHANGELOG.md | 10 +++++++--- spotipy/client.py | 7 +------ tests/integration/test_non_user_endpoints.py | 10 +++++----- 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 588d60c8..2c2e31d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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] diff --git a/spotipy/client.py b/spotipy/client.py index 95e2aa78..8eafb574 100644 --- a/spotipy/client.py +++ b/spotipy/client.py @@ -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. diff --git a/tests/integration/test_non_user_endpoints.py b/tests/integration/test_non_user_endpoints.py index abfafc3d..64868f93 100644 --- a/tests/integration/test_non_user_endpoints.py +++ b/tests/integration/test_non_user_endpoints.py @@ -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(