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
@@ -105,6 +105,7 @@ fun main() {
|
||||
indexer = indexer,
|
||||
gc = gc,
|
||||
tempDir = config.downloadDir,
|
||||
onSongAttached = topUpService::trigger,
|
||||
)
|
||||
if (config.defaultSource == MusicSource.DEEZER && config.deezerArl == null) {
|
||||
log.warn("DEFAULT_SOURCE is deezer but DEEZER_ARL is empty: downloads will fail")
|
||||
|
||||
@@ -117,6 +117,14 @@ object MoodRepository {
|
||||
?.let { moodRecord(it) }
|
||||
}
|
||||
|
||||
/** Owner-agnostic lookup for background work that only has a mood id. */
|
||||
fun byId(id: String): MoodRecord? = transaction {
|
||||
MoodsTable.selectAll()
|
||||
.where { MoodsTable.id eq id }
|
||||
.singleOrNull()
|
||||
?.let { moodRecord(it) }
|
||||
}
|
||||
|
||||
fun create(
|
||||
owner: String,
|
||||
name: String,
|
||||
@@ -590,7 +598,10 @@ object MoodRepository {
|
||||
.map { moodRecord(it) }
|
||||
.filter { mood ->
|
||||
mood.recommendationCount > 0 &&
|
||||
isStale(mood.lastDiscoveryAt, mood.refreshHours, now)
|
||||
(
|
||||
isStale(mood.lastDiscoveryAt, mood.refreshHours, now) ||
|
||||
songsAddedSince(mood.id, mood.lastDiscoveryAt)
|
||||
)
|
||||
}
|
||||
.take(limit.coerceAtLeast(1))
|
||||
}
|
||||
@@ -613,7 +624,23 @@ object MoodRepository {
|
||||
}
|
||||
|
||||
fun isDiscoveryDue(mood: MoodRecord, now: Instant = Instant.now()): Boolean =
|
||||
isStale(mood.lastDiscoveryAt, mood.refreshHours, now)
|
||||
isStale(mood.lastDiscoveryAt, mood.refreshHours, now) ||
|
||||
songsAddedSince(mood.id, mood.lastDiscoveryAt)
|
||||
|
||||
/**
|
||||
* The collection is the discovery seed, so a song added after the last pass
|
||||
* makes the hidden candidate pool stale, even when every slot is filled.
|
||||
* Both timestamps come from [Instant.toString], so the lexical comparison is
|
||||
* also chronological.
|
||||
*/
|
||||
fun songsAddedSince(moodId: String, since: String?): Boolean {
|
||||
if (since == null) return false
|
||||
return transaction {
|
||||
MoodSongsTable.selectAll()
|
||||
.where { MoodSongsTable.moodId eq moodId }
|
||||
.any { row -> row[MoodSongsTable.addedAt] > since }
|
||||
}
|
||||
}
|
||||
|
||||
// ------------------------------------------------------------ internal
|
||||
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.util.IdGenerator
|
||||
|
||||
data class DiscoveryOutcome(
|
||||
@@ -39,24 +40,54 @@ class DiscoveryService(
|
||||
|
||||
val catalog = catalogs.forSource(mood.source)
|
||||
val collection = MoodRepository.songRecords(mood.id)
|
||||
val seedArtists = collection.map { it.artist }.filter { it.isNotBlank() }
|
||||
val queries = buildQueries(mood, seedArtists)
|
||||
if (queries.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
return DiscoveryOutcome(0)
|
||||
val artists = artistsByFrequency(collection)
|
||||
val albums = albumsOf(collection)
|
||||
val seeds = DiscoverySeeds(
|
||||
artists = artists,
|
||||
albums = albums,
|
||||
hint = mood.query,
|
||||
genres = mood.genres,
|
||||
name = mood.name,
|
||||
)
|
||||
val queries = buildQueries(mood, artists.map { it.name })
|
||||
if (queries.isEmpty() && seeds.isEmpty()) {
|
||||
// A new mood with no hint, no genres and no songs has nothing to
|
||||
// base music on. Leave the slots empty instead of downloading
|
||||
// whatever the mood's name happens to match.
|
||||
log.debug("Skipping discovery for mood {}: no music signal yet", mood.name)
|
||||
return DiscoveryOutcome(0, skipped = true)
|
||||
}
|
||||
|
||||
// Queries are independent; running them together keeps a refresh within
|
||||
// the client's request timeout even when a catalog is slow.
|
||||
// Text queries and the catalog's similarity graph run together: the
|
||||
// graph knows about related artists and album deep cuts, which a title
|
||||
// search cannot express.
|
||||
val results = coroutineScope {
|
||||
queries.map { query ->
|
||||
async(Dispatchers.IO) {
|
||||
runCatching { catalog.search(query, QUERY_LIMIT) }
|
||||
val searches = queries.map { query ->
|
||||
async(Dispatchers.IO) { runCatching { catalog.search(query, QUERY_LIMIT) } }
|
||||
}
|
||||
val expansion = async(Dispatchers.IO) {
|
||||
if (seeds.isEmpty()) {
|
||||
emptyList()
|
||||
} else {
|
||||
runCatching { catalog.discover(seeds, DISCOVERY_LIMIT) }
|
||||
.onFailure {
|
||||
log.debug(
|
||||
"{} expansion failed for mood {}: {}",
|
||||
catalog.source,
|
||||
mood.name,
|
||||
it.message,
|
||||
)
|
||||
}
|
||||
.getOrDefault(emptyList())
|
||||
}
|
||||
}.awaitAll()
|
||||
}
|
||||
searches.awaitAll() to expansion.await()
|
||||
}
|
||||
val failures = results.filter { it.isFailure }
|
||||
if (failures.size == queries.size) {
|
||||
val searches = results.first
|
||||
val discovered = results.second
|
||||
val failures = searches.filter { it.isFailure }
|
||||
|
||||
if (searches.isNotEmpty() && failures.size == searches.size && discovered.isEmpty()) {
|
||||
val message = failures.firstNotNullOfOrNull { it.exceptionOrNull()?.message }
|
||||
?: "Search failed"
|
||||
log.warn("{} discovery failed for mood {}: {}", catalog.source, mood.name, message)
|
||||
@@ -65,9 +96,11 @@ class DiscoveryService(
|
||||
}
|
||||
|
||||
val candidates = LinkedHashMap<String, TrackCandidate>()
|
||||
results.mapNotNull { it.getOrNull() }.flatten().forEach { candidate ->
|
||||
if (isMusicLength(candidate.duration)) {
|
||||
candidates.putIfAbsent(candidate.externalId, candidate)
|
||||
(searches.mapNotNull { it.getOrNull() }.flatten() + discovered).forEach { candidate ->
|
||||
if (!isMusicLength(candidate.duration)) return@forEach
|
||||
val existing = candidates[candidate.externalId]
|
||||
if (existing == null || existing.affinity < candidate.affinity) {
|
||||
candidates[candidate.externalId] = candidate
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,28 +118,33 @@ class DiscoveryService(
|
||||
.toSet()
|
||||
|
||||
val moodText = listOfNotNull(mood.query, mood.description, mood.name).joinToString(" ")
|
||||
val artistWeights = artists
|
||||
.associate { matchKey(it.name) to it.songs.toDouble() }
|
||||
.filterKeys { it.isNotEmpty() }
|
||||
val albumKeys = albums
|
||||
.map { matchKey(it.title) }
|
||||
.filter { it.isNotEmpty() }
|
||||
.toSet()
|
||||
|
||||
val upserts = candidates.values
|
||||
.filterNot { it.externalId in excluded }
|
||||
.map { candidate ->
|
||||
candidate to scoreCandidate(candidate, seedArtists, moodText)
|
||||
}
|
||||
.sortedWith(
|
||||
compareByDescending<Pair<TrackCandidate, Double>> { it.second }
|
||||
.thenBy { it.first.title },
|
||||
val ranked = rankCandidates(
|
||||
candidates.values
|
||||
.filterNot { it.externalId in excluded }
|
||||
.map { candidate ->
|
||||
candidate to scoreCandidate(candidate, artistWeights, albumKeys, moodText)
|
||||
},
|
||||
CANDIDATE_POOL,
|
||||
)
|
||||
val upserts = ranked.map { candidate ->
|
||||
RecommendationUpsert(
|
||||
source = mood.source,
|
||||
externalId = candidate.externalId,
|
||||
title = candidate.title,
|
||||
artist = candidate.artist,
|
||||
album = candidate.album,
|
||||
duration = candidate.duration,
|
||||
thumbnail = candidate.thumbnail,
|
||||
)
|
||||
.take(CANDIDATE_POOL)
|
||||
.map { (candidate, _) ->
|
||||
RecommendationUpsert(
|
||||
source = mood.source,
|
||||
externalId = candidate.externalId,
|
||||
title = candidate.title,
|
||||
artist = candidate.artist,
|
||||
album = candidate.album,
|
||||
duration = candidate.duration,
|
||||
thumbnail = candidate.thumbnail,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if (upserts.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
@@ -121,12 +159,22 @@ class DiscoveryService(
|
||||
CANDIDATE_POOL,
|
||||
)
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
log.info("{} discovery for mood {} stored {} candidates", catalog.source, mood.name, stored)
|
||||
log.info(
|
||||
"{} discovery for mood {} stored {} candidates from {} searches and {} expanded hits",
|
||||
catalog.source,
|
||||
mood.name,
|
||||
stored,
|
||||
queries.size,
|
||||
discovered.size,
|
||||
)
|
||||
return DiscoveryOutcome(stored = stored)
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val QUERY_LIMIT = 10
|
||||
|
||||
/** The graph expands far more than a single search page does. */
|
||||
const val DISCOVERY_LIMIT = 200
|
||||
const val CANDIDATE_POOL = 60
|
||||
}
|
||||
}
|
||||
@@ -144,28 +192,96 @@ internal fun isMusicLength(seconds: Int?): Boolean =
|
||||
internal fun buildQueries(mood: MoodRecord, seedArtists: List<String>): List<String> {
|
||||
val queries = LinkedHashSet<String>()
|
||||
mood.query?.trim()?.takeIf { it.isNotEmpty() }?.let(queries::add)
|
||||
seedArtists.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(2).forEach {
|
||||
// The collection is the strongest signal, so its artists fill the slots
|
||||
// before genres do. They arrive ordered by how many songs the mood holds.
|
||||
seedArtists.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(3).forEach {
|
||||
queries += it
|
||||
}
|
||||
mood.genres.map { it.trim() }.filter { it.isNotEmpty() }.take(2).forEach {
|
||||
queries += "$it mix"
|
||||
}
|
||||
if (queries.isEmpty()) queries += mood.name
|
||||
return queries.distinctBy { it.lowercase() }.take(4)
|
||||
}
|
||||
|
||||
/** Collection artists, most represented first. */
|
||||
internal fun artistsByFrequency(collection: List<SongRecord>): List<SeedArtist> =
|
||||
collection
|
||||
.map { it.artist.trim() }
|
||||
.filter { it.isNotEmpty() }
|
||||
.groupingBy { it }
|
||||
.eachCount()
|
||||
.entries
|
||||
.sortedWith(
|
||||
compareByDescending<Map.Entry<String, Int>> { it.value }.thenBy { it.key.lowercase() },
|
||||
)
|
||||
.map { SeedArtist(it.key, it.value) }
|
||||
|
||||
/** Distinct albums of the collection, deduplicated loosely. */
|
||||
internal fun albumsOf(collection: List<SongRecord>): List<SeedAlbum> =
|
||||
collection.mapNotNull { song ->
|
||||
val album = song.album?.trim()?.takeIf { it.isNotEmpty() } ?: return@mapNotNull null
|
||||
SeedAlbum(album, song.artist)
|
||||
}.distinctBy { "${matchKey(it.title)}|${matchKey(it.artist)}" }
|
||||
|
||||
/**
|
||||
* Picks the best [limit] candidates while penalising an artist that keeps
|
||||
* winning: without it, a mood with three songs by one artist would fill every
|
||||
* slot with that artist. Each additional pick by the same artist costs
|
||||
* [ARTIST_REPEAT_PENALTY], so a related artist eventually takes a slot.
|
||||
*/
|
||||
internal fun rankCandidates(
|
||||
scored: List<Pair<TrackCandidate, Double>>,
|
||||
limit: Int,
|
||||
): List<TrackCandidate> {
|
||||
val remaining = scored.toMutableList()
|
||||
val chosen = ArrayList<TrackCandidate>(limit.coerceAtMost(remaining.size))
|
||||
val perArtist = HashMap<String, Int>()
|
||||
|
||||
while (chosen.size < limit && remaining.isNotEmpty()) {
|
||||
var bestIndex = 0
|
||||
var bestScore = Double.NEGATIVE_INFINITY
|
||||
for (index in remaining.indices) {
|
||||
val candidate = remaining[index].first
|
||||
val picked = perArtist[matchKey(candidate.artist)] ?: 0
|
||||
val adjusted = remaining[index].second - ARTIST_REPEAT_PENALTY * picked
|
||||
if (adjusted > bestScore) {
|
||||
bestScore = adjusted
|
||||
bestIndex = index
|
||||
}
|
||||
}
|
||||
val candidate = remaining.removeAt(bestIndex).first
|
||||
chosen += candidate
|
||||
val key = matchKey(candidate.artist)
|
||||
perArtist[key] = (perArtist[key] ?: 0) + 1
|
||||
}
|
||||
return chosen
|
||||
}
|
||||
|
||||
/**
|
||||
* Ranks a candidate against the whole collection:
|
||||
*
|
||||
* - an artist already in the mood is the strongest signal, and an artist with
|
||||
* more songs in the mood ranks higher;
|
||||
* - another track from an album already in the mood is a strong signal;
|
||||
* - a catalog-supplied [TrackCandidate.affinity] (related artist, top track)
|
||||
* is added as-is;
|
||||
* - the mood's own words in the title are a tie-breaker.
|
||||
*/
|
||||
internal fun scoreCandidate(
|
||||
candidate: TrackCandidate,
|
||||
seedArtists: List<String>,
|
||||
artistWeights: Map<String, Double>,
|
||||
albumKeys: Set<String>,
|
||||
moodText: String,
|
||||
): Double {
|
||||
var score = 0.0
|
||||
var score = candidate.affinity
|
||||
val artist = matchKey(candidate.artist)
|
||||
val seedKeys = seedArtists.map { matchKey(it) }.filter { it.isNotEmpty() }
|
||||
val exact = artistWeights[artist]
|
||||
when {
|
||||
artist.isNotEmpty() && artist in seedKeys -> score += 3.0
|
||||
artist.isNotEmpty() && seedKeys.any { artist.contains(it) || it.contains(artist) } -> score += 1.5
|
||||
exact != null -> score += 3.0 + (0.5 * (exact - 1)).coerceAtMost(1.5)
|
||||
artist.isNotEmpty() && artistWeights.keys.any { artistMatches(artist, it) } -> score += 1.5
|
||||
}
|
||||
val album = matchKey(candidate.album.orEmpty())
|
||||
if (album.isNotEmpty() && album in albumKeys) score += 1.0
|
||||
val text = matchKey(moodText)
|
||||
if (text.isNotEmpty() && matchKey(candidate.title).contains(text)) score += 1.0
|
||||
if ((candidate.duration ?: 0) > 8 * 60) score -= 1.0
|
||||
@@ -198,3 +314,4 @@ internal fun artistMatches(a: String, b: String): Boolean {
|
||||
|
||||
internal const val MIN_TRACK_SECONDS = 30
|
||||
internal const val MAX_TRACK_SECONDS = 600
|
||||
internal const val ARTIST_REPEAT_PENALTY = 1.0
|
||||
@@ -2,7 +2,13 @@ package win.taptap.musicmanager.discovery
|
||||
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
/** One source-specific search hit, already split into an artist and a title. */
|
||||
/**
|
||||
* One source-specific search hit, already split into an artist and a title.
|
||||
*
|
||||
* [affinity] is a catalog-supplied similarity weight: the catalog knows where a
|
||||
* hit came from (a related artist, an album deep cut), which is information a
|
||||
* title search cannot express.
|
||||
*/
|
||||
data class TrackCandidate(
|
||||
val source: String,
|
||||
val externalId: String,
|
||||
@@ -11,8 +17,29 @@ data class TrackCandidate(
|
||||
val album: String? = null,
|
||||
val duration: Int? = null,
|
||||
val thumbnail: String? = null,
|
||||
val affinity: Double = 0.0,
|
||||
)
|
||||
|
||||
/** One artist of the mood collection and how many of its songs the mood holds. */
|
||||
data class SeedArtist(val name: String, val songs: Int)
|
||||
|
||||
/** One album of the mood collection. */
|
||||
data class SeedAlbum(val title: String, val artist: String)
|
||||
|
||||
/**
|
||||
* The whole mood collection, handed to a catalog that can expand it through its
|
||||
* own similarity graph instead of title searches alone.
|
||||
*/
|
||||
data class DiscoverySeeds(
|
||||
val artists: List<SeedArtist> = emptyList(),
|
||||
val albums: List<SeedAlbum> = emptyList(),
|
||||
val hint: String? = null,
|
||||
val genres: List<String> = emptyList(),
|
||||
val name: String = "",
|
||||
) {
|
||||
fun isEmpty(): Boolean = artists.isEmpty() && albums.isEmpty()
|
||||
}
|
||||
|
||||
/** A music catalog that can be searched and can resolve a pasted URL. */
|
||||
interface MusicCatalog {
|
||||
val source: String
|
||||
@@ -21,6 +48,16 @@ interface MusicCatalog {
|
||||
|
||||
/** Resolves a pasted track URL, or returns null when it is not a track. */
|
||||
fun resolve(url: String): TrackCandidate?
|
||||
|
||||
/**
|
||||
* Expands the whole collection through the catalog's own similarity graph:
|
||||
* related artists, artist top tracks and album deep cuts. Catalogs without
|
||||
* such an API keep the default empty list and rely on text queries.
|
||||
*
|
||||
* Implementations must be best-effort: one failing sub-request returns what
|
||||
* the other requests found instead of failing the whole discovery pass.
|
||||
*/
|
||||
fun discover(seeds: DiscoverySeeds, limit: Int): List<TrackCandidate> = emptyList()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -38,6 +38,19 @@ class TopUpService(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Same, for work that only knows the id: a finished download attaches a song
|
||||
* and changes the collection the next discovery pass is based on.
|
||||
*/
|
||||
fun trigger(moodId: String) {
|
||||
val scope = backgroundScope ?: return
|
||||
scope.launch(Dispatchers.IO) {
|
||||
val mood = MoodRepository.byId(moodId) ?: return@launch
|
||||
runCatching { topUp(mood, force = true) }
|
||||
.onFailure { log.warn("Recommendation top-up for mood {} failed", mood.name, it) }
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun topUp(mood: MoodRecord, force: Boolean = false): TopUpResult = mutex.withLock {
|
||||
var current = MoodRepository.get(mood.id, mood.owner) ?: return@withLock TopUpResult(0)
|
||||
if (current.recommendationCount <= 0) return@withLock TopUpResult(0)
|
||||
@@ -46,7 +59,10 @@ class TopUpService(
|
||||
|
||||
val stillMissing = current.recommendationCount - MoodRepository.activeCount(current.id)
|
||||
var discoveryError: String? = null
|
||||
if (stillMissing > 0 && (force || MoodRepository.isDiscoveryDue(current))) {
|
||||
val collectionChanged = MoodRepository.songsAddedSince(current.id, current.lastDiscoveryAt)
|
||||
if ((stillMissing > 0 || collectionChanged) &&
|
||||
(force || MoodRepository.isDiscoveryDue(current))
|
||||
) {
|
||||
val outcome = discovery.refresh(current, force)
|
||||
discoveryError = outcome.error
|
||||
current = MoodRepository.get(mood.id, mood.owner) ?: current
|
||||
|
||||
@@ -37,6 +37,12 @@ class DownloadService(
|
||||
private val tagExtractor: Mp3TagExtractor = Mp3TagExtractor(),
|
||||
private val tempDir: Path,
|
||||
private val maxAttempts: Int = 3,
|
||||
/**
|
||||
* Called after a manual download becomes a permanent mood song. The mood
|
||||
* collection is the discovery seed, so the recommendation pool has to be
|
||||
* refreshed instead of waiting for the hourly worker.
|
||||
*/
|
||||
private val onSongAttached: (String) -> Unit = {},
|
||||
) {
|
||||
private val log = LoggerFactory.getLogger(DownloadService::class.java)
|
||||
|
||||
@@ -119,6 +125,8 @@ class DownloadService(
|
||||
DownloadRepository.KIND_SONG -> {
|
||||
MoodRepository.addSong(job.moodId, song.id, "search")
|
||||
DownloadRepository.markDone(job.id, song.id)
|
||||
runCatching { onSongAttached(job.moodId) }
|
||||
.onFailure { log.warn("Mood {} top-up failed", job.moodId, it) }
|
||||
}
|
||||
else -> {
|
||||
when (
|
||||
|
||||
Reference in new issue
Block a user