Spotify is randomly timing out my API requests?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have made an app in python that needs to update a UI with currently playing song info around every second (I have a progress bar for the songs duration) but my requests are randomly timed out with spotify returning a 401. I use spotipy to interact with the API and I have added my account to my dashboard, but no dice. Any suggestions would be appreciated. I have attached the error and here is my code that requests the API:
username = 'apple11533334334343'
scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, redirect_uri='http://localhost:8888/callback')
spotify = spotipy.Spotify(auth=token, requests_timeout=10, retries=10)
image_data = requests.get('https://i.scdn.co/image/ab67616d000048515f1590fb5d2f9e1bed8bb2e8').content #creates a default image to be changed since get_info doesnt update fast enough
track_name = str()
artists_name = str()
album_name = str()
progress = 0.1
total_length = 0.1
def get_info():
while True:
try:
info = spotify.current_user_playing_track()
#print(json.dumps(info, indent=4))
global image_data
global track_name
global artists_name
global album_name
global progress
global total_length
#get the data from the json returned
track_name = info['item']['name']
artists = [artist for artist in info['item']['artists']]
artists_name = ', '.join([artist['name'] for artist in artists])
image_info = info['item']['album']['images'][1]
album_name = info['item']['album']['name']
progress_ms = info['progress_ms']
track_length = info['item']['duration_ms']
track_url = info['item']['external_urls']['spotify']
#make variables to use in app
track_name = 'Song Title: ' + track_name
artists_name = 'Artists: ' + artists_name
album_name = 'Album Name: ' + album_name
#convert the time to s for progress bar
progress = progress_ms / 1000
total_length = track_length / 1000
#get the image from the url to put in app
image_url = image_info['url']
image_data = requests.get(image_url).content
except TypeError:
track_name = 'No Song Playing'
artists_name = '...'
album_name = '...'
except socket.gaierror:
print('Connection Error, try connecting wifi dumbass')
except ConnectionError as e:
print(f"Connection error encountered {e}")
except TimeoutError:
print("Timeout Error")
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page