conradludgate / terraform-provider-spotify

Terraform provider for spotify

Home Page:https://registry.terraform.io/providers/conradludgate/spotify/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature Request: Preserve Playlist Ordering

nnichols opened this issue · comments

Currently, the tracks argument is a set of tracks that comprise a Spotify playlist. Modifying this list appropriately triggers song to be added and removed from the playlist; however, the relative ordering of songs is not preserved.

Since playlists are ordered, and the order may be useful (e.g. the ending of one song immediately flowing into the prelude of another) it would be nice if the playlist resource preserved the ordering of songs to match the order of tracks

For example:

The playlist denoted here lists the track "Induction" by Terraform as the first song, immediately followed by Hanz Zimmer's "Terraforming"; however, the resulting playlist has "FXMLDR" by Thank You Scientist as the first song.

I believe songs are being added to the bottom of the playlist on each application if they're new, and correctly being removed in-place.

Yeah that's right. All inserts currently happen at the end and ordering is not preserved. I think i did originally have track order enforced but it was a bit tricky. I can attempt to look into it again.

I think the best way to do this is to do what already happens (remove and add tracks based on the set difference), then use the ReorderPlaylistTracks api to move them into their final positions. I just need to find a good way to determine the reordings.

commented

Coincidentally I was working on this for a short time just the other day. I approached it by revamping tracks to be a TypeList instead of TypeSet. I have it working, but it has two significant bugs I haven't been able to work out. It could be a reasonable alternative to what you mention above.

If you are interested in the code, check out the WIP: preserve playlist ordering files here

An advantage of using TypeList is you can have duplicate songs in the playlist if you choose.

I didn't specify but I also did mean to use type list. The problem is that you still need to actually insert the tracks into the correct position.

The AddTracksToPlaylist api only adds tracks to the end. And RemoveTracksFromPlaylists removes all instances of the track. There's the ReplacePlaylistTracks api (the Spotify package doesn't expose it but it's on the api docs) can completely replace the tracks with a specific ordering but it seems to suggest it only supports playlists of 100 items. I'll try and test it

Yeah, the solution that came immediately to mind felt hacky, but guaranteed order: Removing all tracks and then re-adding the entirety of the playlist. Are any of the requests rate-limited or otherwise bounded in scope? (Would issues manifest that ultimately result in a similar 100 song limit?)

I was thinking of that too. There is indeed rate limiting, and I think I remember getting bitten by it before with this project.

The idea works in principal. You can repeatedly add songs in batches of 100 to fill up the playlist. But if you're just adding a single song to the end of your playlist and the provider spends a bunch of API calls to just replace all the songs it seems a bit wasteful.

I guess it's just worth considering how often playlists have 500+ songs. I guess not that many do. Most I follow seem to stick below the 100 song limit (probably because the tools they use to manage playlists use the ReplacePlaylistTracks api).

It seems the spotify client I'm using can handle automatic retries for rate limit responses: https://pkg.go.dev/github.com/zmb3/spotify#Client

I don't have time at the moment to implement these changes myself. I'm happy if someone wants to take a stab at it - I know you made a start @tdeknecht. Otherwise I might have some time next week

Released in v0.2.2 🚀