1

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"
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • 1
    You are mixing Client Credentials code with code for Authentication. You should only use the full code [with user authentication](https://github.com/spotipy-dev/spotipy#with-user-authentication). – Ximzend May 06 '23 at 11:34

1 Answers1

1

Your code works, I tested it with my setup information. I think you missing your credentials or not set up correctly in your the developer dashboard.

But as mentioned 'Ximzend' mixed Client Credential Flow and Authorization Code Flow.

Authorization Code Flow Authorization Code Flow vs. Client Credentials Flow

and it would be better my version code. It is not a necessary username. I need to log in the user.

as get-songs-v1.py

import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
import spotipy.util as util

MY_ID   = "<your client id>"
SECRET = "<your client secret>"

# Authorise
spoti = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=MY_ID,
                                                           client_secret=SECRET))
spoti.trace = False

username = "<your user name>"
scope    = "user-read-currently-playing"
redirect_uri = "<your redirect URI>"
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 = current_track["item"]
song_artist = current_track["item"]["artists"][0]["name"]
print(song_artist," - ", song['name'])

Result v1.py

enter image description here

I copy my credential information from the dashboard.

enter image description here

username copy from my user account of profile enter image description here

Simple version v2

import spotipy
from spotipy.oauth2 import SpotifyOAuth

sp = spotipy.Spotify(auth_manager=SpotifyOAuth(
    client_id = '<your client id>',
    client_secret= '<your client secret>',
    redirect_uri='<your redirect URI>',
    scope='user-read-currently-playing'))

def Get_Current_Song():
    response_current_track = sp.current_user_playing_track()
    if response_current_track  is not None and response_current_track["item"] is not None:
        song = response_current_track["item"]
        print(song['artists'][0]['name'], " - ", song['name'])

Get_Current_Song()

Result of v2

enter image description here

#2 Get songs name and artist name.

get artist name <- ['item']['artists'][0]['name']

get song name <- ['item']['album']['artist'][0]['name']

VS code with debugging of Python. enter image description here

OR Postman how to get token in here

[ means array, you need to access by index zero.

enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14