diff --git a/Dockerfile b/Dockerfile index c5b814a..06947c6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,9 +12,15 @@ RUN gradle --no-daemon shadowJar # glibc (not Alpine/musl): sqlite-jdbc ships no native library for Linux-Musl. FROM eclipse-temurin:21-jre-jammy +ARG YTDLP_VERSION=2026.10.26 + RUN apt-get update \ - && apt-get install -y --no-install-recommends ffmpeg \ + && apt-get install -y --no-install-recommends ca-certificates curl ffmpeg \ && rm -rf /var/lib/apt/lists/* \ + && curl -fsSL "https://github.com/yt-dlp/yt-dlp/releases/download/${YTDLP_VERSION}/yt-dlp" \ + -o /usr/local/bin/yt-dlp \ + && chmod 0755 /usr/local/bin/yt-dlp \ + && yt-dlp --version \ && groupadd -g 10001 app \ && useradd -u 10001 -g app -M -s /usr/sbin/nologin app @@ -25,6 +31,7 @@ ENV DATA_DIR=/data \ LIBRARY_CONFIG=/etc/musicmanager/library.yaml \ USERS_FILE=/etc/musicmanager/users.yaml \ FFMPEG_PATH=ffmpeg \ + YTDLP_PATH=yt-dlp \ PORT=8080 USER 10001:10001 diff --git a/README.md b/README.md index 365d4b7..835718c 100644 --- a/README.md +++ b/README.md @@ -109,6 +109,7 @@ Passwords are read into memory at startup and never written to SQLite. In produc | `LIBRARY_CONFIG` | `./config/library.yaml` | Library folders | | `USERS_FILE` | `./config/users.yaml` | Users and passwords | | `FFMPEG_PATH` | `ffmpeg` | Transcoder binary | +| `YTDLP_PATH` | `yt-dlp` | `yt-dlp` binary used for YT Music discovery | ## The mood model @@ -117,8 +118,8 @@ A mood is a private collection owned by one user: - **Seeds** are permanent anchors. Every seed is always eligible for the radio. - **Genres / free-text hint** widen the candidate pool. - **Target size** is the desired batch and session size, not a hard playlist length. -- **Radio** is generated by the server for each playback session. It mixes seeds, related genres and artists, text matches, and the rest of the owner's music. -- **Signals** come from playback: completed tracks gain weight, skipped tracks lose weight. There are no likes. +- **Radio** is generated by the server for each playback session. It mixes seeds, related genres and artists, text matches, and the rest of the owner's music. Tracks started, finished or skipped in the last 7 days are skipped while the library is large enough, and artists/albums are spaced across the batch. +- **Signals** come from playback: completed tracks gain weight, skips weigh three times as much. There are no likes. - **Add to mood** is explicit. The client offers *add to current mood* and *choose another mood*, and the server turns the track into a manual seed. - **Long-form is excluded** at seed validation and radio generation. @@ -175,7 +176,10 @@ Song objects contain deterministic IDs (`sg-…`), album/artist names, duration, - `POST /moods/{id}/seeds` — `{ songId }` - `DELETE /moods/{id}/seeds/{songId}` - `GET /moods/{id}/preview?limit=20` — unpersisted radio preview. -- `GET /moods/{id}/recommendations` — YT Music candidates (see roadmap). +- `GET /moods/{id}/recommendations` — YT Music candidates with state and matched library song. +- `POST /moods/{id}/recommendations/refresh` — run discovery now (bypasses `refreshHours`). +- `POST /moods/{id}/recommendations/{videoId}/accept` — accept; the library match becomes a seed immediately. +- `POST /moods/{id}/recommendations/{videoId}/reject` — reject; the candidate is not offered again. ### Radio sessions @@ -201,6 +205,9 @@ src/main/kotlin/win/taptap/musicmanager/ | |-- AppConfig.kt # Port, data dir, ffmpeg, env overrides | |-- LibraryConfig.kt # Folder IDs, types and paths | `-- UsersConfig.kt # Users, admins and folder assignments +|-- discovery/ +| |-- YtSearch.kt # yt-dlp search and result parsing +| `-- DiscoveryService.kt # Queries, matching, scoring, pruning |-- api/ | |-- ApiRoutes.kt # /api/v1 wiring, auth and system endpoints | |-- ApiModels.kt # Native DTOs @@ -223,6 +230,8 @@ src/main/kotlin/win/taptap/musicmanager/ `-- util/ # Deterministic IDs, sort names ``` +YT discovery runs in the background for stale moods (`refreshHours`) and on demand from the mood screen. Queries come from the mood text, seed artists and genres; results are deduplicated, filtered to music-length videos, matched to library songs by normalized title and artist, and scored. Undecided candidates not seen in a refresh are pruned; accepted and rejected decisions are permanent. Accepted tracks that are not in the library wait for the future downloader, which will drop them into an import folder and rescan. + The old OpenSubsonic layer (`/rest`, `subsonic/`, playlist materialization) was removed in 0.2.0. The server and the KMP client are the two halves of the same product; there is no compatibility layer to preserve and no server-rendered UI. ## Database @@ -324,7 +333,7 @@ The smoke test generates tagged MP3s, starts the server, and verifies login, syn - [x] Native bearer-auth API, sync deltas, tombstones. - [x] Mood CRUD, seeds, server-driven radio, playback events, add-to-mood. - [x] Kotlin Multiplatform client foundation (Android, desktop, web). -- [ ] Offline downloads and offline playback across clients. -- [ ] YT Music discovery through `yt-dlp`, recommendation list, accept/reject. +- [x] Offline downloads and offline playback across clients. +- [x] YT Music discovery through `yt-dlp`, recommendation list, accept/reject. - [ ] Optional downloader that writes accepted tracks into an import folder and rescans. - [ ] Home-screen widgets, Android Auto, and richer radio tuning. diff --git a/charts/audiomanager/Chart.yaml b/charts/audiomanager/Chart.yaml index 3064055..b85d9b1 100644 --- a/charts/audiomanager/Chart.yaml +++ b/charts/audiomanager/Chart.yaml @@ -2,5 +2,5 @@ apiVersion: v2 name: audiomanager description: Mood-first self-hosted music server type: application -version: 0.2.1 +version: 0.2.2 appVersion: "0.1.0" diff --git a/charts/audiomanager/templates/deployment.yaml b/charts/audiomanager/templates/deployment.yaml index 3b3db82..82d8597 100644 --- a/charts/audiomanager/templates/deployment.yaml +++ b/charts/audiomanager/templates/deployment.yaml @@ -42,6 +42,8 @@ spec: value: /etc/audiomanager/users.yaml - name: FFMPEG_PATH value: ffmpeg + - name: YTDLP_PATH + value: yt-dlp {{- with .Values.env }} {{- toYaml . | nindent 12 }} {{- end }} diff --git a/shell.nix b/shell.nix index 3b32f46..d407971 100644 --- a/shell.nix +++ b/shell.nix @@ -5,6 +5,7 @@ pkgs.mkShell { jdk21 gradle ffmpeg + yt-dlp sqlite git curl @@ -17,7 +18,7 @@ pkgs.mkShell { JAVA_HOME = pkgs.jdk21.home; shellHook = '' - echo "MusicManager dev shell (JDK 21, Gradle, ffmpeg, sqlite, helm, kustomize)" + echo "MusicManager dev shell (JDK 21, Gradle, ffmpeg, yt-dlp, sqlite, helm, kustomize)" java -version echo "" echo "Test: gradle test" diff --git a/src/main/kotlin/win/taptap/musicmanager/Application.kt b/src/main/kotlin/win/taptap/musicmanager/Application.kt index 820f720..1658a5d 100644 --- a/src/main/kotlin/win/taptap/musicmanager/Application.kt +++ b/src/main/kotlin/win/taptap/musicmanager/Application.kt @@ -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) } + } +} diff --git a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt index 05fc7d8..21e8a04 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/ApiModels.kt @@ -133,6 +133,13 @@ data class MoodDetailDto( val recommendations: List = emptyList(), ) +@Serializable +data class RecommendationDecisionDto( + val detail: MoodDetailDto, + val recommendation: RecommendationDto, + val seedAdded: Boolean = false, +) + @Serializable data class CreateMoodRequest( val name: String, diff --git a/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt b/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt index 9484336..5ef23a2 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/ApiRoutes.kt @@ -16,6 +16,7 @@ import win.taptap.musicmanager.api.UserDto import win.taptap.musicmanager.config.LibraryConfig import win.taptap.musicmanager.config.UsersConfig import win.taptap.musicmanager.db.ApiTokenRepository +import win.taptap.musicmanager.discovery.DiscoveryService import win.taptap.musicmanager.radio.RadioService import win.taptap.musicmanager.scanner.CoverArtService import win.taptap.musicmanager.scanner.ScanStatus @@ -33,6 +34,7 @@ fun Route.apiRoutes( coverArtService: CoverArtService, transcoder: FfmpegTranscoder, radioService: RadioService, + discoveryService: DiscoveryService, startScan: () -> Boolean, scanStatus: () -> ScanStatus, ) { @@ -125,7 +127,7 @@ fun Route.apiRoutes( } libraryRoutes(users, library, coverArtService, transcoder) - moodRoutes(users, library, radioService) + moodRoutes(users, library, radioService, discoveryService) sessionRoutes(users, library, radioService) } } diff --git a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt index 87a1376..4f141cc 100644 --- a/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt +++ b/src/main/kotlin/win/taptap/musicmanager/api/MoodApi.kt @@ -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) diff --git a/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt b/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt index 7f26911..386a863 100644 --- a/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt +++ b/src/main/kotlin/win/taptap/musicmanager/config/AppConfig.kt @@ -14,6 +14,7 @@ data class AppConfig( val libraryConfigPath: Path, val usersFile: Path, val ffmpegPath: String, + val ytDlpPath: String = "yt-dlp", ) { val databasePath: Path get() = dataDir.resolve("musicmanager.db") @@ -33,6 +34,7 @@ data class AppConfig( libraryConfigPath = Path.of(value("LIBRARY_CONFIG", "./config/library.yaml")), usersFile = Path.of(value("USERS_FILE", "./config/users.yaml")), ffmpegPath = value("FFMPEG_PATH", "ffmpeg"), + ytDlpPath = value("YTDLP_PATH", "yt-dlp"), ) } } diff --git a/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt b/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt index b7f5e69..e1dedfb 100644 --- a/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt +++ b/src/main/kotlin/win/taptap/musicmanager/db/MoodRepository.kt @@ -1,12 +1,14 @@ package win.taptap.musicmanager.db import java.time.Instant +import java.time.temporal.ChronoUnit import kotlinx.serialization.builtins.ListSerializer import kotlinx.serialization.builtins.serializer import kotlinx.serialization.json.Json import org.jetbrains.exposed.sql.ResultRow import org.jetbrains.exposed.sql.SortOrder import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.deleteWhere import org.jetbrains.exposed.sql.insert @@ -40,6 +42,16 @@ data class MoodSeedRecord( val addedAt: String, ) +data class RecommendationUpsert( + val videoId: String, + val title: String, + val artist: String, + val album: String?, + val duration: Int?, + val thumbnail: String?, + val matchedSongId: String?, +) + data class RecommendationRecord( val videoId: String, val title: String, @@ -212,21 +224,117 @@ object MoodRepository { MoodRecommendationsTable.selectAll() .where { MoodRecommendationsTable.moodId eq id } .orderBy(MoodRecommendationsTable.lastSeen to SortOrder.DESC) - .map { - RecommendationRecord( - videoId = it[MoodRecommendationsTable.videoId], - title = it[MoodRecommendationsTable.title], - artist = it[MoodRecommendationsTable.artist], - album = it[MoodRecommendationsTable.album], - duration = it[MoodRecommendationsTable.duration], - thumbnail = it[MoodRecommendationsTable.thumbnail], - state = it[MoodRecommendationsTable.state], - matchedSongId = it[MoodRecommendationsTable.matchedSongId], - timesAdded = it[MoodRecommendationsTable.timesAdded], - firstSeen = it[MoodRecommendationsTable.firstSeen], - lastSeen = it[MoodRecommendationsTable.lastSeen], - ) + .map { it.toRecommendationRecord() } + } + + fun recommendation(moodId: String, videoId: String): RecommendationRecord? = transaction { + MoodRecommendationsTable.selectAll() + .where { + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) } + .singleOrNull() + ?.toRecommendationRecord() + } + + /** + * Inserts new candidates and refreshes the metadata of known ones. Decisions + * (accepted/rejected) and the add counter survive a refresh; stale undecided + * candidates are removed so the review list does not grow without bound. + */ + fun storeRecommendations( + moodId: String, + items: List, + seenAt: String, + ): Int = transaction { + val existing = MoodRecommendationsTable.selectAll() + .where { MoodRecommendationsTable.moodId eq moodId } + .associateBy { it[MoodRecommendationsTable.videoId] } + var stored = 0 + + items.forEach { item -> + val row = existing[item.videoId] + if (row == null) { + MoodRecommendationsTable.insert { + it[MoodRecommendationsTable.moodId] = moodId + it[videoId] = item.videoId + it[title] = item.title.take(500) + it[artist] = item.artist.take(300) + it[album] = item.album?.take(300) + it[duration] = item.duration + it[thumbnail] = item.thumbnail?.take(500) + it[state] = "new" + it[matchedSongId] = item.matchedSongId + it[timesAdded] = 0 + it[firstSeen] = seenAt + it[lastSeen] = seenAt + } + } else { + MoodRecommendationsTable.update({ + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq item.videoId) + }) { + it[title] = item.title.take(500) + it[artist] = item.artist.take(300) + it[album] = item.album?.take(300) + it[duration] = item.duration + it[thumbnail] = item.thumbnail?.take(500) + it[matchedSongId] = item.matchedSongId + ?: row[MoodRecommendationsTable.matchedSongId] + it[lastSeen] = seenAt + } + } + stored += 1 + } + + val keep = items.map { it.videoId }.toSet() + val stale = existing.keys - keep + if (stale.isNotEmpty()) { + MoodRecommendationsTable.deleteWhere { + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId inList stale.toList()) and + (MoodRecommendationsTable.state eq "new") + } + } + stored + } + + fun setRecommendationState(moodId: String, videoId: String, state: String): Boolean = transaction { + MoodRecommendationsTable.update({ + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) + }) { + it[MoodRecommendationsTable.state] = state + } > 0 + } + + fun incrementRecommendationAdds(moodId: String, videoId: String) { + transaction { + val current = MoodRecommendationsTable.selectAll() + .where { + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) + } + .singleOrNull() + ?.get(MoodRecommendationsTable.timesAdded) + ?: return@transaction + MoodRecommendationsTable.update({ + (MoodRecommendationsTable.moodId eq moodId) and + (MoodRecommendationsTable.videoId eq videoId) + }) { + it[timesAdded] = current + 1 + } + } + } + + /** Moods whose YT discovery is due, oldest first. */ + fun staleDiscovery(now: Instant, limit: Int): List = transaction { + MoodsTable.selectAll() + .where { MoodsTable.ytEnabled eq true } + .orderBy(MoodsTable.lastDiscoveryAt to SortOrder.ASC) + .map { moodRecord(it) } + .filter { mood -> isStale(mood.lastDiscoveryAt, mood.refreshHours, now) } + .take(limit.coerceAtLeast(1)) } fun markError(id: String, message: String) { @@ -258,6 +366,26 @@ object MoodRepository { } } + private fun isStale(lastDiscoveryAt: String?, refreshHours: Int, now: Instant): Boolean { + if (lastDiscoveryAt == null) return true + val at = runCatching { Instant.parse(lastDiscoveryAt) }.getOrNull() ?: return true + return !at.plus(refreshHours.toLong().coerceAtLeast(1), ChronoUnit.HOURS).isAfter(now) + } + + private fun ResultRow.toRecommendationRecord() = RecommendationRecord( + videoId = this[MoodRecommendationsTable.videoId], + title = this[MoodRecommendationsTable.title], + artist = this[MoodRecommendationsTable.artist], + album = this[MoodRecommendationsTable.album], + duration = this[MoodRecommendationsTable.duration], + thumbnail = this[MoodRecommendationsTable.thumbnail], + state = this[MoodRecommendationsTable.state], + matchedSongId = this[MoodRecommendationsTable.matchedSongId], + timesAdded = this[MoodRecommendationsTable.timesAdded], + firstSeen = this[MoodRecommendationsTable.firstSeen], + lastSeen = this[MoodRecommendationsTable.lastSeen], + ) + private fun moodRecord(row: ResultRow): MoodRecord { val id = row[MoodsTable.id] val seedCount = MoodSeedsTable.selectAll() diff --git a/src/main/kotlin/win/taptap/musicmanager/db/RadioRepository.kt b/src/main/kotlin/win/taptap/musicmanager/db/RadioRepository.kt index 70518a2..a322516 100644 --- a/src/main/kotlin/win/taptap/musicmanager/db/RadioRepository.kt +++ b/src/main/kotlin/win/taptap/musicmanager/db/RadioRepository.kt @@ -3,6 +3,9 @@ package win.taptap.musicmanager.db import java.time.Instant import org.jetbrains.exposed.sql.SortOrder import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.greaterEq +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inList +import org.jetbrains.exposed.sql.SqlExpressionBuilder.inSubQuery import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.selectAll @@ -25,8 +28,8 @@ data class SessionItemRecord( ) data class AffinityRecord(val completions: Int, val skips: Int) { - /** Skips weigh double so a mood stops serving rejected tracks quickly. */ - val score: Double get() = completions - skips * 2.0 + /** Skips weigh triple so a mood stops serving rejected tracks quickly. */ + val score: Double get() = completions - skips * 3.0 } /** @@ -185,6 +188,28 @@ object RadioRepository { true } + /** + * Songs the user started, finished or skipped since [since]. Used to avoid + * serving the same tracks every day across sessions and moods. + */ + fun recentSongIds(userName: String, since: Instant, limit: Int = 500): Set = transaction { + val sessions = PlaybackSessionsTable + .select(PlaybackSessionsTable.id) + .where { PlaybackSessionsTable.userName eq userName } + + SessionItemsTable + .select(SessionItemsTable.songId) + .where { + (SessionItemsTable.sessionId inSubQuery sessions) and + (SessionItemsTable.state inList listOf("started", "completed", "skipped")) and + (SessionItemsTable.updatedAt greaterEq since.toString()) + } + .orderBy(SessionItemsTable.updatedAt to SortOrder.DESC) + .limit(limit) + .map { it[SessionItemsTable.songId] } + .toSet() + } + fun affinity(userName: String, songIds: Collection): Map { if (songIds.isEmpty()) return emptyMap() return transaction { diff --git a/src/main/kotlin/win/taptap/musicmanager/discovery/DiscoveryService.kt b/src/main/kotlin/win/taptap/musicmanager/discovery/DiscoveryService.kt new file mode 100644 index 0000000..f426567 --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/discovery/DiscoveryService.kt @@ -0,0 +1,220 @@ +package win.taptap.musicmanager.discovery + +import java.time.Instant +import java.time.temporal.ChronoUnit +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.async +import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.coroutineScope +import org.slf4j.LoggerFactory +import win.taptap.musicmanager.config.FolderType +import win.taptap.musicmanager.config.LibraryConfig +import win.taptap.musicmanager.config.UserAccount +import win.taptap.musicmanager.db.LibraryRepository +import win.taptap.musicmanager.db.MoodRecord +import win.taptap.musicmanager.db.MoodRepository +import win.taptap.musicmanager.db.RecommendationUpsert +import win.taptap.musicmanager.util.IdGenerator + +data class DiscoveryOutcome( + val stored: Int, + val matched: Int, + val skipped: Boolean = false, + val error: String? = null, +) + +/** + * Turns mood seeds, genres and free text into YouTube Music candidates. New + * candidates are stored for review; accepted ones either become seeds (when the + * library already has the track) or wait for the future downloader. + */ +class DiscoveryService( + private val search: YtSearch, + private val clock: () -> Instant = { Instant.now() }, +) { + private val log = LoggerFactory.getLogger(DiscoveryService::class.java) + + suspend fun refresh( + mood: MoodRecord, + user: UserAccount, + library: LibraryConfig, + force: Boolean = false, + ): DiscoveryOutcome { + if (!mood.ytEnabled) return DiscoveryOutcome(0, 0, skipped = true) + + val now = clock() + if (!force && isFresh(mood.lastDiscoveryAt, mood.refreshHours, now)) { + return DiscoveryOutcome(0, 0, skipped = true) + } + + val folders = library.folders + .asSequence() + .filter { it.type == FolderType.MUSIC && it.id in user.folders } + .map { it.id } + .toSet() + if (folders.isEmpty()) { + MoodRepository.markError(mood.id, "No music folder is visible to this user") + return DiscoveryOutcome(0, 0, error = "No music folder is visible to this user") + } + + val seeds = LibraryRepository.songsByIds(MoodRepository.seedSongIds(mood.id), folders, user.name) + val seedIds = seeds.map { it.id }.toSet() + val seedArtists = seeds.map { it.displayArtist }.filter { it.isNotBlank() } + val queries = buildQueries(mood, seedArtists) + if (queries.isEmpty()) { + MoodRepository.markDiscovered(mood.id) + return DiscoveryOutcome(0, 0) + } + + // Queries are independent; running them together keeps a refresh within + // the client's request timeout even when yt-dlp is slow. + val results = coroutineScope { + queries.map { query -> + async(Dispatchers.IO) { + runCatching { search.search(query, QUERY_LIMIT) } + } + }.awaitAll() + } + val failures = results.filter { it.isFailure } + if (failures.size == queries.size) { + val message = failures.firstNotNullOfOrNull { it.exceptionOrNull()?.message } + ?: "Search failed" + log.warn("YT discovery failed for mood {}: {}", mood.name, message) + MoodRepository.markError(mood.id, message) + return DiscoveryOutcome(0, 0, error = message) + } + + val candidates = LinkedHashMap() + results.mapNotNull { it.getOrNull() }.flatten().forEach { candidate -> + if (isMusicLength(candidate.duration)) { + candidates.putIfAbsent(candidate.videoId, candidate) + } + } + + val known = MoodRepository.recommendations(mood.id).associateBy { it.videoId } + val moodText = listOfNotNull(mood.query, mood.description, mood.name) + .joinToString(" ") + + val upserts = candidates.values + .mapNotNull { candidate -> + val existing = known[candidate.videoId] + // Accepted or rejected tracks keep their decision and are never + // offered again; only last_seen is refreshed by the upsert. + val matched = existing?.matchedSongId ?: matchSong(candidate, folders, user.name) + if (existing == null && matched != null && matched in seedIds) { + return@mapNotNull null + } + UpsertCandidate( + candidate = candidate, + matchedSongId = matched, + score = scoreCandidate(candidate, seedArtists, moodText, matched != null), + ) + } + .sortedWith(compareByDescending { it.score }.thenBy { it.candidate.title }) + .take(mood.targetSize.coerceIn(10, 60)) + .map { + RecommendationUpsert( + videoId = it.candidate.videoId, + title = it.candidate.title, + artist = it.candidate.artist, + album = it.candidate.album, + duration = it.candidate.duration, + thumbnail = it.candidate.thumbnail, + matchedSongId = it.matchedSongId, + ) + } + + val stored = MoodRepository.storeRecommendations(mood.id, upserts, now.toString()) + MoodRepository.markDiscovered(mood.id) + log.info("YT discovery for mood {} stored {} recommendations", mood.name, stored) + return DiscoveryOutcome( + stored = stored, + matched = upserts.count { it.matchedSongId != null }, + ) + } + + private fun matchSong(candidate: YtCandidate, folders: Set, userName: String): String? { + val titleKey = matchKey(candidate.title) + if (titleKey.isEmpty()) return null + return LibraryRepository + .searchSongs(candidate.title, MATCH_LIMIT, 0, folders, userName) + .firstOrNull { song -> + matchKey(song.title) == titleKey && artistMatches(candidate.artist, song.displayArtist) + } + ?.id + } + + private data class UpsertCandidate( + val candidate: YtCandidate, + val matchedSongId: String?, + val score: Double, + ) + + private companion object { + const val QUERY_LIMIT = 10 + const val MATCH_LIMIT = 25 + } +} + +internal fun isFresh(lastDiscoveryAt: String?, refreshHours: Int, now: Instant): Boolean { + if (lastDiscoveryAt == null) return false + val at = runCatching { Instant.parse(lastDiscoveryAt) }.getOrNull() ?: return false + return at.plus(refreshHours.toLong().coerceAtLeast(1), ChronoUnit.HOURS).isAfter(now) +} + +internal fun isMusicLength(seconds: Int?): Boolean = + seconds == null || seconds in 45..(20 * 60) + +internal fun buildQueries(mood: MoodRecord, seedArtists: List): List { + val queries = LinkedHashSet() + mood.query?.trim()?.takeIf { it.isNotEmpty() }?.let(queries::add) + seedArtists.map { it.trim() }.filter { it.isNotEmpty() }.distinct().take(2).forEach { + queries += it + } + mood.genres.map { it.trim() }.filter { it.isNotEmpty() }.take(2).forEach { + queries += "$it mix" + } + if (queries.isEmpty()) queries += mood.name + return queries.distinctBy { it.lowercase() }.take(4) +} + +internal fun scoreCandidate( + candidate: YtCandidate, + seedArtists: List, + moodText: String, + matched: Boolean, +): Double { + var score = 0.0 + val artist = matchKey(candidate.artist) + val seedKeys = seedArtists.map { matchKey(it) }.filter { it.isNotEmpty() } + when { + artist.isNotEmpty() && artist in seedKeys -> score += 3.0 + artist.isNotEmpty() && seedKeys.any { artist.contains(it) || it.contains(artist) } -> score += 1.5 + } + val text = matchKey(moodText) + if (text.isNotEmpty() && matchKey(candidate.title).contains(text)) score += 1.0 + if (matched) score += 1.5 + if ((candidate.duration ?: 0) > 8 * 60) score -= 1.0 + return score +} + +private val BRACKETS = Regex("\\([^)]*\\)|\\[[^\\]]*\\]") +private val NON_WORD = Regex("[^\\p{L}\\p{N} ]+") +private val SPACES = Regex("\\s+") +private val FEATURING = Regex("\\b(feat|ft|featuring)\\b.*$", RegexOption.IGNORE_CASE) + +/** Loose key for comparing YT titles and artist names with library tags. */ +internal fun matchKey(value: String): String = + IdGenerator.normalize(value) + .replace(BRACKETS, " ") + .replace(FEATURING, " ") + .replace(NON_WORD, " ") + .replace(SPACES, " ") + .trim() + +internal fun artistMatches(a: String, b: String): Boolean { + val left = matchKey(a) + val right = matchKey(b) + if (left.isEmpty() || right.isEmpty()) return false + return left == right || left.contains(right) || right.contains(left) +} diff --git a/src/main/kotlin/win/taptap/musicmanager/discovery/YtSearch.kt b/src/main/kotlin/win/taptap/musicmanager/discovery/YtSearch.kt new file mode 100644 index 0000000..7924ea4 --- /dev/null +++ b/src/main/kotlin/win/taptap/musicmanager/discovery/YtSearch.kt @@ -0,0 +1,130 @@ +package win.taptap.musicmanager.discovery + +import java.util.concurrent.TimeUnit +import kotlin.concurrent.thread +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonObject +import kotlinx.serialization.json.JsonPrimitive +import kotlinx.serialization.json.contentOrNull +import kotlinx.serialization.json.jsonArray +import kotlinx.serialization.json.jsonObject + +/** One YouTube search hit, already split into an artist and a track title. */ +data class YtCandidate( + val videoId: String, + val title: String, + val artist: String, + val album: String? = null, + val duration: Int? = null, + val thumbnail: String? = null, +) + +fun interface YtSearch { + fun search(query: String, limit: Int): List +} + +/** + * Searches YouTube through `yt-dlp`. Flat playlist output avoids resolving every + * video, which keeps a discovery run to a single request per query. + */ +class YtDlpSearch( + private val executable: String, + private val timeoutSeconds: Long = 30, +) : YtSearch { + + override fun search(query: String, limit: Int): List { + val process = ProcessBuilder( + executable, + "--flat-playlist", + "--dump-single-json", + "--no-warnings", + "--ignore-errors", + "--no-cache-dir", + "--socket-timeout", + "10", + "ytsearch${limit.coerceIn(1, 25)}:$query", + ) + .redirectErrorStream(true) + .start() + + val output = StringBuilder() + val reader = thread(isDaemon = true, name = "yt-dlp-search") { + process.inputStream.bufferedReader().useLines { lines -> + lines.forEach { output.appendLine(it) } + } + } + + if (!process.waitFor(timeoutSeconds, TimeUnit.SECONDS)) { + process.destroyForcibly() + reader.join(2_000) + throw IllegalStateException("yt-dlp timed out after ${timeoutSeconds}s") + } + reader.join(2_000) + if (process.exitValue() != 0) { + val detail = output.toString().take(400).trim() + throw IllegalStateException("yt-dlp exited with ${process.exitValue()}: $detail") + } + return parseYtSearchOutput(output.toString()) + } +} + +internal fun parseYtSearchOutput(json: String): List { + if (json.isBlank()) return emptyList() + val root = runCatching { Json.parseToJsonElement(json).jsonObject }.getOrNull() ?: return emptyList() + val entries = runCatching { root["entries"]?.jsonArray }.getOrNull() ?: return emptyList() + + return entries.mapNotNull { element -> + val entry = runCatching { element.jsonObject }.getOrNull() ?: return@mapNotNull null + val videoId = entry.string("id")?.takeIf { it.isNotBlank() } ?: return@mapNotNull null + val rawTitle = entry.string("title")?.takeIf { it.isNotBlank() } ?: return@mapNotNull null + val channel = entry.string("channel") ?: entry.string("uploader").orEmpty() + val (artist, title) = splitYtTitle(rawTitle, channel) + + YtCandidate( + videoId = videoId, + title = title.ifEmpty { rawTitle }, + artist = artist.ifEmpty { "Unknown artist" }, + album = entry.string("album"), + duration = entry.number("duration")?.toInt(), + thumbnail = entry.thumbnail(), + ) + } +} + +private const val NOISE_WORDS = "official|lyrics?|audio|video|visuali[sz]er|mv|hd|4k|remaster(?:ed)?" + +private val TITLE_NOISE = Regex( + "\\((?:$NOISE_WORDS)[^)]*\\)" + + "|\\[(?:$NOISE_WORDS)[^\\]]*\\]" + + "|\\s*\\|\\s*(?:$NOISE_WORDS).*$", + RegexOption.IGNORE_CASE, +) + +/** "Artist - Track (Official Video)" becomes ("Artist", "Track"). */ +internal fun splitYtTitle(rawTitle: String, channel: String): Pair { + val cleaned = rawTitle.replace(TITLE_NOISE, "").trim() + for (separator in listOf(" - ", " – ", " — ")) { + val index = cleaned.indexOf(separator) + if (index > 0) { + val artist = cleaned.substring(0, index).trim() + val title = cleaned.substring(index + separator.length).trim() + if (artist.isNotEmpty() && title.isNotEmpty()) return artist to title + } + } + val artist = channel.removeSuffix(" - Topic").trim() + return artist to cleaned +} + +private fun JsonObject.string(key: String): String? = + (this[key] as? JsonPrimitive)?.contentOrNull + +private fun JsonObject.number(key: String): Double? = + (this[key] as? JsonPrimitive)?.contentOrNull?.toDoubleOrNull() + +private fun JsonObject.thumbnail(): String? { + val thumbnails = runCatching { get("thumbnails")?.jsonArray }.getOrNull() + val last = thumbnails + ?.mapNotNull { runCatching { it.jsonObject.string("url") }.getOrNull() } + ?.lastOrNull() + return last ?: string("thumbnail") +} diff --git a/src/main/kotlin/win/taptap/musicmanager/radio/RadioService.kt b/src/main/kotlin/win/taptap/musicmanager/radio/RadioService.kt index 0abc2ec..239087b 100644 --- a/src/main/kotlin/win/taptap/musicmanager/radio/RadioService.kt +++ b/src/main/kotlin/win/taptap/musicmanager/radio/RadioService.kt @@ -1,5 +1,7 @@ package win.taptap.musicmanager.radio +import java.time.Instant +import java.time.temporal.ChronoUnit import java.util.UUID import kotlin.math.max import kotlin.random.Random @@ -140,15 +142,27 @@ class RadioService { } } + val recent = RadioRepository.recentSongIds( + user.name, + Instant.now().minus(RECENT_DAYS, ChronoUnit.DAYS), + ) + // In a normal-sized library, skip songs from the last week entirely. + // Small libraries keep them as low-scored fallbacks instead of going silent. + if (candidates.size > limit * 3) { + candidates.keys.removeAll(recent - seedIds) + } + val affinity = RadioRepository.affinity(user.name, candidates.keys) val scored = candidates.values.map { song -> - song to score( + song to scoreCandidate( song = song, seedIds = seedIds, seedGenres = seedGenres, seedArtistIds = seedArtistIds, query = query, affinity = affinity[song.id]?.score ?: 0.0, + recent = recent, + jitter = random.nextDouble(0.0, 2.0), ) } @@ -159,50 +173,69 @@ class RadioService { return picks } - private fun score( + internal fun scoreCandidate( song: SongRecord, seedIds: Set, seedGenres: List, seedArtistIds: List, query: String?, affinity: Double, + recent: Set = emptySet(), + jitter: Double = 0.0, ): Double { - var score = random.nextDouble(0.0, 2.0) + var score = jitter if (song.id in seedIds) score += 10.0 if (song.artistId != null && song.artistId in seedArtistIds) score += 3.0 score += song.genres.count { candidate -> seedGenres.any { it.equals(candidate, ignoreCase = true) } } * 1.5 if (query != null && song.title.contains(query, ignoreCase = true)) score += 2.0 - score += affinity.coerceIn(-6.0, 6.0) + score += affinity.coerceIn(-8.0, 8.0) + if (song.id in recent) score -= RECENT_PENALTY return score } /** - * Picks the best remaining candidate, preferring a different artist than the - * previous track so a batch does not become one album on repeat. + * Picks the best remaining candidate while keeping artists and albums apart. + * An artist may return after [artistSpacing] other tracks, an album after + * [albumSpacing]. Fallbacks relax the constraints rather than stalling. */ - private fun pickDiverse( + internal fun pickDiverse( scored: List>, limit: Int, + artistSpacing: Int = 2, + albumSpacing: Int = 3, ): List { val remaining = scored .sortedWith(compareByDescending> { it.second }) .toMutableList() val picked = ArrayList(limit) - var lastArtist: String? = null + val artists = ArrayDeque() + val albums = ArrayDeque() while (picked.size < limit && remaining.isNotEmpty()) { - val different = remaining.indexOfFirst { it.first.displayArtist != lastArtist } - val index = if (different >= 0) different else 0 + var index = remaining.indexOfFirst { (song, _) -> + song.displayArtist !in artists && song.albumId !in albums + } + if (index < 0) { + index = remaining.indexOfFirst { (song, _) -> song.displayArtist !in artists } + } + if (index < 0) index = 0 + val song = remaining.removeAt(index).first picked += song - lastArtist = song.displayArtist + + artists.addLast(song.displayArtist) + if (artists.size > artistSpacing) artists.removeFirst() + albums.addLast(song.albumId) + if (albums.size > albumSpacing) albums.removeFirst() } return picked } private companion object { const val POOL_LIMIT = 400 + const val RECENT_DAYS = 7L + const val RECENT_PENALTY = 6.0 } } diff --git a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt index e638e65..72aded9 100644 --- a/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt +++ b/src/test/kotlin/win/taptap/musicmanager/api/ApiRoutesTest.kt @@ -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) }, ) diff --git a/src/test/kotlin/win/taptap/musicmanager/discovery/DiscoveryServiceTest.kt b/src/test/kotlin/win/taptap/musicmanager/discovery/DiscoveryServiceTest.kt new file mode 100644 index 0000000..3936813 --- /dev/null +++ b/src/test/kotlin/win/taptap/musicmanager/discovery/DiscoveryServiceTest.kt @@ -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 = 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, + ) +} diff --git a/src/test/kotlin/win/taptap/musicmanager/radio/RadioServiceTest.kt b/src/test/kotlin/win/taptap/musicmanager/radio/RadioServiceTest.kt new file mode 100644 index 0000000..2a0861b --- /dev/null +++ b/src/test/kotlin/win/taptap/musicmanager/radio/RadioServiceTest.kt @@ -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 = 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, + ) +}