There are no code samples for this action, and I tried using an example for writing track names to a text file, but I noticed I am sometimes missing tracks if the playlist is over 100 tracks.
i.e. one playlist said the total was 106 tracks, but I only returned 100. But, I was able to get one playlist of over 800 tracks.
Not sure about your code but I've been using this for a long time and has been working fine for me so far:
import spotipy
import spotipy.oauth2 as oauth2
def generate_token():
""" Generate the token. Please respect these credentials :) """
credentials = oauth2.SpotifyClientCredentials(
client_id=client_id,
client_secret=client_secret)
token = credentials.get_access_token()
return token
def write_tracks(text_file, tracks):
with open(text_file, 'a') as file_out:
while True:
for item in tracks['items']:
if 'track' in item:
track = item['track']
else:
track = item
try:
track_url = track['external_urls']['spotify']
file_out.write(track_url + '\n')
except KeyError:
print(u'Skipping track {0} by {1} (local only?)'.format(
track['name'], track['artists'][0]['name']))
# 1 page = 50 results
# check if there are more pages
if tracks['next']:
tracks = spotify.next(tracks)
else:
break
def write_playlist(username, playlist_id):
results = spotify.user_playlist(username, playlist_id,
fields='tracks,next,name')
text_file = u'{0}.txt'.format(results['name'], ok='-_()[]{}')
print(u'Writing {0} tracks to {1}'.format(
results['tracks']['total'], text_file))
tracks = results['tracks']
write_tracks(text_file, tracks)
token = generate_token()
spotify = spotipy.Spotify(auth=token)
# example playlist
write_playlist('doldher', '0B4jhlB6QUGHzY8i3rEwTt')
@CiniqueL
I believe the limit for the amount of tracks to be returned is 100 (check out the min and max track limit at the link below).
Source: https://developer.spotify.com/web-api/get-playlists-tracks/
I just wrote some code related to you question, below is an example:
spot = Spotify()
playlist_tracks = spot.user_playlist_tracks(user_id, playlist_id, fields='items,uri,name,id,total', market='fr')
Here's the link to the above method being called: http://spotipy.readthedocs.io/en/latest/#spotipy.client.Spotify.user_playlist_tracks
To get the user_id and playlist_id, I used spot.me() and go into Spotify app and get the URI when you right-click for the "Share" button, respectively. I believe you can use another method in spotipy library to get the playlist's ID once you get the playlist URI.
Thank you both for your answers.
I was able to find what I needed on StackOverflow. I'm still working out some other bugs in my script, but the code I'm using is:
```
for uri in pl_ids:
results = self.sp.user_playlist_tracks(uri.split(':')[2], uri.split(':')[4])
tracks = results['items']
# Loops to ensure I get every track of the playlist
while results['next']:
results = self.sp.next(results)
tracks.extend(results['items'])
return tracks
```
I think the problem was that I was appending my list instead of extending it.
Most helpful comment
Not sure about your code but I've been using this for a long time and has been working fine for me so far: