Search code examples
pythonapispotifyspotipy

currently_playing not working when using the Spotipy library


I'm trying to access music that the user is currently playing using the spotipy Spotify python library.

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

cid = "xxx"
csecret = "xxx"
redirectURI = "xxx"
username = "xxx"

client_credentials_manager = SpotifyClientCredentials(client_id=cid, client_secret=csecret) 
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)

scope = 'user-read-currently-playing'
token = util.prompt_for_user_token(username, scope, cid, csecret, redirectURI)

if token:
    sp = spotipy.Spotify(auth=token)
else:
    print("Can't get token for", username)

current_track = sp.current_user_playing_track()
print(json.dumps(current_track, sort_keys=False, indent=4))

I've also tried using sp.currently_playing(). I am able to access other data such as sp.current_user_saved_tracks(limit=3, offset=0). Using what I currently have, it always errors out saying AttributeError: 'Spotify' object has no attribute 'current_user_playing_track'. I've explored using node instead but I really would like to stick with python.


Solution

  • You're trying to use a feature that isn't in the latest released version yet.

    You can see on PyPI that the current version was released on 5 Jan 2017, but you can see on Github that the function you want to call was added on 13 May 2017.

    Issue #270 and Issue #211 are both asking when a new release will be pushed to PyPI, (Also, there's some weird issue with the version numbering.)

    Anyway, as #211 says:

    If you're running into issues still, you can install the package directly from the github repo.

    pip install git+https://github.com/plamere/spotipy.git --upgrade