Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LB-1700: Get all tracks from spotify playlist #153

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions troi/tools/spotify_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,30 @@ def get_tracks_from_spotify_playlist(spotify_token, playlist_id):
"""
sp = spotipy.Spotify(auth=spotify_token, requests_timeout=10, retries=10)
playlist_info = sp.playlist(playlist_id)
tracks = sp.playlist_items(playlist_id, limit=100)

offset = 0
tracks = []

# spotipy limits to 100 items for each call, so run iteratively with an offset until there are no more tracks
while True:
results = sp.playlist_items(playlist_id, limit=100, offset=offset)
if len(results['items']) == 0:
break

tracks.extend(results['items'])
# set new offset for the next loop
offset = offset + len(results['items'])

name = playlist_info["name"]
description = playlist_info["description"]

tracks = _convert_spotify_tracks_to_json(tracks)
return tracks, name, description


def _convert_spotify_tracks_to_json(spotify_tracks):
def _convert_spotify_tracks_to_json(spotify_tracks: list):
tracks = []
for track in spotify_tracks["items"]:
for track in spotify_tracks:
artists = track["track"].get("artists", [])
artist_names = []
for a in artists:
Expand Down
Loading