feat: discover YT recommendations and tune the radio
CI / chart (push) Successful in 26s
CI / chart-push (push) Successful in 5s
CI / test (push) Successful in 2m6s
CI / image (push) Failing after 2m54s

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:
TapTap committed 2026-11-15 12:44:30 +00:00
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) },
)