feat: discover YT recommendations and tune the radio
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:
1 parent
67eed8d262
commit
c5aa000d1a
18 files changed
+1061
-37
No files matched your search
@@ -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<RecommendationUpsert>,
|
||||
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<MoodRecord> = 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()
|
||||
|
||||
Reference in new issue
Block a user