feat: rebuild AudioManager around downloadable moods
- V8 wipe migration and the shared song store keyed by YouTube video id - media store, indexer, downloader, recommendation top-up and refcounted GC - mood songs plus the recommendation lifecycle and mixed radio pools - new search, song and recommendation API; remove library/longform/seed routes - chart PVC/media env, docs, Nix, README and an end-to-end smoke test
This commit is contained in:
1 parent
50d8b6c580
commit
824eb36dd2
59 files changed
+3838
-5276
No files matched your search
@@ -7,10 +7,6 @@ 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
|
||||
@@ -18,15 +14,14 @@ 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.
|
||||
* Turns a mood's collection, genres and free text into YouTube candidates.
|
||||
* Candidates are stored as hidden `new` rows; [win.taptap.musicmanager.discovery.TopUpService]
|
||||
* promotes them into active download slots.
|
||||
*/
|
||||
class DiscoveryService(
|
||||
private val search: YtSearch,
|
||||
@@ -34,36 +29,20 @@ class DiscoveryService(
|
||||
) {
|
||||
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)
|
||||
suspend fun refresh(mood: MoodRecord, force: Boolean = false): DiscoveryOutcome {
|
||||
if (mood.recommendationCount <= 0) return DiscoveryOutcome(0, skipped = true)
|
||||
|
||||
val now = clock()
|
||||
if (!force && isFresh(mood.lastDiscoveryAt, mood.refreshHours, now)) {
|
||||
return DiscoveryOutcome(0, 0, skipped = true)
|
||||
return DiscoveryOutcome(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 collection = MoodRepository.songRecords(mood.id)
|
||||
val seedArtists = collection.map { it.artist }.filter { it.isNotBlank() }
|
||||
val queries = buildQueries(mood, seedArtists)
|
||||
if (queries.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
return DiscoveryOutcome(0, 0)
|
||||
return DiscoveryOutcome(0)
|
||||
}
|
||||
|
||||
// Queries are independent; running them together keeps a refresh within
|
||||
@@ -81,7 +60,7 @@ class DiscoveryService(
|
||||
?: "Search failed"
|
||||
log.warn("YT discovery failed for mood {}: {}", mood.name, message)
|
||||
MoodRepository.markError(mood.id, message)
|
||||
return DiscoveryOutcome(0, 0, error = message)
|
||||
return DiscoveryOutcome(0, error = message)
|
||||
}
|
||||
|
||||
val candidates = LinkedHashMap<String, YtCandidate>()
|
||||
@@ -94,71 +73,52 @@ class DiscoveryService(
|
||||
if (candidates.isEmpty()) {
|
||||
val message = "YouTube returned no usable results"
|
||||
MoodRepository.markError(mood.id, message)
|
||||
return DiscoveryOutcome(0, 0, error = message)
|
||||
return DiscoveryOutcome(0, error = message)
|
||||
}
|
||||
|
||||
val known = MoodRepository.recommendations(mood.id).associateBy { it.videoId }
|
||||
val moodText = listOfNotNull(mood.query, mood.description, mood.name)
|
||||
.joinToString(" ")
|
||||
val excluded = MoodRepository.videoIdsInMood(mood.id) + MoodRepository.decidedVideoIds(mood.id) +
|
||||
MoodRepository.allRecommendations(mood.id)
|
||||
.filter { it.state != "new" }
|
||||
.map { it.videoId }
|
||||
.toSet()
|
||||
|
||||
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),
|
||||
)
|
||||
.filterNot { it.videoId in excluded }
|
||||
.map { candidate ->
|
||||
candidate to scoreCandidate(candidate, seedArtists, moodText)
|
||||
}
|
||||
.sortedWith(compareByDescending<UpsertCandidate> { it.score }.thenBy { it.candidate.title })
|
||||
.take(mood.targetSize.coerceIn(10, 60))
|
||||
.map {
|
||||
.sortedWith(
|
||||
compareByDescending<Pair<YtCandidate, Double>> { it.second }
|
||||
.thenBy { it.first.title },
|
||||
)
|
||||
.take(CANDIDATE_POOL)
|
||||
.map { (candidate, _) ->
|
||||
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,
|
||||
videoId = candidate.videoId,
|
||||
title = candidate.title,
|
||||
artist = candidate.artist,
|
||||
album = candidate.album,
|
||||
duration = candidate.duration,
|
||||
thumbnail = candidate.thumbnail,
|
||||
)
|
||||
}
|
||||
|
||||
val stored = MoodRepository.storeRecommendations(mood.id, upserts, now.toString())
|
||||
if (upserts.isEmpty()) {
|
||||
MoodRepository.markDiscovered(mood.id)
|
||||
return DiscoveryOutcome(0)
|
||||
}
|
||||
|
||||
val stored = MoodRepository.storeCandidates(mood.id, upserts, now.toString(), CANDIDATE_POOL)
|
||||
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 },
|
||||
)
|
||||
log.info("YT discovery for mood {} stored {} candidates", mood.name, stored)
|
||||
return DiscoveryOutcome(stored = stored)
|
||||
}
|
||||
|
||||
private fun matchSong(candidate: YtCandidate, folders: Set<Int>, 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
|
||||
const val CANDIDATE_POOL = 60
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,8 +128,9 @@ internal fun isFresh(lastDiscoveryAt: String?, refreshHours: Int, now: Instant):
|
||||
return at.plus(refreshHours.toLong().coerceAtLeast(1), ChronoUnit.HOURS).isAfter(now)
|
||||
}
|
||||
|
||||
/** Search and discovery only accept music-length tracks: 30 seconds to 10 minutes. */
|
||||
internal fun isMusicLength(seconds: Int?): Boolean =
|
||||
seconds == null || seconds in 45..(20 * 60)
|
||||
seconds == null || seconds in MIN_TRACK_SECONDS..MAX_TRACK_SECONDS
|
||||
|
||||
internal fun buildQueries(mood: MoodRecord, seedArtists: List<String>): List<String> {
|
||||
val queries = LinkedHashSet<String>()
|
||||
@@ -188,7 +149,6 @@ internal fun scoreCandidate(
|
||||
candidate: YtCandidate,
|
||||
seedArtists: List<String>,
|
||||
moodText: String,
|
||||
matched: Boolean,
|
||||
): Double {
|
||||
var score = 0.0
|
||||
val artist = matchKey(candidate.artist)
|
||||
@@ -199,7 +159,6 @@ internal fun scoreCandidate(
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -209,7 +168,7 @@ 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. */
|
||||
/** Loose key for comparing YT titles and artist names with downloaded tags. */
|
||||
internal fun matchKey(value: String): String =
|
||||
IdGenerator.normalize(value)
|
||||
.replace(BRACKETS, " ")
|
||||
@@ -227,3 +186,6 @@ internal fun artistMatches(a: String, b: String): Boolean {
|
||||
if (left.length < 3 || right.length < 3) return false
|
||||
return " $left ".contains(" $right ") || " $right ".contains(" $left ")
|
||||
}
|
||||
|
||||
internal const val MIN_TRACK_SECONDS = 30
|
||||
internal const val MAX_TRACK_SECONDS = 600
|
||||
Reference in new issue
Block a user