Skip to content

Instantly share code, notes, and snippets.

@makispl
Last active March 8, 2020 01:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makispl/45eda1bd7cf471d097728eb07b32ca3b to your computer and use it in GitHub Desktop.
Save makispl/45eda1bd7cf471d097728eb07b32ca3b to your computer and use it in GitHub Desktop.
def fetch_playlist_tracks(sp, username, playlist_id):
"""
Returns the tracks for the given playlist.
"""
offset = 0
tracks = []
# Make the API request
while True:
content = sp.user_playlist_tracks(username, playlist_id, fields=None, limit=100, offset=offset, market=None)
tracks += content['items']
if content['next'] is not None:
offset += 100
else:
break
track_id = []
track_name = []
for track in tracks:
track_id.append(track['track']['id'])
track_name.append(track['track']['name'])
# Create the final df
df_playlists_tracks = pd.DataFrame({"track_id":track_id, "track_name": track_name})
return df_playlists_tracks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment