feat: recommend from Deezer and YouTube Music radios
CI / chart (push) Successful in 2s
CI / test (push) Successful in 1m57s
CI / chart-push (push) Successful in 5s
CI / image (push) Successful in 3m39s

- Deezer: /artist/{id}/radio for collection artists, affinity 2.0
- YouTube Music: RDAMVM watch playlist via yt-dlp, affinity 2.0
- seed radios from the 2 newest songs of the mood's own catalog
- dedupe candidates by recording, not only by external id
- clean raw YouTube titles and Topic suffixes when indexing
- tests, smoke marker for the RDAMVM path, docs
This commit is contained in:
TapTap committed 2026-11-17 15:22:05 +00:00
1 parent 62bcefad20
commit 26c41cc7d9
12 files changed
+296 -44

No files matched your search

@@ -45,6 +45,8 @@ class DiscoveryService(
val seeds = DiscoverySeeds(
artists = artists,
albums = albums,
// Only songs from the mood's own catalog can seed its radio.
trackIds = radioSeedIds(collection, mood.source, RADIO_SEEDS),
hint = mood.query,
genres = mood.genres,
name = mood.name,
@@ -95,14 +97,10 @@ class DiscoveryService(
return DiscoveryOutcome(0, error = message)
}
val candidates = LinkedHashMap<String, TrackCandidate>()
(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
}
}
val candidates = mergeCandidates(
(searches.mapNotNull { it.getOrNull() }.flatten() + discovered)
.filter { isMusicLength(it.duration) },
)
if (candidates.isEmpty()) {
val message = "${catalog.source} returned no usable results"
@@ -118,6 +116,10 @@ class DiscoveryService(
.toSet()
val moodText = listOfNotNull(mood.query, mood.description, mood.name).joinToString(" ")
// A different upload of a song already in the mood is still a duplicate.
val collectionRecordings = collection
.map { recordingKey(it.artist, it.title) }
.toSet()
val artistWeights = artists
.associate { matchKey(it.name) to it.songs.toDouble() }
.filterKeys { it.isNotEmpty() }
@@ -129,6 +131,7 @@ class DiscoveryService(
val ranked = rankCandidates(
candidates.values
.filterNot { it.externalId in excluded }
.filterNot { recordingKey(it.artist, it.title) in collectionRecordings }
.map { candidate ->
candidate to scoreCandidate(candidate, artistWeights, albumKeys, moodText)
},
@@ -175,6 +178,7 @@ class DiscoveryService(
/** The graph expands far more than a single search page does. */
const val DISCOVERY_LIMIT = 200
const val RADIO_SEEDS = 2
const val CANDIDATE_POOL = 60
}
}
@@ -216,6 +220,20 @@ internal fun artistsByFrequency(collection: List<SongRecord>): List<SeedArtist>
)
.map { SeedArtist(it.key, it.value) }
/**
* The most recent `(source, externalId)` songs of the mood's catalog, one per
* artist: a radio seeded by three tracks of the same artist adds nothing.
*/
internal fun radioSeedIds(
collection: List<SongRecord>,
source: String,
limit: Int,
): List<String> = collection.asReversed()
.filter { it.source == source }
.distinctBy { matchKey(it.artist) }
.take(limit)
.map { it.externalId }
/** Distinct albums of the collection, deduplicated loosely. */
internal fun albumsOf(collection: List<SongRecord>): List<SeedAlbum> =
collection.mapNotNull { song ->
@@ -223,6 +241,39 @@ internal fun albumsOf(collection: List<SongRecord>): List<SeedAlbum> =
SeedAlbum(album, song.artist)
}.distinctBy { "${matchKey(it.title)}|${matchKey(it.artist)}" }
/**
* Keeps one copy per recording. A radio and an album endpoint may return the
* same song twice, and two uploads of one song only differ by external id; the
* copy with the stronger catalog affinity wins.
*/
internal fun mergeCandidates(
items: Iterable<TrackCandidate>,
): LinkedHashMap<String, TrackCandidate> {
val byId = LinkedHashMap<String, TrackCandidate>()
val idForRecording = HashMap<String, String>()
items.forEach { candidate ->
val recording = recordingKey(candidate.artist, candidate.title)
val currentId = idForRecording[recording]
val current = currentId?.let(byId::get)
when {
current == null -> {
byId[candidate.externalId] = candidate
idForRecording[recording] = candidate.externalId
}
candidate.affinity > current.affinity -> {
byId.remove(current.externalId)
byId[candidate.externalId] = candidate
idForRecording[recording] = candidate.externalId
}
}
}
return byId
}
/** Identity of a recording, independent of the catalog's track id. */
internal fun recordingKey(artist: String, title: String): String =
"${matchKey(artist)}|${matchKey(title)}"
/**
* 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