diff --git a/spotdl/utils/spotify.py b/spotdl/utils/spotify.py index f89aaf78c..f66ce5802 100644 --- a/spotdl/utils/spotify.py +++ b/spotdl/utils/spotify.py @@ -11,6 +11,7 @@ from json import dumps from typing import Dict, Optional +import requests from spotipy import Spotify from spotipy.cache_handler import CacheFileHandler, MemoryCacheHandler from spotipy.oauth2 import SpotifyClientCredentials, SpotifyOAuth @@ -141,7 +142,7 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self._initialized = True - def _get(self, url, args=None, payload=None, **kwargs): + def _get(self, url, args=None, payload=None, retries=3, **kwargs): """ Overrides the get method of the SpotifyClient. Allows us to cache requests @@ -161,7 +162,15 @@ def _get(self, url, args=None, payload=None, **kwargs): if self.cache.get(cache_key) is not None: return self.cache[cache_key] - response = self._internal_call("GET", url, payload, kwargs) + # Wrap in a try-except and retry up to `retries` times. + response = None + while response is None: + try: + response = self._internal_call("GET", url, payload, kwargs) + except requests.exceptions.Timeout as exc: + retries -= 1 + if retries <= 0: + raise exc if use_cache and cache_key is not None: self.cache[cache_key] = response