Help Wizard

Step 1

NEXT STEP

Add sorting to the Web API

Add sorting to the Web API

My Question or Issue

Hi everyone. I have a small project which will retrieve the albums for an artist, and then get the tracks for these albums

 

My application only cares about the latest new tracks and albums. But, the API doesn't provide me any option to sort by date. This means, I have to go through all albums of an artist, find the new ones, and then get the tracks for those albums.

 

This is inefficient, and gets me rate limited.

 

Request: please allow us to sort the responses based on date (asc, desc), so that we do a single request, instead of potentially thousands.

 

Reply
2 Replies

I agree this would be a great feature. My app also only cares about recent releases - I have found that if you specify a single include_group (aka album type) eg single or album, the results are in descending order of release date. But this can change at any time so don't bet your life that it will stay that way. 

 

In case you're interested:

 

 

    def get_albums_released_since(self, artist_id, released_after_date):
        time.sleep(1) # Avoid rate limiting
        albums = []
        for album_type in ['album', 'single']:
            page = self.spotify_client.artist_albums(artist_id, album_type=album_type, limit=50)
            while page:
                time.sleep(1)
                items = page["items"]
                for item in items:
                    if album_released_after_date(item, released_after_date):
                        albums.append(self._artist_album_info(item))
                    else:
                        page = {"next": None}
                        break
                page = self.spotify_client.next(page)
        return albums

 

 

 

This stops paging once the albums in the response are released before the specified date, to minimize the number of requests. My app also does a boatload of time.sleep-ing to avoid rate limiting -_- 

Oh also another reason to do separate requests for each album type/include_group, if you don't do that then the pagination is straight up broken and you won't get the full set of albums even if you go through all the pages. Just FYI. 

Suggested posts