From 276c55105944844e62c81d3395519e6e7f8d8ee4 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Sun, 26 Jun 2022 11:46:42 -0400 Subject: [PATCH 01/15] Update spotify.py --- beetsplug/spotify.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 42e20c828..e81c1b8aa 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -189,6 +189,9 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): seconds.', seconds) time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) + elif 'analysis not found' in response.text: + self._log.debug('No analysis found') + return None else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( @@ -621,6 +624,9 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): item['spotify_track_popularity'] = popularity audio_features = \ self.track_audio_features(spotify_track_id) + if audio_features is None: + self._log.debug('No audio features found for : {}', item) + continue for feature in audio_features.keys(): if feature in self.spotify_audio_features.keys(): item[self.spotify_audio_features[feature]] = \ From 2490737f617195ac2c8a0dca940b653e2c129254 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Sun, 26 Jun 2022 11:54:00 -0400 Subject: [PATCH 02/15] Update changelog.rst --- docs/changelog.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/changelog.rst b/docs/changelog.rst index dbee12764..8cb27013d 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -32,6 +32,8 @@ New features: Bug fixes: +* Fix Spotify API error when tracks do not have audio-features. + :bug:`4385` * We now respect the Spotify API's rate limiting, which avoids crashing when the API reports code 429 (too many requests). :bug:`4370` * Fix implicit paths OR queries (e.g. ``beet list /path/ , /other-path/``) From 086de854d1d03edc1793f1ce9eb5ff129a80c57d Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Sun, 26 Jun 2022 11:56:13 -0400 Subject: [PATCH 03/15] Update spotify.py --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index e81c1b8aa..0de08ff3d 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -190,7 +190,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) elif 'analysis not found' in response.text: - self._log.debug('No analysis found') + self._log.debug('No audio analysis found') return None else: raise ui.UserError( From 9bde98c4405ef4486d1115b46f93e6709677645a Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Wed, 29 Jun 2022 19:47:21 -0400 Subject: [PATCH 04/15] Update beetsplug/spotify.py Co-authored-by: Adrian Sampson --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 0de08ff3d..979cc8975 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -625,7 +625,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): audio_features = \ self.track_audio_features(spotify_track_id) if audio_features is None: - self._log.debug('No audio features found for : {}', item) + self._log.debug('No audio features found for: {}', item) continue for feature in audio_features.keys(): if feature in self.spotify_audio_features.keys(): From 54af411b62054b9e045d597bf54e354a097f49fc Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Wed, 29 Jun 2022 19:54:12 -0400 Subject: [PATCH 05/15] Address comments --- beetsplug/spotify.py | 14 ++++++++------ docs/changelog.rst | 2 -- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 979cc8975..666a19c08 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -189,9 +189,6 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): seconds.', seconds) time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) - elif 'analysis not found' in response.text: - self._log.debug('No audio analysis found') - return None else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( @@ -645,7 +642,12 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): def track_audio_features(self, track_id=None): """Fetch track audio features by its Spotify ID.""" - track_data = self._handle_response( - requests.get, self.audio_features_url + track_id - ) + try: + track_data = self._handle_response( + requests.get, self.audio_features_url + \ + track_id) + track_data.raise_for_status() + except requests.exceptions.RequestException as e: + self._log.debug('Audio feature update failed: {0}', str(e)) + track_data = None return track_data diff --git a/docs/changelog.rst b/docs/changelog.rst index 8cb27013d..dbee12764 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -32,8 +32,6 @@ New features: Bug fixes: -* Fix Spotify API error when tracks do not have audio-features. - :bug:`4385` * We now respect the Spotify API's rate limiting, which avoids crashing when the API reports code 429 (too many requests). :bug:`4370` * Fix implicit paths OR queries (e.g. ``beet list /path/ , /other-path/``) From decdb16a1537f5a88acba359035fa6c71ea26d9e Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Wed, 29 Jun 2022 20:13:48 -0400 Subject: [PATCH 06/15] lint --- beetsplug/spotify.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 666a19c08..f610cd956 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -644,8 +644,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): """Fetch track audio features by its Spotify ID.""" try: track_data = self._handle_response( - requests.get, self.audio_features_url + \ - track_id) + requests.get, self.audio_features_url + track_id) track_data.raise_for_status() except requests.exceptions.RequestException as e: self._log.debug('Audio feature update failed: {0}', str(e)) From 6a131d210837ed59f4d54f45275ad83783866e1b Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Fri, 1 Jul 2022 10:26:08 -0400 Subject: [PATCH 07/15] Address comments --- beetsplug/spotify.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index f610cd956..2e4d9b402 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -622,7 +622,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): audio_features = \ self.track_audio_features(spotify_track_id) if audio_features is None: - self._log.debug('No audio features found for: {}', item) + self._log.info('No audio features found for: {}', item) continue for feature in audio_features.keys(): if feature in self.spotify_audio_features.keys(): @@ -645,8 +645,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): try: track_data = self._handle_response( requests.get, self.audio_features_url + track_id) - track_data.raise_for_status() - except requests.exceptions.RequestException as e: + except AttributeError: self._log.debug('Audio feature update failed: {0}', str(e)) track_data = None return track_data From 016526f30e41dd00a9b33997fac1f96f59e128bb Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Fri, 1 Jul 2022 10:29:20 -0400 Subject: [PATCH 08/15] Lint --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 2e4d9b402..50cf02951 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -646,6 +646,6 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): track_data = self._handle_response( requests.get, self.audio_features_url + track_id) except AttributeError: - self._log.debug('Audio feature update failed: {0}', str(e)) + self._log.debug('Audio feature update failed: {}', str(track_id)) track_data = None return track_data From 1d241b0d52681aa66260f700508144afea1a4659 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Mon, 4 Jul 2022 14:12:22 -0400 Subject: [PATCH 09/15] Update error handling --- beetsplug/spotify.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 50cf02951..6cad6986d 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -34,6 +34,8 @@ from beets.plugins import BeetsPlugin, MetadataSourcePlugin DEFAULT_WAITING_TIME = 5 +class SpotifyAPIError(Exception): + pass class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): data_source = 'Spotify' @@ -189,6 +191,8 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): seconds.', seconds) time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) + elif 'analysis not found' in response.text: + raise SpotifyAPIError("Audio Analysis not found") else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( @@ -645,7 +649,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): try: track_data = self._handle_response( requests.get, self.audio_features_url + track_id) - except AttributeError: - self._log.debug('Audio feature update failed: {}', str(track_id)) + except SpotifyAPIError as e: + self._log.debug('Spotify API error: {}', e) track_data = None return track_data From 85e124bcd0e3e3b35a314564693a7d0f0ad516e3 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Mon, 4 Jul 2022 14:15:08 -0400 Subject: [PATCH 10/15] lint errors --- beetsplug/spotify.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 6cad6986d..f4d43afa4 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -34,9 +34,11 @@ from beets.plugins import BeetsPlugin, MetadataSourcePlugin DEFAULT_WAITING_TIME = 5 + class SpotifyAPIError(Exception): pass + class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): data_source = 'Spotify' From 95243019e98204b3f45922417deb125679cf05aa Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Mon, 4 Jul 2022 14:29:27 -0400 Subject: [PATCH 11/15] Update exception --- beetsplug/spotify.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index f4d43afa4..ee5a6463c 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -194,7 +194,8 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) elif 'analysis not found' in response.text: - raise SpotifyAPIError("Audio Analysis not found") + raise SpotifyAPIError("API Error {0.status_code} for {}" + .format(response, url)) else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( From 12d9b1bd22e1c930df443cc2f58f0bc66af8621d Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Mon, 4 Jul 2022 14:31:28 -0400 Subject: [PATCH 12/15] Update exception --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index ee5a6463c..fb3228baa 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -194,7 +194,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) elif 'analysis not found' in response.text: - raise SpotifyAPIError("API Error {0.status_code} for {}" + raise SpotifyAPIError("API Error {0.status_code} for {1}" .format(response, url)) else: raise ui.UserError( From ca4b5bcec418854abd01ccf3db69a94c542021fa Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Mon, 4 Jul 2022 14:34:32 -0400 Subject: [PATCH 13/15] lint --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index fb3228baa..ee86f2714 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -195,7 +195,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): return self._handle_response(request_type, url, params=params) elif 'analysis not found' in response.text: raise SpotifyAPIError("API Error {0.status_code} for {1}" - .format(response, url)) + .format(response, url)) else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( From c7f465f968082f447ed6146c6a0c52733dc109a4 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Tue, 5 Jul 2022 20:46:14 -0400 Subject: [PATCH 14/15] Address comments --- beetsplug/spotify.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index ee86f2714..75ae52891 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -193,7 +193,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): seconds.', seconds) time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) - elif 'analysis not found' in response.text: + elif response.status_code == 400: raise SpotifyAPIError("API Error {0.status_code} for {1}" .format(response, url)) else: @@ -650,9 +650,8 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): def track_audio_features(self, track_id=None): """Fetch track audio features by its Spotify ID.""" try: - track_data = self._handle_response( + return self._handle_response( requests.get, self.audio_features_url + track_id) except SpotifyAPIError as e: self._log.debug('Spotify API error: {}', e) - track_data = None - return track_data + return None From a4baa742d57c5214d62eae7fb0e742051e7a5b96 Mon Sep 17 00:00:00 2001 From: Alok Saboo Date: Thu, 7 Jul 2022 14:14:11 -0400 Subject: [PATCH 15/15] Update error code --- beetsplug/spotify.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index 75ae52891..a9c8368b0 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -193,7 +193,7 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): seconds.', seconds) time.sleep(int(seconds) + 1) return self._handle_response(request_type, url, params=params) - elif response.status_code == 400: + elif response.status_code == 404: raise SpotifyAPIError("API Error {0.status_code} for {1}" .format(response, url)) else: