Repository navigation
feat: experimental catalog search through pathfinder - #442
Open
leodurandfr wants to merge 3 commits into
Open
leodurandfr wants to merge 3 commits into
leodurandfr wants to merge 3 commits into
Conversation
Pathfinder (api-partner.spotify.com/pathfinder/v2/query) is the GraphQL service the official clients search through (devgianlu#311). It accepts the login5 token spclient already holds, and only runs persisted queries: an operation name and the sha256 hash of its query document. PathfinderQuery sends one such query through innerRequest, so token, 401 renewal, network errors and 5xx are handled as for every spclient request, and reads the result the way PutConnectStateRaw does: a 429 is a RateLimitedError left to the caller, any other failed status a PathfinderError, 412 being an outdated hash. A 200 whose data holds nothing but nulls, such as {"searchV2": null}, is an operation that failed as a whole; a client has seen a search come back without its searchV2 once, and such a result is run again, at most twice. Responses are capped at 8 MiB. RateLimitedError, which only connect-state returned so far, is now documented for any spclient 429. Pathfinder checks the User-Agent before the query hash and refuses Go's default, which the other spclient requests send, and UserAgent() with 403 "Client/request not allowed". Measured with the login5 token, it accepts no User-Agent at all, "abcd" (but not "abcde"), or one starting with "Mozilla" or "Spotify". Its requests carry SpotifyLikeUserAgent(), Spotify/<SpotifyVersionCode> <OS>/0 (go-librespot/<version>): the desktop client go-librespot already presents to the access point, whose client ID it also logs in to login5 with, then its own name. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Pathfinder publishes no schema, so its search responses were described by hand from what it answers, in pathfinder-spec.yml, and the Go models are generated from it with oapi-codegen into pathfinder/, as discussed in devgianlu#311. go generate runs it next to api-spec.yml. The spec only covers what the search endpoints read: the data of searchTracks, searchAlbums, searchArtists, searchPlaylists, searchPodcasts, searchFullEpisodes and searchSuggestions. Results are a oneOf told apart by __typename, so an unknown or error type (NotFound, GenericError...) can be skipped item by item. A missing result node stays nil, so it can be told from an empty page. coverage.sh now leaves out every *_gen.go file, not only api_gen.go. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
An experimental, opt-in catalog search through pathfinder, behind
search.enabled (off by default; nothing is sent to Spotify then).
The endpoints follow /library: GET /search/tracks, /search/albums,
/search/artists, /search/playlists, /search/shows and /search/episodes
each return one page, {total, offset, limit, items}, of one type. Albums
and artists are the libraryAlbum and libraryArtist of /library, so an
album carries its year. Tracks and episodes share searchTrack, named
after the fields of track, an episode naming its show as track does.
Images are picked by server.image_size. GET /search/suggestions returns
the queries Spotify suggests for what was typed so far, and its first
results by type, each list in Spotify's order.
Spotify's result count is an estimate, at times below the results it
then pages through, so total is raised to reach into the next page while
there is one, and on the last page is the offset plus what Spotify
returned there. Pathfinder pages through the first 1000 results only and
refuses an offset plus limit beyond them, so the handlers answer 400
there and total stops at 1000. A result Spotify cannot show (a NotFound
or GenericError in its place) is skipped; a response that lacks the
operation's results altogether is an error rather than an empty page, so
a change on Spotify's side does not read as nothing found.
The pathfinder models are mapped straight to the API types in the
daemon, as the library endpoints map extended metadata. A search runs
off the player loop like them, and fails like them; a 429 answers 429,
with a Retry-After from any spclient RateLimitedError, which CORS now
exposes to browsers.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #311 (search only).
Adds an experimental, opt-in catalog search. It goes through pathfinder (
api-partner.spotify.com/pathfinder), the GraphQL service the official clients search with, using the session's login5 token, not the rate-limited public Web API. Off by default (search.enabled: false); disabled, nothing is sent to Spotify.API
Shaped after
/library: one endpoint per type, each returning one page{total, offset, limit, items}.GET /search/tracks,/search/albums,/search/artists,/search/playlists,/search/showsand/search/episodes, withq,offsetandlimit(1–50, default 20).libraryAlbumandlibraryArtist, so an album carries itsyear. Tracks and episodes sharesearchTrack, named after the fields oftrack; an episode names its show the waytrackdoes. Playlists and shows get small schemas of the same kind. Images followserver.image_size.GET /search/suggestions?q=&limit=(1–30, default 10): the complete queries Spotify suggests for what was typed so far, and its first results by type, each list in Spotify's order.Retry-After. Other failures answer like the library endpoints: 500, logged.How it is built
Spclient.PathfinderQuerysends a persisted query (operation name + sha256 hash) throughinnerRequest, so the token, the 401 renewal, network errors and 500/502/503/504 are handled as for every other spclient request. It reads the result likePutConnectStateRaw: a 429 is aRateLimitedError(now documented for any spclient 429), any other failed status aPathfinderError, 412 meaning the hash is outdated. A 200 whose data holds nothing but nulls, an operation that failed as a whole, is run again, at most twice.pathfinder-spec.ymland the models are generated intopathfinder/with oapi-codegen, bygo generate, next toapi-spec.yml. The spec only covers the fields that are read.daemon/search.gomaps those models straight to theApi*types, the way the library endpoints map extended metadata, and runs off the player loop throughgoDetached, as they do.Things to know
"Invalid query hash"to a hash it does not know, which is what an old one becomes once Spotify rebuilds a query. That search then fails until go-librespot is updated, and the log names it:pathfinder searchTracks failed with status 412, persisted query hash outdated: Invalid query hash. The other searches keep working. There is no config override in this PR; I can add one if you would rather let users fix a rotation without a release.403 Client/request not allowedto Go's default (which spclient requests send) and toUserAgent(), and accepts no User-Agent at all,abcd(but notabcde), or one starting withMozillaorSpotify. Pathfinder requests, and only those, sendSpotifyLikeUserAgent(),Spotify/127700358 <OS>/0 (go-librespot/<version>): the desktop client go-librespot presents to the access point, then its own name. libspot's pathfinder client, for comparison, sends the full desktop app UA. This is the one choice I'd like your call on: isSpotify/…OK with you, or would you rather send noUser-Agentheader at all (which works too), orMozilla/5.0 (compatible; go-librespot/<version>)?totalis raised to reach into the next page while there is one, and on the last page it is the offset plus what Spotify returned. Pathfinder only pages through the first 1000 results and refusesoffset + limitabove that, so the handlers answer 400 there andtotalstops at 1000. A page can hold fewer items thanlimitbefore the last one, and an item can come back on two pages.Testing
Unit and integration tests cover:
PathfinderQuery: refusals, 412, 429, failed operations with and without GraphQL errors, partial data, oversized and non-JSON bodies, cancellation;Retry-After.On a Raspberry Pi 5, with this branch running as a separate daemon (
nullaudio output, nothing played):limit1 and 50 on every type; shows return 44 at 50.GET /statusanswered in 22 ms or less throughout.UserAgent(): 403, logged as above./statusanswering in under 3 ms meanwhile.It is also used by a client that runs four typed searches and suggestions as the user types. One search there came back without its
searchV2; when the data holds nothing but nulls like that, this branch now runs it again.🤖 Generated with Claude Code