[GH-ISSUE #154] get Track IDs of a public palylist #72

Closed
opened 2026-02-27 23:20:41 +03:00 by kerem · 3 comments
Owner

Originally created by @1337sup3rh4x0r on GitHub (Jan 15, 2017).
Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/154

Hi there,
I managed to list the track IDs of my one playlists with this script:

import sys
import os
import spotipy
import spotipy.util as util

if __name__ == '__main__':
    if len(sys.argv) > 1:
        username = sys.argv[1]
        scope = 'playlist-read-private'
        client_id = sys.argv[2]
        client_secret = sys.argv[3]
        redirect_uri = sys.argv[4]
        username = sys.argv[1]
    else:
        print("Whoops, need your username!")
        print("usage: python user_playlists_contents.py [username]")
        sys.exit()

    token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri)

    if token:
        top = 40
        sp = spotipy.Spotify(auth=token)
        playlists = sp.user_playlists(username)
        for playlist in playlists['items']:
            if playlist['owner']['id'] == username:
                print()
                print(playlist['name'])
                print('  total tracks', playlist['tracks']['total'])
                results = sp.user_playlist(username, playlist['id'], fields="tracks,next")
                tracks = results['tracks']
                show_tracks(tracks)
                while tracks['next']:
                    tracks = sp.next(tracks)
                    show_tracks(tracks)
    else:
        print("Can't get token for", username)

however this does not work for public playlists I follow, e.g. this one:

spotify:user:spotifycharts:playlist:37i9dQZEVXbJiZcmkrIHGU

In that case the script returns:

Traceback (most recent call last):
  File "spotify_trackID_fetcher.py", line 37, in <module>
    results = sp.user_playlist(username, playlist['id'], fields="tracks,next")
  File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 378, in user_playlist
    return self._get("users/%s/playlists/%s" % (user, plid), fields=fields)
  File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 146, in _get
    return self._internal_call('GET', url, payload, kwargs)
  File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 124, in _internal_call
    headers=r.headers)
spotipy.client.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/users/myspotifyusername/playlists/37i9dQZEVXbJiZcmkrIHGU?fields=tracks%2Cnext:

Obviously the followed playlist does not conatin the same fields?

I also tried to set the username variable in my script posted above to "spotifycharts" which is the owner of the playlist, but then nothing is returned.

How can I get a list of those tracks?

Originally created by @1337sup3rh4x0r on GitHub (Jan 15, 2017). Original GitHub issue: https://github.com/spotipy-dev/spotipy/issues/154 Hi there, I managed to list the track IDs of my one playlists with this script: import sys import os import spotipy import spotipy.util as util if __name__ == '__main__': if len(sys.argv) > 1: username = sys.argv[1] scope = 'playlist-read-private' client_id = sys.argv[2] client_secret = sys.argv[3] redirect_uri = sys.argv[4] username = sys.argv[1] else: print("Whoops, need your username!") print("usage: python user_playlists_contents.py [username]") sys.exit() token = util.prompt_for_user_token(username,scope,client_id,client_secret,redirect_uri) if token: top = 40 sp = spotipy.Spotify(auth=token) playlists = sp.user_playlists(username) for playlist in playlists['items']: if playlist['owner']['id'] == username: print() print(playlist['name']) print(' total tracks', playlist['tracks']['total']) results = sp.user_playlist(username, playlist['id'], fields="tracks,next") tracks = results['tracks'] show_tracks(tracks) while tracks['next']: tracks = sp.next(tracks) show_tracks(tracks) else: print("Can't get token for", username) however this does not work for public playlists I follow, e.g. this one: `spotify:user:spotifycharts:playlist:37i9dQZEVXbJiZcmkrIHGU` In that case the script returns: Traceback (most recent call last): File "spotify_trackID_fetcher.py", line 37, in <module> results = sp.user_playlist(username, playlist['id'], fields="tracks,next") File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 378, in user_playlist return self._get("users/%s/playlists/%s" % (user, plid), fields=fields) File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 146, in _get return self._internal_call('GET', url, payload, kwargs) File "/home/user/.local/lib/python2.7/site-packages/spotipy/client.py", line 124, in _internal_call headers=r.headers) spotipy.client.SpotifyException: http status: 404, code:-1 - https://api.spotify.com/v1/users/myspotifyusername/playlists/37i9dQZEVXbJiZcmkrIHGU?fields=tracks%2Cnext: Obviously the followed playlist does not conatin the same fields? I also tried to set the username variable in my script posted above to "spotifycharts" which is the owner of the playlist, but then nothing is returned. How can I get a list of those tracks?
kerem closed this issue 2026-02-27 23:20:42 +03:00
Author
Owner

@cmcnutt1 commented on GitHub (Jan 16, 2017):

This is an issue I fixed myself a while back, due to needing to access my Discovery playlist (which for some reason, is public). I've created a pull request to implement public playlists. More information on the usage is documented there.

#156 - Add support for public playlists

<!-- gh-comment-id:272986141 --> @cmcnutt1 commented on GitHub (Jan 16, 2017): This is an issue I fixed myself a while back, due to needing to access my Discovery playlist (which for some reason, is public). I've created a pull request to implement public playlists. More information on the usage is documented there. #156 - Add support for public playlists
Author
Owner

@plamere commented on GitHub (Jan 18, 2017):

You are using the wrong userid when reading the playlist. You need to extract the user id from the playlist uri. See this example:

https://github.com/plamere/spotipy/blob/master/examples/read_a_playlist.py

<!-- gh-comment-id:273628742 --> @plamere commented on GitHub (Jan 18, 2017): You are using the wrong userid when reading the playlist. You need to extract the user id from the playlist uri. See this example: https://github.com/plamere/spotipy/blob/master/examples/read_a_playlist.py
Author
Owner

@1337sup3rh4x0r commented on GitHub (Jan 21, 2017):

Thank you, this is working!

You're right, I was initially passing my username from the token to the playlist function ...

<!-- gh-comment-id:274288895 --> @1337sup3rh4x0r commented on GitHub (Jan 21, 2017): Thank you, this is working! You're right, I was initially passing my username from the token to the playlist function ...
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/spotipy#72
No description provided.