Last active
March 8, 2020 01:31
-
-
Save makispl/45eda1bd7cf471d097728eb07b32ca3b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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