spotipy: Hanging track search

Describe the bug Sometimes, the sp.track() just hangs without an error and without a return but at the same time, sp.audio_features() works properly.

Your code

import os
import time
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from utils.private import * 

os.environ['SPOTIPY_CLIENT_ID'] = spotify_client_id
os.environ['SPOTIPY_CLIENT_SECRET'] = spotify_client_secret


def refresh_spotify():
    print('refreshing spotify token')
    auth_manager = SpotifyClientCredentials()
    sp = spotipy.Spotify(auth_manager=auth_manager, client_credentials_manager=auth_manager, requests_timeout=2)
    return sp


id= '4wCmqSrbyCgxEXROQE6vtV'
uri = 'spotify:track:4wCmqSrbyCgxEXROQE6vtV' 
sp = refresh_spotify()
print("ready")
results = sp.audio_features(uri)
results = sp.track(uri)

And when I call sp.audio_features(uri) it responds immediately but when I call sp.track(uri) it can hang up to more than 3 minutes before I ctrl+C out of the code even though I set a timeout value.

After reading a few GitHub issues (see #913 , #862, #844, etc) it seems that there may be an issue with the spotify token going stale but I am not sure how to fix this since I am following the methodology of the README for initializing my credentials. I know that the URI is valid because it was working for about 30 minutes and then it stopped. Since I doing an augmentation for a large dataset, I need to be able to refresh my token.

Expected behavior Throwing either an error or returning the search query.

Output None and I’m not sure how to debug for one.

Environment:

  • OS: [Linux]
  • Python version [3.9]
  • spotipy version [2.22]
  • your IDE (if using any) [VSCode]

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 22 (10 by maintainers)

Most upvoted comments

No I don’t think anything that elaborate is happening.

Your retry-after header is telling you to wait about 12 minutes, so it’s not a bug but just spotify telling you that your token has been / is being over-used. I would try to start fresh from a new token. On your node/server, try to delete the .cache file in the same folder as your code (it contains your token). Just do rm .cache from a terminal.

Trying the same thing by replacing sp.tracks() with sp.audio_features() doesn’t cause any exceptions so it never prints the retry-value.

Make sense!