feat: base discovery on the whole mood collection
- seed from every artist (weighted by song count) and every album, not one track - walk Deezer's graph: artist top tracks, related artists, album deep cuts - score by artist frequency, collection album, catalog affinity and hint text - penalise repeated artists so a 30-slot mood spans artists, not one - refresh the pool when a song is added instead of waiting for refreshHours - skip discovery for a mood with no hint, genres or songs - extend tests and the smoke test with the artist graph
This commit is contained in:
1 parent
a78d359d0c
commit
62bcefad20
13 files changed
+911
-72
No files matched your search
@@ -6,6 +6,7 @@ import java.net.http.HttpClient
|
||||
import java.net.http.HttpRequest
|
||||
import java.net.http.HttpResponse
|
||||
import java.time.Duration
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import kotlinx.serialization.SerialName
|
||||
import kotlinx.serialization.Serializable
|
||||
import kotlinx.serialization.json.Json
|
||||
@@ -16,8 +17,8 @@ import kotlinx.serialization.json.jsonPrimitive
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
/**
|
||||
* Deezer's public catalog API. No user token is needed for search or track
|
||||
* metadata; the ARL is only used by the downloader.
|
||||
* Deezer's public catalog API. No user token is needed for search, track
|
||||
* metadata or the similarity graph; the ARL is only used by the downloader.
|
||||
*
|
||||
* Deezer returns canonical tracks with a square album cover, which is also what
|
||||
* the indexer embeds into the downloaded MP3.
|
||||
@@ -39,6 +40,10 @@ class DeezerSearch(
|
||||
.followRedirects(HttpClient.Redirect.NORMAL)
|
||||
.build()
|
||||
|
||||
/** Artist and album lookups are stable for a release, so cache them. */
|
||||
private val artistIds = ConcurrentHashMap<String, Lookup<Long>>()
|
||||
private val albumRefs = ConcurrentHashMap<String, Lookup<DeezerAlbumRefDto>>()
|
||||
|
||||
override fun search(query: String, limit: Int): List<TrackCandidate> {
|
||||
val count = limit.coerceIn(1, MAX_RESULTS)
|
||||
val url = "$apiBase/search/track?q=${encode(query)}&limit=$count&output=json"
|
||||
@@ -54,6 +59,147 @@ class DeezerSearch(
|
||||
return parseDeezerTrack(get("$apiBase/track/$trackId"))
|
||||
}
|
||||
|
||||
/**
|
||||
* Walks Deezer's own graph from the entire collection:
|
||||
*
|
||||
* 1. the most represented artists → their top tracks;
|
||||
* 2. those artists' related artists → their top tracks;
|
||||
* 3. the collection's albums → their deep cuts.
|
||||
*
|
||||
* A mood with 40 songs by 6 artists therefore still yields fresh material
|
||||
* even after the obvious artist hits are already in the mood. Every request
|
||||
* is best-effort; a blocked endpoint does not fail the pass.
|
||||
*/
|
||||
override fun discover(seeds: DiscoverySeeds, limit: Int): List<TrackCandidate> {
|
||||
if (seeds.isEmpty()) return emptyList()
|
||||
val found = LinkedHashMap<String, TrackCandidate>()
|
||||
|
||||
val seedArtists = seeds.artists
|
||||
.take(MAX_SEED_ARTISTS)
|
||||
.mapNotNull { seed -> artistId(seed.name)?.let { seed to it } }
|
||||
seedArtists.forEach { (_, id) ->
|
||||
artistTopTracks(id, SEED_TOP_LIMIT).forEach {
|
||||
add(found, it, SEED_TOP_AFFINITY)
|
||||
}
|
||||
}
|
||||
|
||||
val related = LinkedHashSet<Long>()
|
||||
seedArtists.take(MAX_RELATED_SEEDS).forEach { (_, id) ->
|
||||
related += relatedArtists(id, MAX_RELATED_PER_SEED)
|
||||
}
|
||||
val seedIds = seedArtists.map { it.second }.toSet()
|
||||
related.asSequence()
|
||||
.filterNot { it in seedIds }
|
||||
.take(MAX_RELATED_ARTISTS)
|
||||
.forEach { id ->
|
||||
artistTopTracks(id, RELATED_TOP_LIMIT).forEach {
|
||||
add(found, it, RELATED_AFFINITY)
|
||||
}
|
||||
}
|
||||
|
||||
seeds.albums
|
||||
.take(MAX_SEED_ALBUMS)
|
||||
.mapNotNull(::albumRef)
|
||||
.forEach { album ->
|
||||
albumTracks(album, ALBUM_TRACK_LIMIT).forEach {
|
||||
add(found, it, ALBUM_AFFINITY)
|
||||
}
|
||||
}
|
||||
|
||||
return found.values.take(limit.coerceAtLeast(1))
|
||||
}
|
||||
|
||||
/** Keeps the highest weight when the same track comes from two edges. */
|
||||
private fun add(
|
||||
into: MutableMap<String, TrackCandidate>,
|
||||
candidate: TrackCandidate,
|
||||
affinity: Double,
|
||||
) {
|
||||
if (candidate.externalId.isEmpty()) return
|
||||
val existing = into[candidate.externalId]
|
||||
if (existing == null || existing.affinity < affinity) {
|
||||
into[candidate.externalId] = candidate.copy(affinity = affinity)
|
||||
}
|
||||
}
|
||||
|
||||
private fun artistId(name: String): Long? = lookup(
|
||||
cache = artistIds,
|
||||
key = matchKey(name),
|
||||
request = "$apiBase/search/artist?q=${encode(name)}&limit=$ARTIST_LOOKUP_LIMIT&output=json",
|
||||
) { body ->
|
||||
val parsed = json.decodeFromString<DeezerArtistListResponse>(body)
|
||||
parsed.data.firstOrNull { artistMatches(it.name, name) && it.id > 0 }?.id
|
||||
?: parsed.data.firstOrNull { it.id > 0 }?.id
|
||||
}
|
||||
|
||||
private fun albumRef(seed: SeedAlbum): DeezerAlbumRefDto? {
|
||||
val titleKey = matchKey(seed.title)
|
||||
if (titleKey.isEmpty()) return null
|
||||
val artistKey = matchKey(seed.artist)
|
||||
return lookup(
|
||||
cache = albumRefs,
|
||||
key = "$titleKey|$artistKey",
|
||||
request = "$apiBase/search/album?q=${encode("${seed.title} ${seed.artist}")}" +
|
||||
"&limit=$ALBUM_LOOKUP_LIMIT&output=json",
|
||||
) { body ->
|
||||
val parsed = json.decodeFromString<DeezerAlbumListResponse>(body)
|
||||
parsed.data.firstOrNull { album ->
|
||||
album.id > 0 &&
|
||||
matchKey(album.title) == titleKey &&
|
||||
(album.artist == null || artistMatches(album.artist.name, seed.artist))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolves a name to an id once. Misses are cached too, so a mood full of
|
||||
* obscure artists does not re-search them on every refresh.
|
||||
*/
|
||||
private inline fun <T : Any> lookup(
|
||||
cache: MutableMap<String, Lookup<T>>,
|
||||
key: String,
|
||||
request: String,
|
||||
crossinline parse: (String) -> T?,
|
||||
): T? {
|
||||
if (key.isEmpty()) return null
|
||||
cache[key]?.let { return it.value }
|
||||
val value = runCatching { parse(get(request)) }.getOrNull()
|
||||
cache[key] = Lookup(value)
|
||||
return value
|
||||
}
|
||||
|
||||
private fun relatedArtists(id: Long, limit: Int): List<Long> = runCatching {
|
||||
val body = get("$apiBase/artist/$id/related?limit=$limit&output=json")
|
||||
json.decodeFromString<DeezerArtistListResponse>(body)
|
||||
.data
|
||||
.map { it.id }
|
||||
.filter { it > 0 }
|
||||
}.getOrDefault(emptyList())
|
||||
|
||||
private fun artistTopTracks(id: Long, limit: Int): List<TrackCandidate> = runCatching {
|
||||
val body = get("$apiBase/artist/$id/top?limit=$limit&output=json")
|
||||
json.decodeFromString<DeezerTracksResponse>(body).data.mapNotNull { it.toCandidate() }
|
||||
}.getOrDefault(emptyList())
|
||||
|
||||
/**
|
||||
* `/album/{id}/tracks` omits the album object, so inject the album the
|
||||
* collection already gave us to keep the cover and the album name.
|
||||
*/
|
||||
private fun albumTracks(
|
||||
album: DeezerAlbumRefDto,
|
||||
limit: Int,
|
||||
): List<TrackCandidate> = runCatching {
|
||||
val body = get("$apiBase/album/${album.id}/tracks?limit=$limit&output=json")
|
||||
json.decodeFromString<DeezerTracksResponse>(body).data
|
||||
.mapNotNull { it.toCandidate() }
|
||||
.map { candidate ->
|
||||
candidate.copy(
|
||||
album = candidate.album ?: album.title.takeIf { it.isNotBlank() },
|
||||
thumbnail = candidate.thumbnail ?: album.cover(),
|
||||
)
|
||||
}
|
||||
}.getOrDefault(emptyList())
|
||||
|
||||
private fun get(url: String): String {
|
||||
throttle()
|
||||
val request = HttpRequest.newBuilder(URI.create(url))
|
||||
@@ -111,8 +257,31 @@ class DeezerSearch(
|
||||
}
|
||||
}
|
||||
|
||||
/** `value == null` is a cached miss. */
|
||||
private data class Lookup<T>(val value: T?)
|
||||
|
||||
private companion object {
|
||||
const val MAX_RESULTS = 25
|
||||
|
||||
/** Bounds one discovery pass to roughly twenty throttled requests. */
|
||||
const val MAX_SEED_ARTISTS = 5
|
||||
const val MAX_RELATED_SEEDS = 2
|
||||
const val MAX_RELATED_PER_SEED = 8
|
||||
const val MAX_RELATED_ARTISTS = 6
|
||||
const val MAX_SEED_ALBUMS = 2
|
||||
const val SEED_TOP_LIMIT = 25
|
||||
const val RELATED_TOP_LIMIT = 10
|
||||
const val ALBUM_TRACK_LIMIT = 40
|
||||
const val ARTIST_LOOKUP_LIMIT = 5
|
||||
const val ALBUM_LOOKUP_LIMIT = 5
|
||||
|
||||
/** A hit by an artist already in the mood; the scorer adds the rest. */
|
||||
const val SEED_TOP_AFFINITY = 0.5
|
||||
/** A hit by an artist Deezer considers similar to the collection. */
|
||||
const val RELATED_AFFINITY = 1.5
|
||||
/** Another track from an album already in the mood. */
|
||||
const val ALBUM_AFFINITY = 1.0
|
||||
|
||||
const val MIN_REQUEST_INTERVAL_NANOS = 120_000_000L
|
||||
val THROTTLE_LOCK = Any()
|
||||
|
||||
@@ -152,6 +321,29 @@ internal fun parseDeezerTrack(json: String): TrackCandidate? {
|
||||
@Serializable
|
||||
private data class DeezerSearchResponse(val data: List<DeezerTrackDto> = emptyList())
|
||||
|
||||
@Serializable
|
||||
private data class DeezerTracksResponse(val data: List<DeezerTrackDto> = emptyList())
|
||||
|
||||
@Serializable
|
||||
private data class DeezerArtistListResponse(val data: List<DeezerArtistRefDto> = emptyList())
|
||||
|
||||
@Serializable
|
||||
private data class DeezerArtistRefDto(val id: Long = 0, val name: String = "")
|
||||
|
||||
@Serializable
|
||||
private data class DeezerAlbumListResponse(val data: List<DeezerAlbumRefDto> = emptyList())
|
||||
|
||||
@Serializable
|
||||
private data class DeezerAlbumRefDto(
|
||||
val id: Long = 0,
|
||||
val title: String = "",
|
||||
val artist: DeezerArtistDto? = null,
|
||||
@SerialName("cover_big") val coverBig: String? = null,
|
||||
@SerialName("cover_xl") val coverXl: String? = null,
|
||||
) {
|
||||
fun cover(): String? = coverBig ?: coverXl
|
||||
}
|
||||
|
||||
@Serializable
|
||||
private data class DeezerTrackDto(
|
||||
val id: Long = 0,
|
||||
|
||||
Reference in new issue
Block a user