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

Getting more than 100 tracks via getPlaylist #105

Closed
TalonGrayson opened this issue Dec 22, 2015 · 4 comments
Closed

Getting more than 100 tracks via getPlaylist #105

TalonGrayson opened this issue Dec 22, 2015 · 4 comments

Comments

@TalonGrayson
Copy link

Hi there,

Apologies if this isn't the correct place to ask.

I'm trying to get all track from a playlist and the playlist has more than 100 tracks within. Using the code below, I am able to get the first 100 tracks, but despite my efforts I am unable to figure out how to get the next 100 (or increase the limit). Could you tell me how to get all tracks, even if there are, say, thousands?

public void TracksFromSpotifyPlaylist(final String playlistID) {

    api.setAccessToken(mToken);
    SpotifyService spotify = api.getService();

    spotify.getPlaylist(mUserID, playlistID, new Callback<Playlist>() {

        @Override
        public void success(Playlist playlist, retrofit.client.Response response) {

            QueueManager.spotifyPlaylistTracks.clear();

            //Only gets the first 100:
            for (PlaylistTrack track : playlist.tracks.items) {
                MusicTrack newTrack = new MusicTrack(track);
                QueueManager.spotifyPlaylistTracks.add(newTrack);
            }
        }

        @Override
        public void failure(RetrofitError error) {
            Log.i("Error", error.toString());
        }
    });
}
@JonesN3
Copy link

JonesN3 commented Dec 27, 2015

+1

@JonesN3
Copy link

JonesN3 commented Dec 27, 2015

@TalonGrayson

If you use the

getPlaylist(String trackIds, Map<String,Object> options, retrofit.Callback<Tracks> callback)

call instead, you can feed in options with a map. You can edit your code like this (probably)

Map<String, Object> options = new Map<String, Object>();
options.put(SpotifyService.OFFSET, 0)
options.put(SpotifyService.LIMIT,2000)
spotifyService.getPlaylist(userId, playlistId, options, new Callback<Playlist>() {
.......

And get litterally (two) tousands of tracks. Probably a good idea to limit it to do several calls and not set the limit too high and use the offset

Edit: Actually the limit cannot be set higher than 100, you will have to do multiple calls with offset.

@TalonGrayson
Copy link
Author

Hi there,

Just wanted to reply to say thank you for the info. I haven't had a chance to try this out yet as I have barely even been at my PC over the last couple of weeks.

I will report back once I have tried out and hopefully we can close this :)

@kaaes
Copy link
Owner

kaaes commented Jan 7, 2016

If you'd like to fetch all tracks in the playlist you should use getPlaylistTracks method that allows to
request specific subset of tracks using offset and limit parameters.
Unfortunately setting limit to 2000 won't work because max size of the page is 100 (defined in the endpoint docs).

The response of this call is a paging object that has the total field that will tell you how many tracks the playlist has as well as offset and limit fields that will tell you where in the track set you are.

So for a playlists with 268 tracks a simplified example could look like this:

Map<String, Object> options = new Map<String, Object>();
options.put(SpotifyService.LIMIT, 100);

// get tracks 0-99
options.put(SpotifyService.OFFSET, 0);
Pager<PlaylistTrack> response = spotifyService.getPlaylistTracks(userId, playlistId, options);

// get tracks 100-199
options.put(SpotifyService.OFFSET, 100);
Pager<PlaylistTrack> response = spotifyService.getPlaylistTracks(userId, playlistId, options);

// get tracks 200-268
options.put(SpotifyService.OFFSET, 200);
Pager<PlaylistTrack> response = spotifyService.getPlaylistTracks(userId, playlistId, options);

Where you can keep querying as long as offset + limit < total

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants