spotipy: Spotipy doesnt doesnt respond but no error

Hello,

I have been using spotipy for the last year and never ran into any problems. Today, I realized that something is not working anymore and looked into it. I can initializing the sp.Spotify instance as usual without any problems, but if I then call a function (for example spotify.me() or spotify.devices(), it simply hangs and doesnt return anything.

This is the code I used for the last months:

with open("CONFIG.json", "r") as f:
    config = json.load(f)

client_id = config["client_id"]
client_secret = config["client_secret"]
device_name = config["device_name"]
redirect_uri = config["redirect_uri"]
username = config["username"]
scope = config["scope"]

# authenticate account
auth_manager = SpotifyOAuth(
    client_id=client_id,
    client_secret=client_secret,
    redirect_uri=redirect_uri,
    scope=scope,
    username=username
)
spotify = sp.Spotify(auth_manager=auth_manager)

I checked my Spotify dashboard, and noticed, that noticed that the number of daily requests dropped to zero just when December started: image

Do you have any idea what might cause the issue? I already tried to regenerate a new client secret, but it didnt work.

I am using the version 2.21.0 on a raspberry pi (Raspbian version 10 [Buster])

About this issue

  • Original URL
  • State: open
  • Created 2 years ago
  • Comments: 17 (4 by maintainers)

Most upvoted comments

I found the cause of the lack of response, and this is mentioned in https://github.com/urllib3/urllib3/issues/2911. In module urllib3/connectionpool.py, line #943 is code

            retries.sleep(response)

This is honoring the Retry-After that Spotify’s API is sending back. And if you’re like me, who somehow got a long retry time (mine is currently 20k+ seconds), it is going to hang.

A potential fix is simply doing the following…

sp = spotipy.Spotify(
    retries=0,
    ...
)

…so Spotify doesn’t try the retry. But if you do this, it just raises an Exception and doesn’t report back what the Retry-After value was. This is where the improvement can be made, and perhaps build Spotipy’s own retry feature.

Hi! I had a similar issue. I found that once a request gets denied, with each additional call you make following this, the wait time grows exponentially. Since I had a lot of calls to make (I’m doing a dataset augmentation task), I added an error catch for time out that looks like this:

try: 
       results = sp.albums(uri)
except SpotifyException as e: 
        if e.http_status == 429:
            print("'retry-after' value:", e.headers['retry-after'])
            retry_value = e.headers['retry-after']
            if int(e.headers['retry-after']) > 60: 
                print("STOP FOR TODAY, retry value too high {}".format(retry_value))
                exit() 
                

Normally, it makes me wait like 8 hours and then I can come back.

Also, a few tips I have for making it less likely to time out:

  • if you lower the size of the batches, it’s less likely to time out. So for example calling sp.albums(batch) where batch size is 10 worked well for me without timeouts.
  • if you add a few second sleep before each call, it helps.
  • different calls have different timeout times, so tracks seems particularly sensitive while music_features is less sensitive.

Not sure whether you setting allows for these changes, but in the case that it does - hope it helps 😃

@spotifyr1 Hey, regarding this question “So when I use if int(e.headers[‘retry-after’]) > 60 to exit the loop, it doesn’t work because there is no ‘retry-after’ key in the headers. Do you know why that could happen? I’m printing out the values returned by the exception, and they all look fine except the header which is empty {}”, I don’t think spotipy has this returned. This is one of the reasons why I didn’t use spotipy client as the api, I directly called the REST API with the requests module, hence you don’t see me importing spotipy and just the requests module. With the requests module, you should be able to catch the retry-after value.