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

@@ -7,12 +7,15 @@ import io.ktor.server.routing.delete
import io.ktor.server.routing.get
import io.ktor.server.routing.patch
import io.ktor.server.routing.post
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import win.taptap.musicmanager.api.AddSeedRequest
import win.taptap.musicmanager.api.AddToMoodResponse
import win.taptap.musicmanager.api.CreateMoodRequest
import win.taptap.musicmanager.api.MoodDetailDto
import win.taptap.musicmanager.api.MoodDto
import win.taptap.musicmanager.api.RecommendationDecisionDto
import win.taptap.musicmanager.api.RecommendationDto
import win.taptap.musicmanager.api.SearchResponse
import win.taptap.musicmanager.api.UpdateMoodRequest
@@ -24,6 +27,7 @@ import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.LibraryRepository
import win.taptap.musicmanager.db.MoodRecord
import win.taptap.musicmanager.db.MoodRepository
import win.taptap.musicmanager.discovery.DiscoveryService
import win.taptap.musicmanager.radio.RadioService
@Serializable
@@ -33,6 +37,7 @@ fun Route.moodRoutes(
users: UsersConfig,
library: LibraryConfig,
radioService: RadioService,
discoveryService: DiscoveryService,
) {
get("/moods") {
val user = call.requireApiUser(users)
@@ -147,6 +152,50 @@ fun Route.moodRoutes(
val mood = ownedMood(call.parameters["id"], user)
call.respond(recommendations(mood, user, library))
}
/** Runs a discovery pass now, bypassing the mood's refresh interval. */
post("/moods/{id}/recommendations/refresh") {
val user = call.requireApiUser(users)
val mood = ownedMood(call.parameters["id"], user)
if (!mood.ytEnabled) conflict("YT discovery is disabled for this mood")
val outcome = withContext(Dispatchers.IO) {
discoveryService.refresh(mood, user, library, force = true)
}
outcome.error?.let { message ->
throw ApiException(HttpStatusCode.BadGateway, "discovery_failed", message)
}
val refreshed = MoodRepository.get(mood.id, user.name) ?: mood
call.respond(detail(refreshed, user, library))
}
post("/moods/{id}/recommendations/{videoId}/accept") {
val user = call.requireApiUser(users)
val mood = ownedMood(call.parameters["id"], user)
val videoId = call.parameters["videoId"] ?: badRequest("Video id is required")
val recommendation = MoodRepository.recommendation(mood.id, videoId)
?: notFound("Recommendation")
val matched = recommendation.matchedSongId?.let { songId ->
LibraryRepository.song(songId, user.musicFolderIds(library), user.name)
}
val seedAdded = matched != null &&
MoodRepository.addSeed(mood.id, user.name, matched.id, "recommendation")
val alreadyAccepted = recommendation.state == "accepted"
MoodRepository.setRecommendationState(mood.id, videoId, "accepted")
if (!alreadyAccepted) MoodRepository.incrementRecommendationAdds(mood.id, videoId)
call.respond(decision(mood.id, videoId, user, library, seedAdded))
}
post("/moods/{id}/recommendations/{videoId}/reject") {
val user = call.requireApiUser(users)
val mood = ownedMood(call.parameters["id"], user)
val videoId = call.parameters["videoId"] ?: badRequest("Video id is required")
MoodRepository.recommendation(mood.id, videoId) ?: notFound("Recommendation")
MoodRepository.setRecommendationState(mood.id, videoId, "rejected")
call.respond(decision(mood.id, videoId, user, library, seedAdded = false))
}
}
private fun ownedMood(id: String?, user: UserAccount): MoodRecord {
@@ -205,5 +254,23 @@ private fun recommendations(
)
}
private fun decision(
moodId: String,
videoId: String,
user: UserAccount,
library: LibraryConfig,
seedAdded: Boolean,
): RecommendationDecisionDto {
val mood = MoodRepository.get(moodId, user.name) ?: notFound("Mood")
val detail = detail(mood, user, library)
val recommendation = detail.recommendations.firstOrNull { it.videoId == videoId }
?: notFound("Recommendation")
return RecommendationDecisionDto(
detail = detail,
recommendation = recommendation,
seedAdded = seedAdded,
)
}
@Serializable
data class RecommendationsResponse(val recommendations: List<RecommendationDto>)