I am building a custom Spotify music player using Flask on the backend to handle calls to the Spotify API. It gets information from the currently playing track and populates the local webpage. I now need to poll the Spotify API (every 2 seconds or so) to check whether the song has changed and if so, update the webpage (hopefully with JavaScript so there is no need to refresh the page). I am not sure how do this with Flask or if there is a better method to go about it. I believe I can solve the issue by creating and calling an asynchronous function to do the polling, but if a change was identified I'm not sure where to go from there.
Here is the spotify view I have thus far. I am using the spotipy library inside custom auth and now_playing functions in a spotify.py file.
@app.route('/spotify')
def spotify():
# Get Spotify instance and authorization token
authData = dev.auth()
sp = authData[0]
token = authData[1]
current = dev.now_playing(sp, token)
# Assign individual track information to variable for sending to web page via Jinja
separator = ', '
return render_template('spotify.html',
artists = separator.join(current[0]),
song = current[1],
album = current[2],
cover_url = current[3],
year = current[4],
auth_tok = token
)
I have limited web development experience and this is my first time using Flask. I am using this project to learn more but have hit this roadblock.