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='user-read-playback-state', username='jaydee', redirect_uri='https://ha.sectorq.eu/auth/external/callback', client_id='6a37229fc9a04878a608d519ea4b1d22', client_secret='6569f6f876234093983920de2966ff84') 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)