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

@@ -19,11 +19,16 @@ import io.ktor.server.response.respond
import io.ktor.server.response.respondText
import io.ktor.server.routing.get
import io.ktor.server.routing.routing
import java.time.Instant
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancelAndJoin
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.job
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.json.Json
import org.slf4j.LoggerFactory
@@ -37,6 +42,9 @@ import win.taptap.musicmanager.api.apiRoutes
import win.taptap.musicmanager.db.ConfigRepository
import win.taptap.musicmanager.db.DatabaseFactory
import win.taptap.musicmanager.db.MediaRepository
import win.taptap.musicmanager.db.MoodRepository
import win.taptap.musicmanager.discovery.DiscoveryService
import win.taptap.musicmanager.discovery.YtDlpSearch
import win.taptap.musicmanager.radio.RadioService
import win.taptap.musicmanager.scanner.CoverArtService
import win.taptap.musicmanager.scanner.MediaScanner
@@ -58,6 +66,7 @@ fun main() {
val coverArtService = CoverArtService(config.dataDir)
val transcoder = FfmpegTranscoder(config.ffmpegPath)
val radioService = RadioService()
val discoveryService = DiscoveryService(YtDlpSearch(config.ytDlpPath))
val scanner = MediaScanner(
library = library,
coverArtService = coverArtService,
@@ -67,6 +76,17 @@ fun main() {
scanner.start(applicationScope)
}
// Moods refresh their YT recommendations in the background; the client
// can also force a pass from the mood screen.
applicationScope.launch(Dispatchers.IO) {
delay(TimeUnit.MINUTES.toMillis(2))
while (isActive) {
runCatching { discoverStaleMoods(users, library, discoveryService) }
.onFailure { log.warn("Background discovery pass failed", it) }
delay(TimeUnit.HOURS.toMillis(1))
}
}
val server = embeddedServer(Netty, port = config.port, host = "0.0.0.0") {
install(ContentNegotiation) {
json(
@@ -136,6 +156,7 @@ fun main() {
coverArtService = coverArtService,
transcoder = transcoder,
radioService = radioService,
discoveryService = discoveryService,
startScan = { scanner.start(applicationScope) },
scanStatus = { scanner.status() },
)
@@ -163,3 +184,15 @@ fun main() {
throw t
}
}
private suspend fun discoverStaleMoods(
users: UsersConfig,
library: LibraryConfig,
discovery: DiscoveryService,
) {
MoodRepository.staleDiscovery(Instant.now(), 10).forEach { mood ->
val owner = users.byName(mood.owner) ?: return@forEach
runCatching { discovery.refresh(mood, owner, library) }
.onFailure { log.warn("Discovery for mood {} failed", mood.name, it) }
}
}