Skip to content

Autoplay on Spotify, from the artists in my queue and my playlists - #769

Merged
isaaclins merged 6 commits into
mainfrom
fix/617-spotify-autoplay
Nov 26, 2026
Merged

isaaclins merged 6 commits into
mainfrom
fix/617-spotify-autoplay

Conversation

@isaaclins

@isaaclins isaaclins commented Nov 26, 2026 •

Copy link
Copy Markdown
Owner

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).

Before After, off After, on
Dark
Light

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:

  • the artists of the last 20 songs (played, playing and queued; songs I picked count more than ones Autoplay added, newer more than older, same as Apple Music);
  • the artists that share my own playlists with them, the more playlists they share the stronger. This reads the playlists the app already keeps on disk, so it costs no requests;
  • songs by those artists: the ones in my playlists, plus what a search with an 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

  • QueueAutoplayArtistWeights is the artist weighting both sources use (moved out of AppleMusicAutoplaySource, same behaviour).
  • SpotiglassPlaybackHost gets a token provider for the catalog so Spotify's Autoplay can search; previews and tests without one have no Spotify Autoplay, as before.
  • New strings in English, German and Spanish.
  • searchTracks is used now, so it's out of the dead-code baseline.

How I tested it

  • SpotifyAutoplayTests replays 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.
  • The search sends market=from_token, and a song Spotify marks as not playable never reaches the queue.
  • The live reads are tested on their own: the search request (artist filter, quotes removed, my market), the playlists read from the app's cache with episodes and missing playlists left out, and the fallback to the playlists "More from your library" collected.
  • Source tests: playlist neighbours rank first and featured-only search hits are dropped; each artist is searched once and at most 3 per look over 21 looks; a 429 pauses searches, playlist songs still come, searches resume after the pause.
  • make build, make test (2755 tests, 0 failures) and scripts/check-periphery.sh pass locally. The scan's one new finding, SpotiglassSettingsView.columnVisibility, is the one my newer local Xcode reports and CI doesn't.
  • Ran the app in screenshot mode, light and dark, turned Autoplay on and off in the queue panel, and checked the status line and help text through the accessibility tree.

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

  • Privileged code: n/a, nothing here touches the helper, the driver or the install path
  • Files: n/a, Autoplay only reads the playlist files the app already keeps and writes nothing
  • Data from outside: search results go through the existing Spotify decoding; the artist name in the query has its quotes removed, and a song Spotify marks as not playable in my market is dropped
  • Secrets and privacy: the log says that a search failed or paused, never which artist was searched or what my playlists hold
  • Dependencies: n/a
  • Tests: n/a, not a security fix

What I couldn't check

  • Real Spotify playback: I have no Premium account here, so I haven't seen a real Spotify queue run out and get a song added. Screenshot mode's stand-in always reports 14 songs ahead, so it never asks for one. The queue behaviour is covered by the test above, built on how Spotify documents its queue.
  • How good the picks feel with a real library and real search results.
  • Whether Spotify's own autoplay (the account setting) puts its songs into the queue the Web API reports. If it does, my Autoplay sees songs ahead and adds nothing, so the two don't fight.

@isaaclins isaaclins added enhancement New feature or request priority:medium Fix when convenient labels Nov 26, 2026

@isaaclins isaaclins left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 isaaclins left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another pass over it. Three more things to fix, below.

Comment thread Spotiglass/Views/QueuePanelView.swift Outdated
/// 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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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())

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@isaaclins

Copy link
Copy Markdown
Owner Author

Went over it once more after the fixes. What I changed along the way:

  • Podcast episodes left in a playlist or show now count as ahead, so Autoplay never adds a song before them.
  • Searches send market=from_token, so songs that don't play in my country are never queued.
  • The playlist index is built off the main thread and only looks at the playlists an artist is in.
  • The Spotify help text keys are spelled out, which turns Static audits green again.

Nothing else open from my side.

@isaaclins
isaaclins force-pushed the fix/617-spotify-autoplay branch from f21283d to c603d01 Compare November 26, 2026 11:30

@isaaclins isaaclins left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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
…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.
@isaaclins
isaaclins force-pushed the fix/617-spotify-autoplay branch from d1b803a to 6c7139a Compare November 26, 2026 15:05
@isaaclins
isaaclins merged commit 0373706 into main Nov 26, 2026
6 checks passed
@isaaclins
isaaclins deleted the fix/617-spotify-autoplay branch November 26, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request priority:medium Fix when convenient

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[feat]: Autoplay on Spotify, built from what Spotify still lets apps read

1 participant