I'm trying to loop over the tracks in the current playlist using Spotipy.
However, I've got two stumbling blocks:
1, Being about to work out the format of information from Spotipy sp.current_user_playing_track()
- I tried a JSON pretty print which didn't work as it was "invalid"
2, Accessing the current playlist from the current track
This is what I have so far:
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util
MY_ID = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
SECRET = "000000000000000000000000000000000"
# Authorise
spoti = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=MY_ID,
client_secret=SECRET))
spoti.trace = False
username = "xxxxxxxxxxxxxxxxxxx"
scope = "user-read-currently-playing"
redirect_uri = "my_callback"
token = util.prompt_for_user_token(username, scope, client_id=MY_ID, client_secret=SECRET, redirect_uri=redirect_uri)
# sp as spotify - makes it easier
sp = spotipy.Spotify(auth=token)
current_track = sp.current_user_playing_track()
song_artist = current_track["item"]["artists"][0]["name"]
print(song_artist)
## trying to get current playlist
# current_playlist = current_track["item"]["playlist"] # wrong, but I know that now
# current_playlist = current_track["item"]["playlist"][0] # wrong, but I know that now
# current_playlist = current_track["context"]["playlist"] # keyError: "playlist"
# current_playlist = current_track["context"] # no print
# current_playlist = current_track["external_urls"] # keyError: "external_urls"