BimmerGestalt / AAIdrive

Implementations of some Android Auto features as unofficial IDrive apps
MIT License
547 stars 90 forks source link

Spotify GetPlaylistUri only works properly up to 50 user playlists #749

Open basti4557 opened 10 months ago

basti4557 commented 10 months ago

When using spotify with many playlists (51+) a problem with GetPlaylistUri can occur. The API call in getPlaylistUri() is limited to 50 entries with offset 0.

See: https://developer.spotify.com/documentation/web-api/reference/get-list-users-playlists

The Solution would be using the "total" key in the response to calculate a type of "pagination" to cycle through all playlists. For example:

Total 150 should be 3 pages, so fetch getClientPlaylists(50,0), getClientPlaylists(50,50) and getClientPlaylists(50,100)

https://github.com/BimmerGestalt/AAIdrive/blob/30f40ae8af05b9bceb10a6f8973af6a023a820dd/app/src/main/java/me/hufman/androidautoidrive/music/spotify/SpotifyWebApi.kt#L140

suspend fun getPlaylistUri(playlistName: String): SpotifyUri? = executeApiCall("Failed to find playlist with name $playlistName") {
        var playlists = webApi?.playlists?.getClientPlaylists(50,0)
        while (playlists != null) {
            val matchingPlaylist = playlists.items.find { it.name == playlistName }
            if (matchingPlaylist != null) {
                return@executeApiCall matchingPlaylist.uri
            }
            playlists = playlists.getNext()
        }

        return@executeApiCall null
    }
hufman commented 10 months ago

Good catch! SpotifyWebApi.getPlaylistUri is only used by the SpotifyAppController to manage the temporary cloned playlists of the buggy Liked Songs and Artist Radio playlists. If a user has add 50 playlists since the last time AAIdrive has updated this cloned playlist, I expect it will create a duplicate at the top of the list, or perhaps throw an error and not enable this workaround for skipping in these playlists. I personally dislike this workaround and wish it wasn't even needed, so I haven't tried polishing this particular facet :)

Definitely something to fix at some point! You can check the example for how AAIdrive iterates through the contents of a playlist.