Skip to content

Unofficial YouTube Music / Spotify Premium client for Android, built in Liquid Glass. Audio effects, Spotify Connect, listening history, 8 languages. Rust core on librespot.

Read playlists from the gateway as well

Lelonio2b48e9fcf577
diff --git a/app/src/main/java/dev/lelonio/square/SquareApplication.kt b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
index 6d16d76..9875522 100644
--- a/app/src/main/java/dev/lelonio/square/SquareApplication.kt
+++ b/app/src/main/java/dev/lelonio/square/SquareApplication.kt
@@ -71,6 +71,11 @@ class SquareApplication : Application() {
dev.lelonio.square.data.PathfinderKeys(this)
}
+ /** Track lists from Spotify's own gateway; see [dev.lelonio.square.data.Gateway]. */
+ val gateway: dev.lelonio.square.data.Gateway by lazy {
+ dev.lelonio.square.data.Gateway(pathfinderKeys)
+ }
+
/** Which stretcher works out speed and pitch. */
val effectQuality: dev.lelonio.square.data.EffectQualityStore by lazy {
dev.lelonio.square.data.EffectQualityStore(this)
diff --git a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
index 50837b3..f84dc72 100644
--- a/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
+++ b/app/src/main/java/dev/lelonio/square/backend/SpotifyBackend.kt
@@ -10,7 +10,6 @@ import dev.lelonio.square.data.PlaylistDetailsDto
import dev.lelonio.square.data.SearchResults
import dev.lelonio.square.data.toCatalogTrack
import dev.lelonio.square.data.toResults
-import dev.lelonio.square.data.SavedTracks
import dev.lelonio.square.nativecore.NativeBridge
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
@@ -133,59 +132,33 @@ class SpotifyBackend(private val container: SquareApplication) : MusicBackend {
.map { it.toCatalogTrack() }
}
- container.webApi.isReady && uri.startsWith("spotify:playlist:") ->
- webApiPlaylistTracks(uri.substringAfterLast(':'))
+ // Spotify's own gateway first, for both of the lists it answers for:
+ // it needs nothing of the listener beyond being logged in, and hands
+ // back whole tracks rather than URIs to look up one at a time. The Web
+ // API, which is metered against an application the listener has to
+ // register themselves, is what is left when a query hash is retired.
+ uri.startsWith("spotify:playlist:") ->
+ container.gateway.readAll { offset -> container.gateway.playlistTracks(uri, offset) }
+ ?: if (container.webApi.isReady) {
+ webApiPlaylistTracks(uri.substringAfterLast(':'))
+ } else {
+ Catalog.tracks(Catalog.contextTrackUris(uri))
+ }
- // Liked Songs is not a context the access point will resolve: asked
- // for it answers 503. Spotify's own gateway answers it for anyone
- // logged in, and the Web API — which needs the listener's registered
- // application — is what is left when a query hash has been retired.
- uri.endsWith(":collection") -> gatewaySavedTracks() ?: run {
- check(container.webApi.isReady) {
- container.getString(dev.lelonio.square.R.string.liked_songs_failed)
+ // Liked Songs is not a context the access point will resolve either:
+ // asked for it answers 503. So when both of these fail there is
+ // nothing left to try.
+ uri.endsWith(":collection") ->
+ container.gateway.readAll { offset -> container.gateway.savedTracks(offset) } ?: run {
+ check(container.webApi.isReady) {
+ container.getString(dev.lelonio.square.R.string.liked_songs_failed)
+ }
+ savedTracks()
}
- savedTracks()
- }
else -> Catalog.tracks(Catalog.contextTrackUris(uri))
}
- /**
- * Everything the account has saved, from the gateway; see SavedTracks.
- *
- * Null when the gateway will not answer, which is the caller's cue to ask
- * the Web API the same question.
- */
- private suspend fun gatewaySavedTracks(): List<CatalogTrack>? {
- val keys = container.pathfinderKeys
- keys.refresh()
- val language = java.util.Locale.getDefault().language
-
- val loaded = mutableListOf<CatalogTrack>()
- var offset = 0
- while (true) {
- val page = runCatching {
- val raw = withContext(Dispatchers.IO) {
- NativeBridge.likedSongs(
- """{"offset":$offset,"limit":$GATEWAY_LIBRARY_PAGE}""",
- language,
- keys.likedSongs,
- keys.appVersion,
- )
- }
- SavedTracks.parse(raw)
- // A page that will not come back gives up the whole read, part
- // way through as much as at the start: half a list handed to
- // the queue is a queue that ends where the page did.
- }.getOrNull() ?: return null
-
- loaded += page.tracks
- val next = page.nextOffset
- if (next == null || next <= offset || loaded.size >= page.total) return loaded
- offset = next
- }
- }
-
/** Everything the account has saved, newest first, a page at a time. */
private suspend fun savedTracks(): List<CatalogTrack> {
val loaded = mutableListOf<CatalogTrack>()
@@ -294,9 +267,6 @@ class SpotifyBackend(private val container: SquareApplication) : MusicBackend {
*/
const val WEB_API_LIBRARY_PAGE = 50
- /** What the gateway takes at a time; see MainViewModel. */
- const val GATEWAY_LIBRARY_PAGE = 200
-
/**
* The purple heart Spotify's own clients draw for Liked Songs.
*
diff --git a/app/src/main/java/dev/lelonio/square/data/Gateway.kt b/app/src/main/java/dev/lelonio/square/data/Gateway.kt
new file mode 100644
index 0000000..cf9d9d8
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/Gateway.kt
@@ -0,0 +1,104 @@
+package dev.lelonio.square.data
+
+import dev.lelonio.square.nativecore.NativeBridge
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.withContext
+
+/**
+ * Track lists read from Spotify's own GraphQL gateway.
+ *
+ * The endpoint the web player uses, and the reason to prefer it: the Web API
+ * meters every request against an application the listener has to register for
+ * themselves, and the access point answers a context with URIs that then cost
+ * a round trip each to turn into names. This answers with whole tracks, two
+ * hundred at a time, for anyone who is logged in.
+ *
+ * What it asks in return is that nothing here be relied on. A persisted query
+ * is addressed by the hash of a query Spotify already knows, and those are
+ * retired whenever its web client is rebuilt, so every read here can come back
+ * with nothing and every caller has to have somewhere else to go. See
+ * [PathfinderKeys] for how the hashes are kept current, and
+ * native/src/pathfinder.rs for the request itself.
+ */
+class Gateway(private val keys: PathfinderKeys) {
+
+ /** One page of the account's saved tracks; see [SavedTracks]. */
+ suspend fun savedTracks(offset: Int): GatewayPage? = SavedTracks.parse(
+ query(
+ operation = "getLikedSongs",
+ hash = keys.likedSongs,
+ variables = """{"offset":$offset,"limit":$PAGE}""",
+ ),
+ )
+
+ /** One page of a playlist; see [PlaylistContents]. */
+ suspend fun playlistTracks(uri: String, offset: Int): GatewayPage? = PlaylistContents.parse(
+ query(
+ operation = "fetchPlaylistContents",
+ hash = keys.playlist,
+ variables = """{"uri":"$uri","offset":$offset,"limit":$PAGE,""" +
+ """"includeEpisodeContentRatingsV2":true}""",
+ ),
+ )
+
+ /**
+ * Reads a list to its end.
+ *
+ * Null when the first page will not come back, and null part way through
+ * as well: what this returns is cached and stamped as the list, so half a
+ * playlist would be remembered as all of it.
+ *
+ * @param onPage called with everything read so far, and whether more is
+ * coming, so a screen can fill in as it arrives.
+ */
+ suspend fun readAll(
+ onPage: ((List<CatalogTrack>, Boolean) -> Unit)? = null,
+ page: suspend (offset: Int) -> GatewayPage?,
+ ): List<CatalogTrack>? {
+ keys.refresh()
+
+ val loaded = mutableListOf<CatalogTrack>()
+ var offset = 0
+ while (true) {
+ val read = runCatching { page(offset) }
+ .onFailure { android.util.Log.i(TAG, "gateway list unavailable: ${it.message}") }
+ .getOrNull()
+ ?: return null
+
+ loaded += read.tracks
+ val next = read.nextOffset
+ onPage?.invoke(loaded.toList(), next != null)
+ // Counted as well as followed: a page naming itself as its own
+ // successor would otherwise be read for ever.
+ if (next == null || next <= offset) {
+ android.util.Log.i(TAG, "gateway list: ${loaded.size} of ${read.total}")
+ return loaded
+ }
+ offset = next
+ }
+ }
+
+ private suspend fun query(operation: String, hash: String, variables: String): String =
+ withContext(Dispatchers.IO) {
+ NativeBridge.gatewayQuery(
+ operation,
+ hash,
+ variables,
+ java.util.Locale.getDefault().language,
+ keys.appVersion,
+ )
+ }
+
+ private companion object {
+ const val TAG = "SquareGateway"
+
+ /**
+ * What one request takes.
+ *
+ * Two hundred whole tracks came back in half a second on a phone, so a
+ * collection of two thousand is ten requests. The Web API's own
+ * maximum for the same lists is a hundred, and fifty for the library.
+ */
+ const val PAGE = 200
+ }
+}
diff --git a/app/src/main/java/dev/lelonio/square/data/GatewayTracks.kt b/app/src/main/java/dev/lelonio/square/data/GatewayTracks.kt
new file mode 100644
index 0000000..2db0ac3
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/GatewayTracks.kt
@@ -0,0 +1,89 @@
+package dev.lelonio.square.data
+
+import org.json.JSONObject
+
+/**
+ * One track as Spotify's gateway writes it, whatever list it came out of.
+ *
+ * The library's saved tracks and a playlist's contents are different queries
+ * answering different shapes, but the track inside them is the same object with
+ * the same fields, so reading it belongs in one place.
+ *
+ * Forgiving on purpose, like everything else that reads this gateway: an item
+ * with a shape nobody recognises is skipped rather than treated as a failure.
+ */
+internal object GatewayTracks {
+
+ /**
+ * Null for anything that is not a playable track: a podcast episode, a
+ * local file, something this market has no licensed copy of. Keeping those
+ * would put items in the queue that the engine could only skip.
+ */
+ fun track(data: JSONObject?, addedAt: String?): CatalogTrack? {
+ if (data == null) return null
+ val uri = data.optString("uri").takeIf { it.startsWith("spotify:track:") } ?: return null
+ if (data.optJSONObject("playability")?.optBoolean("playable", true) == false) return null
+
+ val artists = data.optJSONObject("artists")?.optJSONArray("items")
+ val credited = buildList {
+ for (index in 0 until (artists?.length() ?: 0)) {
+ val artist = artists?.optJSONObject(index) ?: continue
+ val name = artist.optJSONObject("profile")?.optString("name").orEmpty()
+ if (name.isEmpty()) continue
+ add(CatalogArtist(name, artist.optString("uri").takeIf { it.isNotEmpty() }))
+ }
+ }
+
+ val album = data.optJSONObject("albumOfTrack")
+
+ return CatalogTrack(
+ uri = uri,
+ name = data.optString("name"),
+ artist = credited.joinToString(", ") { it.name },
+ artistUri = credited.firstOrNull()?.uri,
+ artists = credited,
+ album = album?.optString("name").orEmpty(),
+ // The library calls it `duration` and a playlist calls it
+ // `trackDuration`, for the same number.
+ durationMs = (data.optJSONObject("duration") ?: data.optJSONObject("trackDuration"))
+ ?.optLong("totalMilliseconds") ?: 0L,
+ explicit = data.optJSONObject("contentRating")?.optString("label") == "EXPLICIT",
+ artworkUrl = cover(album),
+ addedAt = addedAt,
+ )
+ }
+
+ /**
+ * The middle cover, or whatever there is.
+ *
+ * Three sizes come back, in no particular order. A row shows a thumbnail,
+ * so this takes the 300px one when it is there rather than the first named.
+ */
+ private fun cover(album: JSONObject?): String? {
+ val sources = album?.optJSONObject("coverArt")?.optJSONArray("sources") ?: return null
+ var fallback: String? = null
+ for (index in 0 until sources.length()) {
+ val source = sources.optJSONObject(index) ?: continue
+ val url = source.optString("url").takeIf { it.isNotEmpty() } ?: continue
+ if (source.optInt("width") == PREFERRED_COVER) return url
+ if (fallback == null) fallback = url
+ }
+ return fallback
+ }
+
+ /** What the rows are drawn at; see [cover]. */
+ private const val PREFERRED_COVER = 300
+}
+
+/**
+ * A page of a list read from the gateway.
+ *
+ * @param nextOffset where the next page starts, or null when this was the last.
+ * @param total how many the list holds, which is what says whether what is on
+ * screen is all of it.
+ */
+data class GatewayPage(
+ val tracks: List<CatalogTrack>,
+ val nextOffset: Int?,
+ val total: Int,
+)
diff --git a/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt b/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt
index 1e5e4b5..0ac454a 100644
--- a/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt
+++ b/app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt
@@ -37,6 +37,9 @@ class PathfinderKeys(context: Context) {
/** The same for `getLikedSongs`; see SavedTracks. */
val likedSongs: String get() = prefs.getString(KEY_LIKED, null) ?: DEFAULT_LIKED
+ /** And for `fetchPlaylistContents`; see PlaylistContents. */
+ val playlist: String get() = prefs.getString(KEY_PLAYLIST, null) ?: DEFAULT_PLAYLIST
+
/** The web client version the gateway is told about. */
val appVersion: String get() = prefs.getString(KEY_VERSION, null) ?: DEFAULT_VERSION
@@ -61,6 +64,8 @@ class PathfinderKeys(context: Context) {
?.let { edit.putString(KEY_HOME, it) }
body.optString("likedSongs").takeIf { it.length == HASH_LENGTH }
?.let { edit.putString(KEY_LIKED, it) }
+ body.optString("playlist").takeIf { it.length == HASH_LENGTH }
+ ?.let { edit.putString(KEY_PLAYLIST, it) }
body.optString("appVersion").takeIf { it.isNotEmpty() }
?.let { edit.putString(KEY_VERSION, it) }
edit.apply()
@@ -73,6 +78,7 @@ class PathfinderKeys(context: Context) {
const val FILE_NAME = "square_pathfinder"
const val KEY_HOME = "home"
const val KEY_LIKED = "liked_songs"
+ const val KEY_PLAYLIST = "playlist"
const val KEY_VERSION = "app_version"
const val KEY_CHECKED = "checked_at"
@@ -91,6 +97,8 @@ class PathfinderKeys(context: Context) {
"76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a"
const val DEFAULT_LIKED =
"c2c53c28f71da143c0753c22dc84d98b315cb4275472ea5a597c29338ae20b23"
+ const val DEFAULT_PLAYLIST =
+ "86dde7b9d9356e2369414647cf6950cfed96e778e129cfdfc99aea6c1613b3b0"
const val DEFAULT_VERSION = "1.2.97.155.g5dd0dcaf-development"
}
}
diff --git a/app/src/main/java/dev/lelonio/square/data/PlaylistContents.kt b/app/src/main/java/dev/lelonio/square/data/PlaylistContents.kt
new file mode 100644
index 0000000..b5fa55c
--- /dev/null
+++ b/app/src/main/java/dev/lelonio/square/data/PlaylistContents.kt
@@ -0,0 +1,54 @@
+package dev.lelonio.square.data
+
+import org.json.JSONObject
+
+/**
+ * A playlist's tracks, read from Spotify's own gateway.
+ *
+ * The same list the Web API answers for, from the endpoint Spotify's own web
+ * player uses, and the difference is who pays: the Web API is metered against
+ * an application the listener has to register for themselves, while this is
+ * answered for anyone who is logged in. It also comes two hundred at a time
+ * rather than a hundred, and unlike the access point's context it arrives with
+ * the whole track in it rather than a URI to look up one by one.
+ *
+ * Forgiving in the same way [SavedTracks] is, and for the same reason.
+ */
+object PlaylistContents {
+
+ /** Null when the answer was not one of these — a retired query hash, a 401. */
+ fun parse(json: String): GatewayPage? = runCatching {
+ val content = JSONObject(json)
+ .getJSONObject("data")
+ .getJSONObject("playlistV2")
+ .getJSONObject("content")
+
+ val items = content.getJSONArray("items")
+ val read = (0 until items.length()).mapNotNull { index ->
+ val item = items.optJSONObject(index) ?: return@mapNotNull null
+ // Episodes come through here too, wrapped in a different type, and
+ // GatewayTracks turns down anything without a track URI anyway.
+ GatewayTracks.track(
+ item.optJSONObject("itemV2")?.optJSONObject("data"),
+ addedAt = item.optJSONObject("addedAt")
+ ?.optString("isoString")
+ ?.takeIf { it.isNotEmpty() },
+ )
+ }
+
+ val paging = content.optJSONObject("pagingInfo")
+ val offset = paging?.optInt("offset") ?: 0
+ val total = content.optInt("totalCount", offset + items.length())
+ val next = offset + items.length()
+
+ GatewayPage(
+ tracks = read,
+ // Counted rather than announced: this page says where it starts
+ // and how long the list is, and nothing about what follows it. An
+ // empty page ends the read whatever the total claims, or a playlist
+ // whose count is wrong would be read for ever.
+ nextOffset = next.takeIf { items.length() > 0 && it < total },
+ total = total,
+ )
+ }.getOrNull()
+}
diff --git a/app/src/main/java/dev/lelonio/square/data/SavedTracks.kt b/app/src/main/java/dev/lelonio/square/data/SavedTracks.kt
index cf0dbea..c7926b4 100644
--- a/app/src/main/java/dev/lelonio/square/data/SavedTracks.kt
+++ b/app/src/main/java/dev/lelonio/square/data/SavedTracks.kt
@@ -19,19 +19,8 @@ import org.json.JSONObject
*/
object SavedTracks {
- /**
- * @param nextOffset where the next page starts, or null at the end.
- * @param total how many the account has, which is what says whether the
- * list on screen is all of them.
- */
- data class Page(
- val tracks: List<CatalogTrack>,
- val nextOffset: Int?,
- val total: Int,
- )
-
/** Null when the answer was not one of these — a retired query hash, a 401. */
- fun parse(json: String): Page? = runCatching {
+ fun parse(json: String): GatewayPage? = runCatching {
val tracks = JSONObject(json)
.getJSONObject("data")
.getJSONObject("me")
@@ -40,10 +29,17 @@ object SavedTracks {
val items = tracks.getJSONArray("items")
val read = (0 until items.length()).mapNotNull { index ->
- track(items.optJSONObject(index) ?: return@mapNotNull null)
+ GatewayTracks.track(
+ items.optJSONObject(index)?.optJSONObject("track")?.optJSONObject("data"),
+ // The gateway does not carry the date a track was saved. The
+ // pages arrive newest first, which is what the "recently
+ // added" order shows anyway, and sorting by a field that is
+ // null throughout leaves that order alone.
+ addedAt = null,
+ )
}
- Page(
+ GatewayPage(
tracks = read,
// Null on the last page, and null is how the caller knows it is
// the last. Present-but-null, at that: the key is always there, so
@@ -54,65 +50,4 @@ object SavedTracks {
total = tracks.optInt("totalCount", read.size),
)
}.getOrNull()
-
- private fun track(item: JSONObject): CatalogTrack? {
- val data = item.optJSONObject("track")?.optJSONObject("data") ?: return null
- val uri = data.optString("uri").takeIf { it.startsWith("spotify:track:") } ?: return null
-
- // Same rule as every other list: something the engine could only skip
- // has no business being in the queue.
- val playable = data.optJSONObject("playability")?.optBoolean("playable", true) ?: true
- if (!playable) return null
-
- val artists = data.optJSONObject("artists")?.optJSONArray("items")
- val credited = buildList {
- for (index in 0 until (artists?.length() ?: 0)) {
- val artist = artists?.optJSONObject(index) ?: continue
- val name = artist.optJSONObject("profile")?.optString("name").orEmpty()
- if (name.isEmpty()) continue
- add(CatalogArtist(name, artist.optString("uri").takeIf { it.isNotEmpty() }))
- }
- }
-
- val album = data.optJSONObject("albumOfTrack")
-
- return CatalogTrack(
- uri = uri,
- name = data.optString("name"),
- artist = credited.joinToString(", ") { it.name },
- artistUri = credited.firstOrNull()?.uri,
- artists = credited,
- album = album?.optString("name").orEmpty(),
- durationMs = data.optJSONObject("duration")?.optLong("totalMilliseconds") ?: 0L,
- explicit = data.optJSONObject("contentRating")?.optString("label") == "EXPLICIT",
- artworkUrl = cover(album),
- // The gateway does not carry the date a track was saved. The pages
- // arrive newest first, which is what the "recently added" order
- // shows anyway, and sorting by a field that is null throughout
- // leaves that order alone.
- addedAt = null,
- )
- }
-
- /**
- * The middle cover, or whatever there is.
- *
- * Three sizes come back, in no particular order. A row shows a thumbnail
- * and the sheet behind the player wants something bigger, so this takes the
- * 300px one when it is there rather than the first named.
- */
- private fun cover(album: JSONObject?): String? {
- val sources = album?.optJSONObject("coverArt")?.optJSONArray("sources") ?: return null
- var fallback: String? = null
- for (index in 0 until sources.length()) {
- val source = sources.optJSONObject(index) ?: continue
- val url = source.optString("url").takeIf { it.isNotEmpty() } ?: continue
- if (source.optInt("width") == PREFERRED_COVER) return url
- if (fallback == null) fallback = url
- }
- return fallback
- }
-
- /** What the rows are drawn at; see [cover]. */
- private const val PREFERRED_COVER = 300
}
diff --git a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
index 1e80933..a0a23a9 100644
--- a/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
+++ b/app/src/main/java/dev/lelonio/square/nativecore/NativeBridge.kt
@@ -273,6 +273,21 @@ object NativeBridge {
appVersion: String,
): String = nativeHomeFeed(timeZone, language, hash, appVersion)
+ /**
+ * Any of the gateway's persisted queries, named and addressed by hash.
+ *
+ * The same endpoint and the same fragility as [homeFeed]: the hash is of a
+ * query Spotify already knows, and it retires them when its web client is
+ * rebuilt. Callers must be able to carry on with nothing. Blocking.
+ */
+ fun gatewayQuery(
+ operation: String,
+ hash: String,
+ variables: String,
+ language: String,
+ appVersion: String,
+ ): String = nativeGatewayQuery(operation, hash, variables, language, appVersion)
+
/**
* The account's saved tracks, as raw JSON from Spotify's gateway.
*
@@ -422,6 +437,13 @@ object NativeBridge {
hash: String,
appVersion: String,
): String
+ private external fun nativeGatewayQuery(
+ operation: String,
+ hash: String,
+ variables: String,
+ language: String,
+ appVersion: String,
+ ): String
private external fun nativeLikedSongs(
variables: String,
language: String,
diff --git a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
index 2158105..aeff2ae 100644
--- a/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
+++ b/app/src/main/java/dev/lelonio/square/ui/MainViewModel.kt
@@ -20,7 +20,7 @@ import dev.lelonio.square.data.HomeShelf
import dev.lelonio.square.data.SpotifyHome
import dev.lelonio.square.data.CatalogTrack
import dev.lelonio.square.data.ContextCacheStore
-import dev.lelonio.square.data.SavedTracks
+import dev.lelonio.square.data.GatewayPage
import dev.lelonio.square.data.SearchItem
import dev.lelonio.square.data.TransferRequestDto
import dev.lelonio.square.data.SearchResults
@@ -1799,11 +1799,11 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
* growing one.
*/
private suspend fun loadContextInto(base: PlaylistState, uri: String, showProgress: Boolean) {
- val viaWebApi = runCatching { webApiTracks(base, uri, showProgress) }
- .onFailure { android.util.Log.w(TAG, "web api playlist read failed: ${describe(it)}") }
+ val paged = runCatching { pagedTracks(base, uri, showProgress) }
+ .onFailure { android.util.Log.w(TAG, "paged read failed: ${describe(it)}") }
.getOrNull()
- val tracks = viaWebApi ?: accessPointTracks(base, uri, showProgress)
+ val tracks = paged ?: accessPointTracks(base, uri, showProgress)
publishPlaylist(base.copy(tracks = tracks, loadingMore = false))
@@ -1824,19 +1824,64 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}.getOrNull()
}
- /** Null when this is not something the Web API can page through. */
- private suspend fun webApiTracks(
+ /**
+ * A playlist or the saved tracks, paged.
+ *
+ * Spotify's gateway first and the Web API behind it, for both. The gateway
+ * is the endpoint the web player itself reads and it answers for anyone
+ * logged in, two hundred whole tracks a request; the Web API is metered
+ * against an application the listener has to register for themselves, and
+ * gives a hundred. So the Web API is what is left when a query hash has
+ * been retired, rather than the way in.
+ *
+ * Null when this is neither of those — an album, a list from the access
+ * point — which is the caller's cue to resolve it the other way.
+ */
+ private suspend fun pagedTracks(
base: PlaylistState,
uri: String,
showProgress: Boolean,
): List<CatalogTrack>? {
- // Liked Songs first, and without the registered-application check: it
- // comes from the gateway, which asks only that somebody is logged in.
- if (uri.endsWith(":collection")) return savedTracks(base, showProgress)
- if (!container.webApi.isReady) return null
+ if (uri.endsWith(":collection")) {
+ gatewayTracks(base, showProgress) { offset ->
+ container.gateway.savedTracks(offset)
+ }?.let { return it }
+ // Nothing else knows this list: the access point refuses the
+ // collection as a context, so without the gateway and without a
+ // registered application there is no answer to give.
+ check(container.webApi.isReady) { string(R.string.liked_songs_failed) }
+ return webApiSavedTracks(base, showProgress)
+ }
+
if (!uri.startsWith("spotify:playlist:")) return null
- val id = uri.substringAfterLast(':')
+ gatewayTracks(base, showProgress) { offset ->
+ container.gateway.playlistTracks(uri, offset)
+ }?.let { return it }
+ if (!container.webApi.isReady) return null
+ return webApiPlaylistTracks(base, uri, showProgress)
+ }
+
+ /** The shared gateway reader, with each page published as it lands. */
+ private suspend fun gatewayTracks(
+ base: PlaylistState,
+ showProgress: Boolean,
+ page: suspend (offset: Int) -> GatewayPage?,
+ ): List<CatalogTrack>? = container.gateway.readAll(
+ onPage = if (showProgress) {
+ { tracks, more -> publishPlaylist(base.copy(tracks = tracks, loadingMore = more)) }
+ } else {
+ null
+ },
+ page = page,
+ )
+ /** A playlist through the listener's own application; see [pagedTracks]. */
+ private suspend fun webApiPlaylistTracks(
+ base: PlaylistState,
+ uri: String,
+ showProgress: Boolean,
+ ): List<CatalogTrack> {
+ val id = uri.substringAfterLast(':')
val loaded = mutableListOf<CatalogTrack>()
var offset = 0
while (true) {
@@ -1861,28 +1906,11 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}
}
- /**
- * Liked Songs, fifty complete tracks a request.
- *
- * It is not a playlist and the access point will not resolve it as a
- * context, so it used to fall through to [accessPointTracks] — which asks
- * for the track list and then spends one round trip per track turning each
- * URI into a name. Two thousand saved songs were two thousand requests:
- * minutes of them, the account's quota with them, and a page that looked
- * like it would never finish. `me/tracks` answers with whole tracks and
- * caps at fifty a page, so the same two thousand are forty requests.
- */
- private suspend fun savedTracks(
+ /** Liked Songs through the listener's own application; see [pagedTracks]. */
+ private suspend fun webApiSavedTracks(
base: PlaylistState,
showProgress: Boolean,
): List<CatalogTrack> {
- gatewaySavedTracks(base, showProgress)?.let { return it }
-
- // The gateway said nothing usable — a retired query hash, most likely.
- // The Web API knows the same list, fifty at a time, for a listener who
- // has registered an application of their own.
- check(container.webApi.isReady) { string(R.string.liked_songs_failed) }
-
val loaded = mutableListOf<CatalogTrack>()
var offset = 0
while (true) {
@@ -1905,68 +1933,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
}
}
- /**
- * Liked Songs from Spotify's own gateway, two hundred at a time.
- *
- * The list nobody else will answer for: the access point refuses the
- * collection as a context, and the Web API wants the listener to have
- * registered an application. This is the same gateway the personalised
- * home comes from, so it costs nothing that the home page does not already
- * cost, and it hands back whole tracks rather than URIs to look up one by
- * one.
- *
- * Null rather than an exception when the gateway will not answer: the
- * query is addressed by a hash Spotify retires on its own schedule, and
- * the caller has a second way to read the same list.
- */
- private suspend fun gatewaySavedTracks(
- base: PlaylistState,
- showProgress: Boolean,
- ): List<CatalogTrack>? {
- val keys = container.pathfinderKeys
- keys.refresh()
- val language = java.util.Locale.getDefault().language
-
- val loaded = mutableListOf<CatalogTrack>()
- var offset = 0
- while (true) {
- val page = runCatching {
- val raw = withContext(Dispatchers.IO) {
- NativeBridge.likedSongs(
- """{"offset":$offset,"limit":$GATEWAY_LIBRARY_PAGE}""",
- language,
- keys.likedSongs,
- keys.appVersion,
- )
- }
- SavedTracks.parse(raw)
- }
- .onFailure { android.util.Log.i(TAG, "no liked songs from the gateway: ${describe(it)}") }
- .getOrNull()
- // A page that will not come back gives up the whole read, part
- // way through as much as at the start. What is returned here is
- // written to the cache and stamped as the list, so half of Liked
- // Songs would be remembered as all of it.
- if (page == null) return null
-
- loaded += page.tracks
- val next = page.nextOffset
- if (showProgress) {
- publishPlaylist(base.copy(
- tracks = loaded.toList(),
- loadingMore = next != null,
- ))
- }
- // Counted as well as followed: a page that names itself as its own
- // successor would otherwise be read for ever.
- if (next == null || next <= offset || loaded.size >= page.total) {
- android.util.Log.i(TAG, "liked songs: ${loaded.size} of ${page.total} from the gateway")
- return loaded
- }
- offset = next
- }
- }
-
private suspend fun accessPointTracks(
base: PlaylistState,
uri: String,
@@ -2246,14 +2212,6 @@ class MainViewModel(app: Application) : AndroidViewModel(app) {
*/
const val WEB_API_LIBRARY_PAGE = 50
- /**
- * And what the gateway takes, which is four times either.
- *
- * Two hundred whole tracks came back in half a second on a phone, so
- * the account's whole collection is a handful of requests.
- */
- const val GATEWAY_LIBRARY_PAGE = 200
-
/** How many track lists to keep resolved; see contextCache. */
const val CONTEXT_CACHE_SIZE = 8
diff --git a/native/src/ffi.rs b/native/src/ffi.rs
index fb45e69..4100a8a 100644
--- a/native/src/ffi.rs
+++ b/native/src/ffi.rs
@@ -557,6 +557,27 @@ pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeHom
})
}
+/// Any of the gateway's persisted queries, by name and hash; raw JSON back.
+#[no_mangle]
+pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeGatewayQuery(
+ mut env: JNIEnv,
+ _class: JClass,
+ operation: JString,
+ hash: JString,
+ variables: JString,
+ language: JString,
+ app_version: JString,
+) -> jstring {
+ let operation = read_string(&mut env, &operation).unwrap_or_default();
+ let hash = read_string(&mut env, &hash).unwrap_or_default();
+ let variables = read_string(&mut env, &variables).unwrap_or_default();
+ let language = read_string(&mut env, &language).unwrap_or_default();
+ let app_version = read_string(&mut env, &app_version).unwrap_or_default();
+ guard_string(&mut env, "GatewayQuery", || {
+ crate::pathfinder::gateway(&operation, &hash, &variables, &language, &app_version)
+ })
+}
+
/// The account's saved tracks, as raw JSON from Spotify's gateway.
#[no_mangle]
pub extern "system" fn Java_dev_lelonio_square_nativecore_NativeBridge_nativeLikedSongs(
diff --git a/native/src/pathfinder.rs b/native/src/pathfinder.rs
index 2c5a6c4..1f17f2c 100644
--- a/native/src/pathfinder.rs
+++ b/native/src/pathfinder.rs
@@ -42,7 +42,7 @@ const APP_VERSION: &str = "1.2.97.155.g5dd0dcaf-development";
/// One persisted query, by name and hash. See the note at the top of the file.
struct Query {
- name: &'static str,
+ name: String,
hash: String,
}
@@ -57,7 +57,7 @@ const HOME_HASH: &str = "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e845
const LIKED_HASH: &str = "c2c53c28f71da143c0753c22dc84d98b315cb4275472ea5a597c29338ae20b23";
/// Runs one persisted query and returns the raw JSON body.
-async fn query(
+async fn query_with(
session: &Session,
query: &Query,
variables: String,
@@ -181,7 +181,7 @@ pub fn home(
);
let home = Query {
- name: "home",
+ name: "home".to_string(),
hash: if hash.is_empty() {
HOME_HASH.to_string()
} else {
@@ -190,7 +190,36 @@ pub fn home(
};
let app_version = app_version.to_string();
- handle.block_on(async move { query(&session, &home, variables, &language, &app_version).await })
+ handle.block_on(async move { query_with(&session, &home, variables, &language, &app_version).await })
+}
+
+/// Any persisted query the caller can name, by hash.
+///
+/// The gateway is one endpoint serving dozens of queries, and every one of
+/// them is the same request with a different name, hash and set of variables.
+/// Named by the caller so a query can be added, or its hash replaced, without
+/// anything on this side changing; see PathfinderKeys.
+pub fn gateway(
+ operation: &str,
+ hash: &str,
+ variables: &str,
+ language: &str,
+ app_version: &str,
+) -> engine::EngineResult<String> {
+ if operation.is_empty() || hash.is_empty() {
+ return Err("a gateway query needs a name and a hash".into());
+ }
+ let handle = engine::runtime_handle()?;
+ let session = engine::with_session(|session| session.clone())?;
+ let language = if language.is_empty() { "en" } else { language }.to_string();
+ let variables = if variables.is_empty() { "{}" } else { variables }.to_string();
+ let app_version = app_version.to_string();
+ let query = Query {
+ name: operation.to_string(),
+ hash: hash.to_string(),
+ };
+
+ handle.block_on(async move { query_with(&session, &query, variables, &language, &app_version).await })
}
/// The account's saved tracks, as the web player's library page reads them.
@@ -210,7 +239,7 @@ pub fn liked_songs(
let language = if language.is_empty() { "en" } else { language }.to_string();
let liked = Query {
- name: "getLikedSongs",
+ name: "getLikedSongs".to_string(),
hash: if hash.is_empty() {
LIKED_HASH.to_string()
} else {
@@ -221,6 +250,6 @@ pub fn liked_songs(
let variables = if variables.is_empty() { "{}" } else { variables }.to_string();
handle.block_on(async move {
- query(&session, &liked, variables, &language, &app_version).await
+ query_with(&session, &liked, variables, &language, &app_version).await
})
}
diff --git a/pathfinder.json b/pathfinder.json
index 1fa0e27..62d5043 100644
--- a/pathfinder.json
+++ b/pathfinder.json
@@ -2,5 +2,6 @@
"_comment": "Identifiers Spotify's GraphQL gateway wants from a caller. Read by Square at runtime so a change here needs no new version of the app: see app/src/main/java/dev/lelonio/square/data/PathfinderKeys.kt. Both values are retired by Spotify whenever it rebuilds its web client, and both are readable from a network capture of open.spotify.com.",
"home": "76243c78b0e20ecdbe41b794dec8cbe73f75e585b0a7201b8d2e84578412847a",
"likedSongs": "c2c53c28f71da143c0753c22dc84d98b315cb4275472ea5a597c29338ae20b23",
+ "playlist": "86dde7b9d9356e2369414647cf6950cfed96e778e129cfdfc99aea6c1613b3b0",
"appVersion": "1.2.97.155.g5dd0dcaf-development"
}