I saw the solution by mardiff which absolutely works, but I didn't like that it waits for an error to occur and then fixes it so I found a solution which doesn't require catching errors, using methods which spotipy already has implemented.
import spotipy
from spotipy.oauth2 import SpotifyOAuth
import time
USERNAME = '...'
CLIENT_ID = '...'
CLIENT_SECRET = '...'
SCOPE = 'user-read-currently-playing'
def create_spotify():
auth_manager = SpotifyOAuth(
scope=SCOPE,
username=USERNAME,
redirect_uri='http://localhost:8080',
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET)
spotify = spotipy.Spotify(auth_manager=auth_manager)
return auth_manager, spotify
def refresh_spotify(auth_manager, spotify):
token_info = auth_manager.cache_handler.get_cached_token()
if auth_manager.is_token_expired(token_info):
auth_manager, spotify = create_spotify()
return auth_manager, spotify
if __name__ == '__main__':
auth_manager, spotify = create_spotify()
while True:
auth_manager, spotify = refresh_spotify(auth_manager, spotify)
playing = spotify.currently_playing()
if playing:
print(playing['item']['name'])
else:
print('Nothing is playing.')
time.sleep(30)
Using this method you check if the token is expired (or within 60 seconds of expiring) before each use of the spotify object. Creating new auth_manager and spotify objects as needed.