diff --git a/beetsplug/spotify.py b/beetsplug/spotify.py index d7062f7d4..4980d9c1b 100644 --- a/beetsplug/spotify.py +++ b/beetsplug/spotify.py @@ -17,20 +17,21 @@ Spotify playlist construction. """ -import re -import json import base64 +import collections +import json +import re import time import webbrowser -import collections -import unidecode -import requests import confuse - +import requests +import unidecode from beets import ui from beets.autotag.hooks import AlbumInfo, TrackInfo -from beets.plugins import MetadataSourcePlugin, BeetsPlugin +from beets.plugins import BeetsPlugin, MetadataSourcePlugin + +DEFAULT_WAITING_TIME = 5 class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): @@ -164,6 +165,13 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): ) self._authenticate() return self._handle_response(request_type, url, params=params) + elif response.status_code == 429: + seconds = response.headers.get('Retry-After', + DEFAULT_WAITING_TIME) + self._log.debug('Too many API requests. Retrying after {} \ + seconds.', seconds) + time.sleep(int(seconds) + 1) + return self._handle_response(request_type, url, params=params) else: raise ui.UserError( '{} API error:\n{}\nURL:\n{}\nparams:\n{}'.format( @@ -577,9 +585,6 @@ class SpotifyPlugin(MetadataSourcePlugin, BeetsPlugin): self._log.debug('Total {} tracks', len(items)) for index, item in enumerate(items, start=1): - # Added sleep to avoid API rate limit - # https://developer.spotify.com/documentation/web-api/guides/rate-limits/ - time.sleep(.5) self._log.info('Processing {}/{} tracks - {} ', index, len(items), item) # If we're not forcing re-downloading for all tracks, check diff --git a/docs/changelog.rst b/docs/changelog.rst index c95287443..dbee12764 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -32,7 +32,9 @@ New features: Bug fixes: -* Fix implicit paths OR queries (e.g. ``beet list /path/ , /other-path/``) +* 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/``) which have previously been returning the entire library. :bug:`1865` * The Discogs release ID is now populated correctly to the discogs_albumid