feat: discover YT recommendations and tune the radio
yt-dlp now feeds mood discovery. Queries come from the free-text hint, seed artists and genres; they run in parallel, are filtered to music-length videos, matched against library songs by normalized title and artist, scored, and stored as review candidates. Refresh, accept and reject endpoints are added: accepting a library match adds a seed immediately, while accepted tracks that are not owned wait for the future downloader. Decisions survive refreshes and stale undecided candidates are pruned. A background pass refreshes stale moods every hour, and the image ships a pinned yt-dlp binary (YTDLP_PATH, also added to the Nix shell and chart). Radio now avoids tracks played in the last 7 days when the pool is large enough, spaces artists and albums across a batch, and weighs skips three times as heavily as completions. The chart moves to 0.2.2.
This commit is contained in:
1 parent
67eed8d262
commit
c5aa000d1a
18 files changed
+1061
-37
No files matched your search
@@ -53,6 +53,9 @@ import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.SongArtistsTable
|
||||
import win.taptap.musicmanager.db.SongGenresTable
|
||||
import win.taptap.musicmanager.db.SongsTable
|
||||
import win.taptap.musicmanager.discovery.DiscoveryService
|
||||
import win.taptap.musicmanager.discovery.YtCandidate
|
||||
import win.taptap.musicmanager.discovery.YtSearch
|
||||
import win.taptap.musicmanager.radio.RadioService
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.ScanStatus
|
||||
@@ -240,6 +243,107 @@ class ApiRoutesTest {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
fun `yt recommendations can be refreshed accepted and rejected`() {
|
||||
seededContext().use { seed ->
|
||||
val results = listOf(
|
||||
YtCandidate(
|
||||
videoId = "v-match",
|
||||
title = "Soft Light",
|
||||
artist = "Beta",
|
||||
duration = 180,
|
||||
thumbnail = "https://img.example/v-match.jpg",
|
||||
),
|
||||
YtCandidate("v-new", "Fresh Track", "New Artist", duration = 200),
|
||||
YtCandidate("v-reject", "Loud", "Gamma", duration = 120),
|
||||
YtCandidate("v-long", "Late Night Set", "Some DJ", duration = 3_600),
|
||||
)
|
||||
val search = YtSearch { _, _ -> results }
|
||||
|
||||
testApplication {
|
||||
setupApp(seed, search)
|
||||
val token = client.token("alice", "alice-pw")
|
||||
|
||||
val created = client.post("/api/v1/moods") {
|
||||
bearer(token)
|
||||
contentType(ContentType.Application.Json)
|
||||
setBody(
|
||||
"""{"name":"Focus","genres":["Ambient"],"seedSongIds":["${seed.aliceAmbient1}"]}""",
|
||||
)
|
||||
}
|
||||
val moodId = parse(created.bodyAsText())["mood"]!!.jsonObject.string("id")
|
||||
|
||||
val refreshed = client.post("/api/v1/moods/$moodId/recommendations/refresh") {
|
||||
bearer(token)
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, refreshed.status)
|
||||
val refreshedBody = refreshed.bodyAsText()
|
||||
val recommendations = parse(refreshedBody)["recommendations"]!!.jsonArray
|
||||
.map { it.jsonObject }
|
||||
// Long DJ sets are not music candidates.
|
||||
assertFalse(recommendations.any { it.string("videoId") == "v-long" })
|
||||
assertEquals(3, recommendations.size)
|
||||
|
||||
val match = recommendations.first { it.string("videoId") == "v-match" }
|
||||
assertEquals(
|
||||
seed.aliceAmbient2,
|
||||
(match["matchedSong"] as? JsonObject)?.string("id"),
|
||||
)
|
||||
assertEquals("https://img.example/v-match.jpg", match.string("thumbnail"))
|
||||
val unmatched = recommendations.first { it.string("videoId") == "v-new" }
|
||||
assertTrue(unmatched["matchedSong"] !is JsonObject)
|
||||
|
||||
// A recommendation already in the library becomes a seed at once.
|
||||
val accepted = client.post("/api/v1/moods/$moodId/recommendations/v-match/accept") {
|
||||
bearer(token)
|
||||
}
|
||||
assertEquals(HttpStatusCode.OK, accepted.status)
|
||||
val acceptedBody = parse(accepted.bodyAsText())
|
||||
assertTrue(acceptedBody["seedAdded"]!!.jsonPrimitive.content.toBoolean())
|
||||
assertEquals("accepted", acceptedBody["recommendation"]!!.jsonObject.string("state"))
|
||||
assertEquals(2, acceptedBody["detail"]!!.jsonObject["mood"]!!.jsonObject.int("seedCount"))
|
||||
|
||||
// Unknown tracks are accepted for the future downloader.
|
||||
val acceptedNew = client.post("/api/v1/moods/$moodId/recommendations/v-new/accept") {
|
||||
bearer(token)
|
||||
}
|
||||
val acceptedNewBody = parse(acceptedNew.bodyAsText())
|
||||
assertFalse(acceptedNewBody["seedAdded"]!!.jsonPrimitive.content.toBoolean())
|
||||
assertEquals(
|
||||
"accepted",
|
||||
acceptedNewBody["recommendation"]!!.jsonObject.string("state"),
|
||||
)
|
||||
|
||||
val rejected = client.post("/api/v1/moods/$moodId/recommendations/v-reject/reject") {
|
||||
bearer(token)
|
||||
}
|
||||
assertEquals(
|
||||
"rejected",
|
||||
parse(rejected.bodyAsText())["recommendation"]!!.jsonObject.string("state"),
|
||||
)
|
||||
|
||||
// Decisions survive a refresh: nothing is resurrected.
|
||||
val again = client.post("/api/v1/moods/$moodId/recommendations/refresh") {
|
||||
bearer(token)
|
||||
}
|
||||
val states = parse(again.bodyAsText())["recommendations"]!!.jsonArray
|
||||
.map { it.jsonObject }
|
||||
.associate { it.string("videoId") to it.string("state") }
|
||||
assertEquals("accepted", states["v-match"])
|
||||
assertEquals("accepted", states["v-new"])
|
||||
assertEquals("rejected", states["v-reject"])
|
||||
|
||||
// Recommendations are private to the mood owner.
|
||||
val bob = client.token("bob", "bob-pw")
|
||||
val denied = client.post("/api/v1/moods/$moodId/recommendations/refresh") {
|
||||
bearer(bob)
|
||||
}
|
||||
assertEquals(HttpStatusCode.NotFound, denied.status)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a session is scoped to its owner`() {
|
||||
seededContext().use { seed ->
|
||||
@@ -267,7 +371,10 @@ class ApiRoutesTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun ApplicationTestBuilder.setupApp(seed: Seed) {
|
||||
private fun ApplicationTestBuilder.setupApp(
|
||||
seed: Seed,
|
||||
ytSearch: YtSearch = YtSearch { _, _ -> emptyList() },
|
||||
) {
|
||||
application {
|
||||
install(ContentNegotiation) {
|
||||
json(Json { ignoreUnknownKeys = true; encodeDefaults = true })
|
||||
@@ -288,6 +395,7 @@ class ApiRoutesTest {
|
||||
coverArtService = seed.coverArt,
|
||||
transcoder = FfmpegTranscoder("ffmpeg"),
|
||||
radioService = RadioService(),
|
||||
discoveryService = DiscoveryService(ytSearch),
|
||||
startScan = { false },
|
||||
scanStatus = { ScanStatus(false, 0) },
|
||||
)
|
||||
|
||||
@@ -0,0 +1,113 @@
|
||||
package win.taptap.musicmanager.discovery
|
||||
|
||||
import java.time.Instant
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertFalse
|
||||
import kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
|
||||
class DiscoveryServiceTest {
|
||||
|
||||
@Test
|
||||
fun `parses flat playlist output and splits artist from title`() {
|
||||
val json = """
|
||||
{"id":"search","entries":[
|
||||
{"id":"a","title":"Alpha - Focus One (Official Video)","duration":123.0,
|
||||
"channel":"Alpha","thumbnails":[{"url":"http://t/1.jpg"},{"url":"http://t/2.jpg"}]},
|
||||
{"id":"b","title":"Some Song","duration":61,"channel":"Some Artist - Topic"},
|
||||
{"id":"c","title":"Loud | Official Audio","duration":90,"channel":"Gamma"}
|
||||
]}
|
||||
""".trimIndent()
|
||||
|
||||
val parsed = parseYtSearchOutput(json)
|
||||
|
||||
assertEquals(3, parsed.size)
|
||||
assertEquals(
|
||||
YtCandidate("a", "Focus One", "Alpha", null, 123, "http://t/2.jpg"),
|
||||
parsed[0],
|
||||
)
|
||||
assertEquals(YtCandidate("b", "Some Song", "Some Artist", null, 61, null), parsed[1])
|
||||
assertEquals(YtCandidate("c", "Loud", "Gamma", null, 90, null), parsed[2])
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `malformed search output yields no candidates`() {
|
||||
assertTrue(parseYtSearchOutput("").isEmpty())
|
||||
assertTrue(parseYtSearchOutput("not json").isEmpty())
|
||||
assertTrue(parseYtSearchOutput("""{"entries": "nope"}""").isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `music length keeps songs and unknown durations but drops long mixes`() {
|
||||
assertTrue(isMusicLength(null))
|
||||
assertTrue(isMusicLength(45))
|
||||
assertTrue(isMusicLength(200))
|
||||
assertFalse(isMusicLength(30))
|
||||
assertFalse(isMusicLength(21 * 60))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `match keys ignore brackets punctuation and featuring credits`() {
|
||||
assertEquals(matchKey("Focus One"), matchKey("Focus One (Official Video)"))
|
||||
assertEquals(matchKey("Focus One"), matchKey("Focus One [Remastered]"))
|
||||
assertEquals(matchKey("Alpha"), matchKey("Alpha feat. Beta"))
|
||||
assertTrue(artistMatches("Alpha - Topic", "Alpha"))
|
||||
assertTrue(artistMatches("Alpha & Friends", "Alpha"))
|
||||
assertFalse(artistMatches("Beta", "Alpha"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `freshness follows the mood refresh window`() {
|
||||
val now = Instant.parse("2026-11-15T10:00:00Z")
|
||||
assertFalse(isFresh(null, 24, now))
|
||||
assertTrue(isFresh("2026-11-15T01:00:00Z", 24, now))
|
||||
assertFalse(isFresh("2026-11-13T01:00:00Z", 24, now))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `queries combine text seed artists and genres`() {
|
||||
val mood = mood(query = "late night", genres = listOf("Ambient", "Jazz"))
|
||||
val queries = buildQueries(mood, listOf("Alpha", "Beta", "Alpha"))
|
||||
|
||||
assertEquals(listOf("late night", "Alpha", "Beta", "Ambient mix"), queries)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `queries fall back to the mood name`() {
|
||||
assertEquals(listOf("Focus"), buildQueries(mood(), emptyList()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `seed artists and library matches score higher`() {
|
||||
val candidate = YtCandidate("v", "Focus Two", "Beta", duration = 200)
|
||||
|
||||
val plain = scoreCandidate(candidate, listOf("Alpha"), "focus", matched = false)
|
||||
val seedArtist = scoreCandidate(candidate, listOf("Beta"), "focus", matched = false)
|
||||
val matched = scoreCandidate(candidate, listOf("Alpha"), "focus", matched = true)
|
||||
|
||||
assertTrue(seedArtist > plain)
|
||||
assertTrue(matched > plain)
|
||||
}
|
||||
|
||||
private fun mood(
|
||||
query: String? = null,
|
||||
genres: List<String> = emptyList(),
|
||||
) = MoodRecord(
|
||||
id = "mo-1",
|
||||
owner = "alice",
|
||||
name = "Focus",
|
||||
description = null,
|
||||
query = query,
|
||||
genres = genres,
|
||||
targetSize = 30,
|
||||
refreshHours = 24,
|
||||
ytEnabled = true,
|
||||
lastDiscoveryAt = null,
|
||||
createdAt = "2026-01-01T00:00:00Z",
|
||||
changedAt = "2026-01-01T00:00:00Z",
|
||||
lastError = null,
|
||||
seedCount = 0,
|
||||
newRecommendations = 0,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package win.taptap.musicmanager.radio
|
||||
|
||||
import kotlin.test.Test
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertTrue
|
||||
import win.taptap.musicmanager.db.AffinityRecord
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
|
||||
class RadioServiceTest {
|
||||
|
||||
private val service = RadioService()
|
||||
|
||||
@Test
|
||||
fun `recently played songs are penalized`() {
|
||||
val song = song("s-1")
|
||||
val fresh = service.scoreCandidate(
|
||||
song, emptySet(), emptyList(), emptyList(), null, affinity = 0.0, jitter = 0.0,
|
||||
)
|
||||
val recent = service.scoreCandidate(
|
||||
song, emptySet(), emptyList(), emptyList(), null, affinity = 0.0,
|
||||
recent = setOf("s-1"), jitter = 0.0,
|
||||
)
|
||||
|
||||
assertEquals(0.0, fresh)
|
||||
assertEquals(-6.0, recent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `skips outweigh completions`() {
|
||||
assertEquals(1.0, AffinityRecord(completions = 1, skips = 0).score)
|
||||
assertEquals(-2.0, AffinityRecord(completions = 1, skips = 1).score)
|
||||
assertEquals(-5.0, AffinityRecord(completions = 1, skips = 2).score)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `seeds and matching genres are boosted`() {
|
||||
val seeded = service.scoreCandidate(
|
||||
song("s-1", genres = listOf("Ambient")),
|
||||
seedIds = setOf("s-1"),
|
||||
seedGenres = listOf("Ambient"),
|
||||
seedArtistIds = emptyList(),
|
||||
query = null,
|
||||
affinity = 0.0,
|
||||
jitter = 0.0,
|
||||
)
|
||||
val plain = service.scoreCandidate(
|
||||
song("s-2"),
|
||||
seedIds = setOf("s-1"),
|
||||
seedGenres = listOf("Ambient"),
|
||||
seedArtistIds = emptyList(),
|
||||
query = null,
|
||||
affinity = 0.0,
|
||||
jitter = 0.0,
|
||||
)
|
||||
|
||||
assertEquals(11.5, seeded)
|
||||
assertEquals(0.0, plain)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `picks keep artists and albums apart`() {
|
||||
val scored = listOf(
|
||||
song("s1", artist = "A", albumId = "al1") to 0.0,
|
||||
song("s2", artist = "B", albumId = "al2") to 0.0,
|
||||
song("s3", artist = "C", albumId = "al3") to 0.0,
|
||||
song("s4", artist = "D", albumId = "al4") to 0.0,
|
||||
song("s5", artist = "A", albumId = "al5") to 0.0,
|
||||
song("s6", artist = "B", albumId = "al6") to 0.0,
|
||||
)
|
||||
|
||||
val picked = service.pickDiverse(scored, limit = 6)
|
||||
|
||||
assertEquals(6, picked.size)
|
||||
picked.indices.forEach { index ->
|
||||
val artists = picked.subList(index, minOf(index + 3, picked.size)).map { it.displayArtist }
|
||||
assertEquals(artists.size, artists.distinct().size, "artist spacing at $index")
|
||||
val albums = picked.subList(index, minOf(index + 4, picked.size)).map { it.albumId }
|
||||
assertEquals(albums.size, albums.distinct().size, "album spacing at $index")
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `picks relax spacing when the pool is too small`() {
|
||||
val scored = listOf(
|
||||
song("s1", artist = "A", albumId = "al1") to 0.0,
|
||||
song("s2", artist = "A", albumId = "al1") to 0.0,
|
||||
)
|
||||
|
||||
val picked = service.pickDiverse(scored, limit = 2)
|
||||
|
||||
assertEquals(listOf("s1", "s2"), picked.map { it.id })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `higher scores are picked first`() {
|
||||
val scored = listOf(
|
||||
song("low", artist = "A", albumId = "al1") to 1.0,
|
||||
song("high", artist = "B", albumId = "al2") to 9.0,
|
||||
)
|
||||
|
||||
assertTrue(service.pickDiverse(scored, limit = 2).first().id == "high")
|
||||
}
|
||||
|
||||
private fun song(
|
||||
id: String,
|
||||
artist: String = "Artist $id",
|
||||
albumId: String = "album-$id",
|
||||
genres: List<String> = emptyList(),
|
||||
) = SongRecord(
|
||||
id = id,
|
||||
albumId = albumId,
|
||||
albumName = "Album",
|
||||
artistId = "ar-$artist",
|
||||
artistName = artist,
|
||||
displayArtist = artist,
|
||||
title = "Title $id",
|
||||
trackNumber = 1,
|
||||
discNumber = 1,
|
||||
year = 2026,
|
||||
duration = 180,
|
||||
bitRate = 320,
|
||||
size = 1_000,
|
||||
suffix = "mp3",
|
||||
contentType = "audio/mpeg",
|
||||
coverArt = null,
|
||||
folderId = 1,
|
||||
relativePath = "$id.mp3",
|
||||
created = "2026-01-01T00:00:00Z",
|
||||
updatedAt = "2026-01-01T00:00:00Z",
|
||||
starredAt = null,
|
||||
rating = null,
|
||||
playCount = 0,
|
||||
lastPlayedAt = null,
|
||||
artists = emptyList(),
|
||||
genres = genres,
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user