feat: add Deezer as the default download source
CI / chart (push) Successful in 3s
CI / test (push) Successful in 2m23s
CI / chart-push (push) Successful in 5s
CI / image (push) Successful in 3m50s

- V9 adds source/external_id to songs, moods, recommendations and jobs
- Deezer public catalog for search, related album covers and metadata
- streamrip downloader configured with DEEZER_ARL, 128/320 MP3
- per-mood source selector; switching clears and refills recommendations
- keep YouTube Music as a full second catalog and downloader
- media layout <source>-<externalId>, legacy YouTube folders still read
- Docker/streamrip install, chart ARL secret, docs and dual-source smoke test
This commit is contained in:
TapTap committed 2026-11-17 07:32:34 +00:00
1 parent 81a47f4cd2
commit 141698282d
43 files changed
+2072 -605

No files matched your search

@@ -18,6 +18,7 @@ import org.jetbrains.exposed.sql.insertIgnore
import org.jetbrains.exposed.sql.selectAll
import org.jetbrains.exposed.sql.transactions.transaction
import org.jetbrains.exposed.sql.update
import win.taptap.musicmanager.source.MusicSource
import win.taptap.musicmanager.util.IdGenerator
data class MoodRecord(
@@ -27,6 +28,7 @@ data class MoodRecord(
val description: String?,
val query: String?,
val genres: List<String>,
val source: String,
val collectionPercent: Int,
val recommendationCount: Int,
val refreshHours: Int,
@@ -47,7 +49,8 @@ data class MoodSongRecord(
)
data class RecommendationUpsert(
val videoId: String,
val source: String,
val externalId: String,
val title: String,
val artist: String,
val album: String?,
@@ -56,7 +59,8 @@ data class RecommendationUpsert(
)
data class RecommendationRecord(
val videoId: String,
val source: String,
val externalId: String,
val title: String,
val artist: String,
val album: String?,
@@ -79,6 +83,10 @@ enum class RecommendationCompletion { READY, ALREADY_ADDED, REJECTED, MISSING }
* recommendation table also holds hidden `new` candidates and the lifecycle of
* the active slots. The play queue is generated by [win.taptap.musicmanager.radio.RadioService]
* for every playback session instead of being stored as a playlist.
*
* A mood has one download source at a time. Switching it clears the active and
* hidden recommendations of the old source; permanent songs and the
* added/rejected history stay.
*/
object MoodRepository {
@@ -87,6 +95,7 @@ object MoodRepository {
private val ACTIVE_STATES = listOf("downloading", "ready", "failed")
private val VISIBLE_STATES = ACTIVE_STATES
private val HIDDEN_DECISIONS = listOf("added", "rejected")
private val CLEARED_ON_SOURCE_CHANGE = listOf("new", "downloading", "ready", "failed")
fun list(owner: String): List<MoodRecord> = transaction {
MoodsTable.selectAll()
@@ -114,6 +123,7 @@ object MoodRepository {
description: String?,
query: String?,
genres: List<String>,
source: String,
collectionPercent: Int,
recommendationCount: Int,
refreshHours: Int,
@@ -128,6 +138,7 @@ object MoodRepository {
it[MoodsTable.description] = description?.take(2000)
it[MoodsTable.query] = query?.trim()?.takeIf(String::isNotEmpty)
it[MoodsTable.genres] = encodeGenres(genres)
it[MoodsTable.musicSource] = source
it[MoodsTable.collectionPercent] = collectionPercent.coerceIn(0, 100)
it[MoodsTable.recommendationCount] = recommendationCount.coerceIn(0, 30)
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
@@ -145,24 +156,41 @@ object MoodRepository {
description: String?,
query: String?,
genres: List<String>,
source: String,
collectionPercent: Int,
recommendationCount: Int,
refreshHours: Int,
): MoodRecord? = transaction {
val exists = MoodsTable.selectAll()
val current = MoodsTable.selectAll()
.where { (MoodsTable.id eq id) and (MoodsTable.owner eq owner) }
.any()
if (!exists) return@transaction null
.singleOrNull()
?: return@transaction null
val sourceChanged = current[MoodsTable.musicSource] != source
if (sourceChanged) {
// Candidates are tied to the old catalog. Keep permanent songs and
// the added/rejected decisions, but drop the active pool so it can
// be refilled from the new source.
MoodRecommendationsTable.deleteWhere {
(MoodRecommendationsTable.moodId eq id) and
(MoodRecommendationsTable.state inList CLEARED_ON_SOURCE_CHANGE)
}
}
MoodsTable.update({ MoodsTable.id eq id }) {
it[MoodsTable.name] = name.take(200)
it[MoodsTable.description] = description?.take(2000)
it[MoodsTable.query] = query?.trim()?.takeIf(String::isNotEmpty)
it[MoodsTable.genres] = encodeGenres(genres)
it[MoodsTable.musicSource] = source
it[MoodsTable.collectionPercent] = collectionPercent.coerceIn(0, 100)
it[MoodsTable.recommendationCount] = recommendationCount.coerceIn(0, 30)
it[MoodsTable.refreshHours] = refreshHours.coerceIn(1, 720)
it[MoodsTable.changedAt] = Instant.now().toString()
if (sourceChanged) {
it[lastDiscoveryAt] = null
it[lastError] = null
}
}
moodRecord(MoodsTable.selectAll().where { MoodsTable.id eq id }.single())
@@ -222,19 +250,18 @@ object MoodRepository {
// A track that is now permanently in the mood must not also linger as a
// hidden candidate or an active recommendation slot.
val videoId = SongsTable.select(SongsTable.videoId)
.where { SongsTable.id eq songId }
.singleOrNull()
?.get(SongsTable.videoId)
if (videoId != null) {
val song = SongRepository.byId(songId)
if (song != null) {
MoodRecommendationsTable.deleteWhere {
(MoodRecommendationsTable.moodId eq id) and
(MoodRecommendationsTable.videoId eq videoId) and
(MoodRecommendationsTable.musicSource eq song.source) and
(MoodRecommendationsTable.externalId eq song.externalId) and
(MoodRecommendationsTable.state eq "new")
}
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq id) and
(MoodRecommendationsTable.videoId eq videoId) and
(MoodRecommendationsTable.musicSource eq song.source) and
(MoodRecommendationsTable.externalId eq song.externalId) and
(MoodRecommendationsTable.state inList listOf("downloading", "ready", "failed"))
}) {
it[state] = "added"
@@ -288,11 +315,11 @@ object MoodRepository {
.map { it.toRecommendationRecord() }
}
fun recommendation(moodId: String, videoId: String): RecommendationRecord? = transaction {
fun recommendation(moodId: String, externalId: String): RecommendationRecord? = transaction {
MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
(MoodRecommendationsTable.externalId eq externalId)
}
.singleOrNull()
?.toRecommendationRecord()
@@ -308,22 +335,26 @@ object MoodRepository {
.toInt()
}
fun videoIdsInMood(moodId: String): Set<String> = transaction {
fun externalIdsInMood(moodId: String, source: String): Set<String> = transaction {
val songs = MoodSongsTable.select(MoodSongsTable.songId)
.where { MoodSongsTable.moodId eq moodId }
SongsTable.select(SongsTable.videoId)
.where { SongsTable.id inSubQuery songs }
.map { it[SongsTable.videoId] }
SongsTable.select(SongsTable.externalId)
.where {
(SongsTable.id inSubQuery songs) and
(SongsTable.musicSource eq source)
}
.map { it[SongsTable.externalId] }
.toSet()
}
fun decidedVideoIds(moodId: String): Set<String> = transaction {
fun decidedExternalIds(moodId: String, source: String): Set<String> = transaction {
MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.musicSource eq source) and
(MoodRecommendationsTable.state inList HIDDEN_DECISIONS)
}
.map { it[MoodRecommendationsTable.videoId] }
.map { it[MoodRecommendationsTable.externalId] }
.toSet()
}
@@ -333,21 +364,26 @@ object MoodRepository {
*/
fun storeCandidates(
moodId: String,
source: String,
items: List<RecommendationUpsert>,
seenAt: String,
poolLimit: Int = CANDIDATE_POOL,
): Int = transaction {
val existing = MoodRecommendationsTable.selectAll()
.where { MoodRecommendationsTable.moodId eq moodId }
.associateBy { it[MoodRecommendationsTable.videoId] }
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.musicSource eq source)
}
.associateBy { it[MoodRecommendationsTable.externalId] }
var stored = 0
items.take(poolLimit.coerceAtLeast(1)).forEachIndexed { index, item ->
val row = existing[item.videoId]
val row = existing[item.externalId]
if (row == null) {
MoodRecommendationsTable.insert {
it[MoodRecommendationsTable.moodId] = moodId
it[videoId] = item.videoId
it[MoodRecommendationsTable.musicSource] = source
it[externalId] = item.externalId
it[title] = item.title.take(500)
it[artist] = item.artist.take(300)
it[album] = item.album?.take(300)
@@ -361,7 +397,7 @@ object MoodRepository {
} else if (row[MoodRecommendationsTable.state] == "new") {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq item.videoId)
(MoodRecommendationsTable.externalId eq item.externalId)
}) {
it[title] = item.title.take(500)
it[artist] = item.artist.take(300)
@@ -376,49 +412,52 @@ object MoodRepository {
}
// Candidates that were not returned again are no longer useful.
val keep = items.take(poolLimit.coerceAtLeast(1)).map { it.videoId }.toSet()
val keep = items.take(poolLimit.coerceAtLeast(1)).map { it.externalId }.toSet()
if (keep.isNotEmpty()) {
MoodRecommendationsTable.deleteWhere {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.musicSource eq source) and
(MoodRecommendationsTable.state eq "new") and
(MoodRecommendationsTable.videoId notInList keep.toList())
(MoodRecommendationsTable.externalId notInList keep.toList())
}
}
stored
}
/** Moves up to [limit] hidden candidates into the downloading state. */
fun promoteCandidates(moodId: String, limit: Int): List<RecommendationRecord> = transaction {
if (limit <= 0) return@transaction emptyList()
val rows = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.state eq "new")
}
.orderBy(
MoodRecommendationsTable.rank to SortOrder.ASC,
MoodRecommendationsTable.firstSeen to SortOrder.ASC,
)
.limit(limit)
.toList()
fun promoteCandidates(moodId: String, source: String, limit: Int): List<RecommendationRecord> =
transaction {
if (limit <= 0) return@transaction emptyList()
val rows = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.musicSource eq source) and
(MoodRecommendationsTable.state eq "new")
}
.orderBy(
MoodRecommendationsTable.rank to SortOrder.ASC,
MoodRecommendationsTable.firstSeen to SortOrder.ASC,
)
.limit(limit)
.toList()
rows.forEach { row ->
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq row[MoodRecommendationsTable.videoId])
}) {
it[state] = "downloading"
it[error] = null
rows.forEach { row ->
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.externalId eq row[MoodRecommendationsTable.externalId])
}) {
it[state] = "downloading"
it[error] = null
}
}
rows.map { it.toRecommendationRecord().copy(state = "downloading", error = null) }
}
rows.map { it.toRecommendationRecord().copy(state = "downloading", error = null) }
}
fun setRecommendationState(moodId: String, videoId: String, state: String, error: String? = null) {
fun setRecommendationState(moodId: String, externalId: String, state: String, error: String? = null) {
transaction {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
(MoodRecommendationsTable.externalId eq externalId)
}) {
it[this.state] = state
it[this.error] = error?.take(1000)
@@ -426,11 +465,11 @@ object MoodRepository {
}
}
fun markRecommendationDownloading(moodId: String, videoId: String) {
fun markRecommendationDownloading(moodId: String, externalId: String) {
transaction {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
(MoodRecommendationsTable.externalId eq externalId)
}) {
it[state] = "downloading"
it[attempts] = 0
@@ -439,11 +478,11 @@ object MoodRepository {
}
}
fun failRecommendation(moodId: String, videoId: String, error: String, attempts: Int) {
fun failRecommendation(moodId: String, externalId: String, error: String, attempts: Int) {
transaction {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId) and
(MoodRecommendationsTable.externalId eq externalId) and
(MoodRecommendationsTable.state eq "downloading")
}) {
it[state] = "failed"
@@ -457,50 +496,55 @@ object MoodRepository {
* Applies a finished download. A rejection that happened while the download
* was running wins: the file is left for the GC to delete.
*/
fun completeDownload(moodId: String, videoId: String, songId: String): RecommendationCompletion =
transaction {
val row = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
}
.singleOrNull()
?: return@transaction RecommendationCompletion.MISSING
when (row[MoodRecommendationsTable.state]) {
"rejected" -> RecommendationCompletion.REJECTED
"added" -> {
insertRecommendationSong(moodId, songId)
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
}) {
it[this.songId] = songId
it[state] = "added"
}
RecommendationCompletion.ALREADY_ADDED
}
else -> {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
}) {
it[this.songId] = songId
it[state] = "ready"
it[error] = null
it[lastSeen] = Instant.now().toString()
}
RecommendationCompletion.READY
}
}
}
/** Called when the user presses Add. Marks the row and attaches a ready song. */
fun markAdded(moodId: String, videoId: String): RecommendationCompletion = transaction {
fun completeDownload(
moodId: String,
source: String,
externalId: String,
songId: String,
): RecommendationCompletion = transaction {
val row = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
(MoodRecommendationsTable.musicSource eq source) and
(MoodRecommendationsTable.externalId eq externalId)
}
.singleOrNull()
?: return@transaction RecommendationCompletion.MISSING
when (row[MoodRecommendationsTable.state]) {
"rejected" -> RecommendationCompletion.REJECTED
"added" -> {
insertRecommendationSong(moodId, songId)
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.externalId eq externalId)
}) {
it[this.songId] = songId
it[state] = "added"
}
RecommendationCompletion.ALREADY_ADDED
}
else -> {
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.externalId eq externalId)
}) {
it[this.songId] = songId
it[state] = "ready"
it[error] = null
it[lastSeen] = Instant.now().toString()
}
RecommendationCompletion.READY
}
}
}
/** Called when the user presses Add. Marks the row and attaches a ready song. */
fun markAdded(moodId: String, externalId: String): RecommendationCompletion = transaction {
val row = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.externalId eq externalId)
}
.singleOrNull()
?: return@transaction RecommendationCompletion.MISSING
@@ -508,7 +552,7 @@ object MoodRepository {
val songId = row[MoodRecommendationsTable.songId]
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
(MoodRecommendationsTable.externalId eq externalId)
}) {
it[state] = "added"
}
@@ -519,8 +563,8 @@ object MoodRepository {
RecommendationCompletion.READY
}
fun markRejected(moodId: String, videoId: String) {
setRecommendationState(moodId, videoId, "rejected")
fun markRejected(moodId: String, externalId: String) {
setRecommendationState(moodId, externalId, "rejected")
}
fun readySongs(moodId: String): List<SongRecord> = transaction {
@@ -534,25 +578,7 @@ object MoodRepository {
if (songIds.isEmpty()) emptyList()
else SongsTable.selectAll()
.where { SongsTable.id inList songIds }
.map {
SongRecord(
id = it[SongsTable.id],
videoId = it[SongsTable.videoId],
title = it[SongsTable.title],
artist = it[SongsTable.artist],
album = it[SongsTable.album],
duration = it[SongsTable.duration],
bitRate = it[SongsTable.bitRate],
size = it[SongsTable.size],
suffix = it[SongsTable.suffix],
contentType = it[SongsTable.contentType],
relativePath = it[SongsTable.relativePath],
fileMtime = it[SongsTable.fileMtime],
thumbnail = it[SongsTable.thumbnail],
createdAt = it[SongsTable.createdAt],
updatedAt = it[SongsTable.updatedAt],
)
}
.map { it.toSongRecord() }
}
// ---------------------------------------------------------- discovery
@@ -562,7 +588,10 @@ object MoodRepository {
MoodsTable.selectAll()
.orderBy(MoodsTable.lastDiscoveryAt to SortOrder.ASC)
.map { moodRecord(it) }
.filter { mood -> mood.recommendationCount > 0 && isStale(mood.lastDiscoveryAt, mood.refreshHours, now) }
.filter { mood ->
mood.recommendationCount > 0 &&
isStale(mood.lastDiscoveryAt, mood.refreshHours, now)
}
.take(limit.coerceAtLeast(1))
}
@@ -607,7 +636,8 @@ object MoodRepository {
}
private fun ResultRow.toRecommendationRecord() = RecommendationRecord(
videoId = this[MoodRecommendationsTable.videoId],
source = this[MoodRecommendationsTable.musicSource],
externalId = this[MoodRecommendationsTable.externalId],
title = this[MoodRecommendationsTable.title],
artist = this[MoodRecommendationsTable.artist],
album = this[MoodRecommendationsTable.album],
@@ -622,6 +652,25 @@ object MoodRepository {
lastSeen = this[MoodRecommendationsTable.lastSeen],
)
private fun ResultRow.toSongRecord() = SongRecord(
id = this[SongsTable.id],
source = this[SongsTable.musicSource],
externalId = this[SongsTable.externalId],
title = this[SongsTable.title],
artist = this[SongsTable.artist],
album = this[SongsTable.album],
duration = this[SongsTable.duration],
bitRate = this[SongsTable.bitRate],
size = this[SongsTable.size],
suffix = this[SongsTable.suffix],
contentType = this[SongsTable.contentType],
relativePath = this[SongsTable.relativePath],
fileMtime = this[SongsTable.fileMtime],
thumbnail = this[SongsTable.thumbnail],
createdAt = this[SongsTable.createdAt],
updatedAt = this[SongsTable.updatedAt],
)
private fun moodRecord(row: ResultRow): MoodRecord {
val id = row[MoodsTable.id]
val songCount = MoodSongsTable.selectAll()
@@ -639,6 +688,7 @@ object MoodRepository {
description = row[MoodsTable.description],
query = row[MoodsTable.query],
genres = decodeGenres(row[MoodsTable.genres]),
source = row[MoodsTable.musicSource],
collectionPercent = row[MoodsTable.collectionPercent],
recommendationCount = row[MoodsTable.recommendationCount],
refreshHours = row[MoodsTable.refreshHours],