Merge pull request #4370 from arsaboo/rate_limit

Add Spotify 429 (too many requests) API error handling
This commit is contained in:
Adrian Sampson 2022-06-19 18:46:25 -04:00 committed by GitHub
commit b1ceee6233
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 11 deletions

View file

@ -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

View file

@ -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