Repository navigation
Autoplay on Spotify, from the artists in my queue and my playlists - #769
Conversation
isaaclins
left a comment
There was a problem hiding this comment.
Read through it, built it, ran the 2702 tests and the dead-code scan, and looked at the queue panel in screenshot mode. The search and playlist parts and the rate limits look right to me. One real problem, below.
| static func fillTarget(for service: MusicService) -> Int { | ||
| switch service { | ||
| case .appleMusic: 8 | ||
| case .spotify: 1 |
There was a problem hiding this comment.
With a fill target of 1 this rule is only as good as the count it is compared with, and ProviderQueueAutoplayPlayer.autoplayUpcomingTracks() drops every podcast episode from Spotify's queue before work() counts it (want = fillTarget - upcoming.count). So on Spotify, when what's left of a playlist or a show is episodes, Autoplay sees nothing ahead and adds a song, and Spotify plays it before those episodes. That's exactly the "lands before the rest of the playlist" case #617 rules out. Apple Music has no episodes, so it never showed there.
Fix: count every item ahead (songs and episodes) for the fill rule, and keep using only the songs as seeds and for the exclusions. Plus a test on the Spotify-like queue with episodes left in the context.
There was a problem hiding this comment.
Fixed in b57694a. The fill rule now counts every item ahead, songs and episodes, and only the songs steer and get excluded as before. testEpisodesLeftInThePlaylistCountAsAhead plays a playlist that ends in two episodes: on its last song nothing is added. It fails with the old count.
isaaclins
left a comment
There was a problem hiding this comment.
Another pass over it. Three more things to fix, below.
| /// suggestions, and where its songs go (#617). | ||
| static func helpKey(isEnabled: Bool, service: MusicService?) -> String { | ||
| let key = isEnabled ? "queue.autoplay.help.on" : "queue.autoplay.help.off" | ||
| return service == .spotify ? key + ".spotify" : key |
There was a problem hiding this comment.
Building queue.autoplay.help.on.spotify and .off.spotify by appending ".spotify" means neither key appears in full in the code, so the localization audit reports both as orphaned catalog keys. That's why Static audits is red. The four keys should be written out in full.
There was a problem hiding this comment.
Fixed in f21283d: helpKey now returns each of the four keys written out in full, and scripts/audit-localization.sh reports 0 orphaned keys.
| if let library, let libraryReadAt, libraryReadAt.duration(to: now()) < Self.libraryMaxAge { | ||
| return library | ||
| } | ||
| let index = SpotifyAutoplayLibraryIndex(playlists: await catalog.autoplayLibraryPlaylists()) |
There was a problem hiding this comment.
Only the disk read is detached. SpotifyAutoplayLibraryIndex(playlists:) then runs on the main thread (the source is @MainActor), normalizing every artist name in every playlist, and relatedArtists(of:) walks every playlist for each recent artist on each look. With a big library that's real work on main every 15 minutes, which the description says Autoplay never does. The index should be built off the main thread, and the related-artist lookup should only touch the playlists that artist is in.
There was a problem hiding this comment.
Fixed in f21283d: the index is built in a detached utility task, and it now keeps which playlists each artist is in, so finding related artists only walks that artist's playlists instead of all of them.
|
|
||
| func autoplayTracks(byArtistNamed name: String) async throws -> [MusicTrack] { | ||
| let quoted = name.replacingOccurrences(of: "\"", with: "") | ||
| return try await api.searchTracks(query: "artist:\"\(quoted)\"", limit: SpotifyAutoplaySource.songsPerArtist) |
There was a problem hiding this comment.
The search sends no market, so Spotify leaves out is_playable and the ranking's isPlayable != false filter lets everything through. Autoplay can then queue a song that isn't available in my country. market=from_token fixes that.
There was a problem hiding this comment.
Fixed in f21283d: searchTracks takes an optional market and Autoplay passes from_token. A new test checks the request carries it and that a song marked is_playable: false never comes out of the ranking.
|
Went over it once more after the fixes. What I changed along the way:
Nothing else open from my side. |
f21283d to
c603d01
Compare
isaaclins
left a comment
There was a problem hiding this comment.
One more before this goes in.
| // Only what keeps ``fillTarget`` songs ahead: a long list still ahead gets nothing | ||
| // added, so the player's queue isn't touched while it plays (#735). | ||
| let want = fillTarget - upcoming.count | ||
| let want = fillTarget - itemsAhead |
There was a problem hiding this comment.
With one song kept ahead on Spotify, this count has no slack. Right after a song change GET /me/player/queue can still answer with the queue from before it, which lists the song that just started first. On an album's last song that reads as one song ahead, so nothing is added, and since no later song change comes, playback just ends. The read should leave out a first entry that is the song now playing, with a test where the queue read lags.
There was a problem hiding this comment.
Fixed in d1b803a. The queue Autoplay reads is now a list of the items ahead, and a first entry that is the song now playing is left out. testAQueueReadFromBeforeTheSongChangeStillAddsOnTheLastSong lets the read lag once on the album's last song: one song is still added. It fails without the fix.
…t share my playlists with them Spotify gives this app no similar-song suggestions, so Spotify Autoplay searches songs by the artists in the recent queue and by the artists that share my own playlists with them (read from the playlists the app already keeps on disk). Each artist is searched once per session, at most three new searches each time it looks, and none for a while after a 429. Spotify's queue has one place to add to, before the rest of the album or playlist, so on Spotify Autoplay adds one song at a time and only once nothing else is left to play: its songs never cut ahead of the album or land between songs I queued. The button's help and the status line say what it uses and where its songs go. Fixes #617
…g before them Spotify's queue also lists episodes. Autoplay counted only the songs, so with episodes left in a playlist or show it saw nothing ahead and added a song that Spotify then played before them.
…hread, spell out the help keys - the search sends market=from_token, so songs that don't play in my country are marked and never queued - the playlist index is built off the main thread, and finding the artists that share playlists only looks at that artist's playlists - the Spotify help text keys are written out in full, so the localization audit sees them
…lists on disk and their fallback
…irst Right after a song change GET /me/player/queue can answer with the queue from before it. With one song kept ahead, that stale answer read as a song still to come on an album's last song, so nothing was added and playback ended. The 429 log line now says how long searches really pause.
d1b803a to
6c7139a
Compare
Fixes #617
How it looks
The queue panel in Spotify mode (screenshot mode's sample library). Before, Spotify had no ∞ button. Now it has the same button as Apple Music, and while it's on, the line under the header says what Spotify Autoplay uses. Hovering the button explains the rest (what it's built from, that Spotify doesn't let the app use its own suggestions, and where the songs go).
Apple Music's button, status line and help text are unchanged.
What changed
Where the songs come from (
SpotifyAutoplaySource). Spotify closed Recommendations, Related Artists and artist top tracks to apps like mine, so Spotify Autoplay is built from what's still open:artist:"…"filter finds. Search results where the artist only features are left out.Searches ask for my market (
market=from_token), so a song that doesn't play in my country comes back marked and is never queued. Reading the playlists from disk and indexing them happens off the main thread.The rules from Apple Music Autoplay stay the same because it's the same
QueueAutoplay: never a song I heard this session, never one I took out of the queue, never the same song twice, artists spread out, off stops adding.I left out
genre:search: it needs each artist's genres, and reading artists is one of the endpoints development-mode apps lost.Rate limits. Each artist is searched once per session and then kept. At most 3 new searches each time Autoplay looks, which is at most once per new song. After a 429 the searches pause for the
Retry-After(at least a minute) and Autoplay keeps going with songs from my playlists. In a long session that's a handful of requests per song at the start and close to none later.Where the songs go. Spotify's queue has one place to add to: after the songs I queued by hand, but before the rest of the album or playlist. So on Spotify Autoplay keeps one song ahead instead of eight, which means it only adds once nothing else is left to play. Podcast episodes count as ahead too: with episodes left in a playlist or show, nothing is added before them. And when Spotify's queue answer lags a song change and still lists the song that just started, that song isn't counted as ahead. Its songs never cut ahead of the album or land between songs I queued. The help text says this.
Smaller bits
QueueAutoplayArtistWeightsis the artist weighting both sources use (moved out ofAppleMusicAutoplaySource, same behaviour).SpotiglassPlaybackHostgets a token provider for the catalog so Spotify's Autoplay can search; previews and tests without one have no Spotify Autoplay, as before.searchTracksis used now, so it's out of the dead-code baseline.How I tested it
SpotifyAutoplayTestsreplays a session on a queue that behaves like Spotify's (adds go after my queued songs, before the rest of the album): an album plays, I queue two songs partway, Autoplay is on. The album and my songs play in the order Spotify queued them with nothing of Autoplay's in between, then 34 new songs follow without a repeat. With Apple Music's fill rule this test fails: 42 adds cut ahead of other songs.testEpisodesLeftInThePlaylistCountAsAhead: a playlist that ends in two episodes gets nothing added on its last song. It fails when only songs are counted.testAQueueReadFromBeforeTheSongChangeStillAddsOnTheLastSong: the queue read lags once on the album's last song, and a song is still added. It fails without the fix.market=from_token, and a song Spotify marks as not playable never reaches the queue.make build,make test(2755 tests, 0 failures) andscripts/check-periphery.shpass locally. The scan's one new finding,SpotiglassSettingsView.columnVisibility, is the one my newer local Xcode reports and CI doesn't.Speed. The ∞ button is the same control and the same code path as on Apple Music: it flips a stored flag and the button's colour on the main thread. The latency test doesn't time the queue panel's own buttons yet (#680), and a quick probe step I tried couldn't find the button in the app's accessibility tree, so I have no measured time for it. The suggestions are worked out after a song starts, with the disk read, the indexing and the searches off the main thread, so nothing waits on them.
Security
What I couldn't check