feat: download accepted recommendations and track longform progress
CI / chart (push) Successful in 4s
CI / chart-push (push) Successful in 4s
CI / test (push) Successful in 2m5s
CI / image (push) Successful in 3m14s

Accepted recommendations without a library match now enter a download queue.
A worker downloads the MP3 with yt-dlp into "<music>/AudioManager Imports",
runs a scan, matches the new song, links it to the recommendation and adds it
as a seed. Jobs retry three times and expose queued/downloading/done/failed
state (plus the error) on the recommendation DTO, so clients can show progress
and retry. Downloads can be disabled and the target folder chosen.

Long-form gets resume points: GET/PUT /longform/progress, scoped to longform
folders and to the owner, with automatic completion detection. Longform stays
excluded from moods and radio.

The chart mounts the music hostPath writable for imports and passes the new
DOWNLOAD_* settings; version 0.2.3.
This commit is contained in:
TapTap committed 2026-11-15 13:26:22 +00:00
1 parent 51d1b20bf6
commit a5af1ee83f
18 files changed
+996 -9

No files matched your search

@@ -19,6 +19,7 @@ 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.nio.file.Files
import java.time.Instant
import java.util.concurrent.TimeUnit
import kotlinx.coroutines.CoroutineScope
@@ -45,6 +46,8 @@ 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.download.DownloadService
import win.taptap.musicmanager.download.YtDlpDownloader
import win.taptap.musicmanager.radio.RadioService
import win.taptap.musicmanager.scanner.CoverArtService
import win.taptap.musicmanager.scanner.MediaScanner
@@ -71,11 +74,39 @@ fun main() {
library = library,
coverArtService = coverArtService,
)
val downloadService = if (config.downloadsEnabled) {
Files.createDirectories(config.downloadDir)
DownloadService(
users = users,
library = library,
downloader = YtDlpDownloader(config.ytDlpPath),
tempDir = config.downloadDir,
startScan = { scanner.start(applicationScope) },
isScanning = { scanner.status().scanning },
folderId = config.downloadFolderId,
)
} else {
null
}
if (MediaRepository.countSongs() == 0L) {
log.info("Song table is empty, starting initial library scan")
scanner.start(applicationScope)
}
// Accepted recommendations that are not in the library are fetched in
// the background and promoted after a scan.
downloadService?.let { service ->
applicationScope.launch(Dispatchers.IO) {
delay(TimeUnit.MINUTES.toMillis(1))
while (isActive) {
val worked = runCatching { service.processOne() }
.onFailure { log.warn("Download worker error", it) }
.getOrDefault(false)
delay(if (worked) 1_000 else 20_000)
}
}
}
// Moods refresh their YT recommendations in the background; the client
// can also force a pass from the mood screen.
applicationScope.launch(Dispatchers.IO) {
@@ -157,6 +188,7 @@ fun main() {
transcoder = transcoder,
radioService = radioService,
discoveryService = discoveryService,
downloadsEnabled = config.downloadsEnabled,
startScan = { scanner.start(applicationScope) },
scanStatus = { scanner.status() },
)
@@ -228,6 +228,14 @@ data class ScanStatusDto(
val error: String? = null,
)
@Serializable
data class DownloadDto(
val state: String,
val attempts: Int,
val error: String? = null,
val updatedAt: String,
)
@Serializable
data class RecommendationDto(
val videoId: String,
@@ -241,6 +249,26 @@ data class RecommendationDto(
val timesAdded: Int,
val firstSeen: String,
val lastSeen: String,
val download: DownloadDto? = null,
)
@Serializable
data class LongformProgressDto(
val song: SongDto,
val positionSeconds: Int,
val durationSeconds: Int,
val completed: Boolean,
val updatedAt: String,
)
@Serializable
data class LongformProgressResponse(val items: List<LongformProgressDto> = emptyList())
@Serializable
data class SaveLongformProgressRequest(
val positionSeconds: Int,
val durationSeconds: Int? = null,
val completed: Boolean = false,
)
fun seedDto(seed: MoodSeedRecord, song: SongRecord): SeedDto =
@@ -35,6 +35,7 @@ fun Route.apiRoutes(
transcoder: FfmpegTranscoder,
radioService: RadioService,
discoveryService: DiscoveryService,
downloadsEnabled: Boolean = true,
startScan: () -> Boolean,
scanStatus: () -> ScanStatus,
) {
@@ -127,7 +128,8 @@ fun Route.apiRoutes(
}
libraryRoutes(users, library, coverArtService, transcoder)
moodRoutes(users, library, radioService, discoveryService)
longformRoutes(users, library)
moodRoutes(users, library, radioService, discoveryService, downloadsEnabled)
sessionRoutes(users, library, radioService)
}
}
@@ -0,0 +1,70 @@
package win.taptap.musicmanager.api
import io.ktor.http.HttpStatusCode
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.get
import io.ktor.server.routing.put
import win.taptap.musicmanager.api.LongformProgressDto
import win.taptap.musicmanager.api.LongformProgressResponse
import win.taptap.musicmanager.api.SaveLongformProgressRequest
import win.taptap.musicmanager.api.toDto
import win.taptap.musicmanager.config.LibraryConfig
import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.LibraryRepository
import win.taptap.musicmanager.db.LongformRepository
/**
* Resume points for audiobooks and podcasts. The library itself is synced
* through `/library/songs?scope=longform`; this only tracks where the user was.
*/
fun Route.longformRoutes(
users: UsersConfig,
library: LibraryConfig,
) {
get("/longform/progress") {
val user = call.requireApiUser(users)
val folders = user.longformFolderIds(library)
val records = LongformRepository.forUser(user.name)
val songs = LibraryRepository
.songsByIds(records.map { it.songId }, folders, user.name)
.associateBy { it.id }
call.respond(
LongformProgressResponse(
items = records.mapNotNull { record ->
songs[record.songId]?.let { song ->
LongformProgressDto(
song = song.toDto(),
positionSeconds = record.positionSeconds,
durationSeconds = record.durationSeconds,
completed = record.completed,
updatedAt = record.updatedAt,
)
}
},
),
)
}
put("/longform/progress/{songId}") {
val user = call.requireApiUser(users)
val songId = call.parameters["songId"] ?: badRequest("Song id is required")
val song = LibraryRepository.song(songId, user.longformFolderIds(library), user.name)
?: notFound("Longform track")
val request = call.receiveJson<SaveLongformProgressRequest>()
val duration = (request.durationSeconds ?: song.duration).coerceAtLeast(0)
val position = request.positionSeconds.coerceIn(0, maxOf(duration, song.duration))
val completed = request.completed || (duration > 0 && position >= duration - 5)
LongformRepository.save(
userName = user.name,
songId = song.id,
positionSeconds = position,
durationSeconds = duration,
completed = completed,
)
call.respond(HttpStatusCode.NoContent)
}
}
@@ -15,6 +15,7 @@ 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.DownloadDto
import win.taptap.musicmanager.api.RecommendationDecisionDto
import win.taptap.musicmanager.api.RecommendationDto
import win.taptap.musicmanager.api.SearchResponse
@@ -24,6 +25,7 @@ import win.taptap.musicmanager.api.toDto
import win.taptap.musicmanager.config.LibraryConfig
import win.taptap.musicmanager.config.UserAccount
import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.DownloadRepository
import win.taptap.musicmanager.db.LibraryRepository
import win.taptap.musicmanager.db.MoodRecord
import win.taptap.musicmanager.db.MoodRepository
@@ -38,6 +40,7 @@ fun Route.moodRoutes(
library: LibraryConfig,
radioService: RadioService,
discoveryService: DiscoveryService,
downloadsEnabled: Boolean,
) {
get("/moods") {
val user = call.requireApiUser(users)
@@ -184,6 +187,15 @@ fun Route.moodRoutes(
val alreadyAccepted = recommendation.state == "accepted"
MoodRepository.setRecommendationState(mood.id, videoId, "accepted")
if (!alreadyAccepted) MoodRepository.incrementRecommendationAdds(mood.id, videoId)
if (matched == null && downloadsEnabled) {
DownloadRepository.enqueue(
userName = user.name,
moodId = mood.id,
videoId = recommendation.videoId,
title = recommendation.title,
artist = recommendation.artist,
)
}
call.respond(decision(mood.id, videoId, user, library, seedAdded))
}
@@ -234,6 +246,7 @@ private fun recommendations(
val matchedSongs = LibraryRepository
.songsByIds(matchedIds, user.visibleFolderIds(library), user.name)
.associateBy { it.id }
val downloads = DownloadRepository.forMood(user.name, mood.id)
return RecommendationsResponse(
recommendations = records.map { record ->
@@ -249,6 +262,14 @@ private fun recommendations(
timesAdded = record.timesAdded,
firstSeen = record.firstSeen,
lastSeen = record.lastSeen,
download = downloads[record.videoId]?.let { job ->
DownloadDto(
state = job.state,
attempts = job.attempts,
error = job.error,
updatedAt = job.updatedAt,
)
},
)
},
)
@@ -15,6 +15,9 @@ data class AppConfig(
val usersFile: Path,
val ffmpegPath: String,
val ytDlpPath: String = "yt-dlp",
val downloadsEnabled: Boolean = true,
val downloadDir: Path = dataDir.resolve("downloads"),
val downloadFolderId: Int? = null,
) {
val databasePath: Path get() = dataDir.resolve("musicmanager.db")
@@ -28,13 +31,18 @@ data class AppConfig(
?: throw IllegalArgumentException("PORT must be an integer, got: $rawPort")
require(port in 1..65535) { "PORT must be between 1 and 65535, got: $port" }
val dataDir = Path.of(value("DATA_DIR", "./data"))
return AppConfig(
port = port,
dataDir = Path.of(value("DATA_DIR", "./data")),
dataDir = dataDir,
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"),
downloadsEnabled = value("DOWNLOADS_ENABLED", "true").toBooleanStrictOrNull() ?: true,
downloadDir = Path.of(value("DOWNLOAD_DIR", dataDir.resolve("downloads").toString())),
downloadFolderId = env["DOWNLOAD_FOLDER_ID"]?.takeIf { it.isNotBlank() }?.toIntOrNull(),
)
}
}
@@ -0,0 +1,231 @@
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.and
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
data class DownloadJobRecord(
val id: String,
val userName: String,
val moodId: String,
val videoId: String,
val title: String,
val artist: String,
val state: String,
val attempts: Int,
val error: String?,
val songId: String?,
val createdAt: String,
val updatedAt: String,
)
/**
* Jobs created when a user accepts a recommendation that the library does not
* have yet. A background worker downloads, scans and promotes them.
*/
object DownloadRepository {
fun enqueue(
userName: String,
moodId: String,
videoId: String,
title: String,
artist: String,
): Boolean = transaction {
val now = Instant.now().toString()
val existing = DownloadJobsTable.selectAll()
.where {
(DownloadJobsTable.userName eq userName) and
(DownloadJobsTable.moodId eq moodId) and
(DownloadJobsTable.videoId eq videoId)
}
.singleOrNull()
if (existing == null) {
val id = "dl-" + ApiTokenRepository.hash("$userName|$moodId|$videoId").take(24)
DownloadJobsTable.insertIgnore {
it[DownloadJobsTable.id] = id
it[DownloadJobsTable.userName] = userName
it[DownloadJobsTable.moodId] = moodId
it[DownloadJobsTable.videoId] = videoId
it[DownloadJobsTable.title] = title.take(500)
it[DownloadJobsTable.artist] = artist.take(300)
it[state] = "queued"
it[attempts] = 0
it[createdAt] = now
it[updatedAt] = now
}
} else if (existing[DownloadJobsTable.state] == "failed") {
DownloadJobsTable.update({ DownloadJobsTable.id eq existing[DownloadJobsTable.id] }) {
it[state] = "queued"
it[attempts] = 0
it[error] = null
it[updatedAt] = now
}
}
true
}
/** Takes the oldest queued job and marks it as downloading. */
fun claimNext(): DownloadJobRecord? = transaction {
val row = DownloadJobsTable.selectAll()
.where { DownloadJobsTable.state eq "queued" }
.orderBy(DownloadJobsTable.createdAt to SortOrder.ASC)
.limit(1)
.singleOrNull()
?: return@transaction null
val attempts = row[DownloadJobsTable.attempts] + 1
DownloadJobsTable.update({ DownloadJobsTable.id eq row[DownloadJobsTable.id] }) {
it[state] = "downloading"
it[DownloadJobsTable.attempts] = attempts
it[updatedAt] = Instant.now().toString()
}
row.toRecord().copy(state = "downloading", attempts = attempts)
}
fun markDone(id: String, songId: String) {
transaction {
DownloadJobsTable.update({ DownloadJobsTable.id eq id }) {
it[state] = "done"
it[this.songId] = songId
it[error] = null
it[updatedAt] = Instant.now().toString()
}
}
}
fun markFailed(id: String, error: String) {
transaction {
DownloadJobsTable.update({ DownloadJobsTable.id eq id }) {
it[state] = "failed"
it[this.error] = error.take(1000)
it[updatedAt] = Instant.now().toString()
}
}
}
fun retryLater(id: String, error: String) {
transaction {
DownloadJobsTable.update({ DownloadJobsTable.id eq id }) {
it[state] = "queued"
it[this.error] = error.take(1000)
it[updatedAt] = Instant.now().toString()
}
}
}
fun forUser(userName: String): List<DownloadJobRecord> = transaction {
DownloadJobsTable.selectAll()
.where { DownloadJobsTable.userName eq userName }
.orderBy(DownloadJobsTable.createdAt to SortOrder.DESC)
.map { it.toRecord() }
}
fun forMood(userName: String, moodId: String): Map<String, DownloadJobRecord> = transaction {
DownloadJobsTable.selectAll()
.where {
(DownloadJobsTable.userName eq userName) and
(DownloadJobsTable.moodId eq moodId)
}
.associate { it[DownloadJobsTable.videoId] to it.toRecord() }
}
fun forRecommendation(userName: String, moodId: String, videoId: String): DownloadJobRecord? =
transaction {
DownloadJobsTable.selectAll()
.where {
(DownloadJobsTable.userName eq userName) and
(DownloadJobsTable.moodId eq moodId) and
(DownloadJobsTable.videoId eq videoId)
}
.singleOrNull()
?.toRecord()
}
private fun org.jetbrains.exposed.sql.ResultRow.toRecord() = DownloadJobRecord(
id = this[DownloadJobsTable.id],
userName = this[DownloadJobsTable.userName],
moodId = this[DownloadJobsTable.moodId],
videoId = this[DownloadJobsTable.videoId],
title = this[DownloadJobsTable.title],
artist = this[DownloadJobsTable.artist],
state = this[DownloadJobsTable.state],
attempts = this[DownloadJobsTable.attempts],
error = this[DownloadJobsTable.error],
songId = this[DownloadJobsTable.songId],
createdAt = this[DownloadJobsTable.createdAt],
updatedAt = this[DownloadJobsTable.updatedAt],
)
}
data class LongformProgressRecord(
val songId: String,
val positionSeconds: Int,
val durationSeconds: Int,
val completed: Boolean,
val updatedAt: String,
)
/** Resume points for audiobooks and podcasts. Music never uses this table. */
object LongformRepository {
fun save(
userName: String,
songId: String,
positionSeconds: Int,
durationSeconds: Int,
completed: Boolean,
updatedAt: String = Instant.now().toString(),
) {
transaction {
val exists = LongformProgressTable.selectAll()
.where {
(LongformProgressTable.userName eq userName) and
(LongformProgressTable.songId eq songId)
}
.any()
if (exists) {
LongformProgressTable.update({
(LongformProgressTable.userName eq userName) and
(LongformProgressTable.songId eq songId)
}) {
it[this.positionSeconds] = positionSeconds.coerceAtLeast(0)
it[this.durationSeconds] = durationSeconds.coerceAtLeast(0)
it[this.completed] = completed
it[this.updatedAt] = updatedAt
}
} else {
LongformProgressTable.insertIgnore {
it[this.userName] = userName
it[this.songId] = songId
it[this.positionSeconds] = positionSeconds.coerceAtLeast(0)
it[this.durationSeconds] = durationSeconds.coerceAtLeast(0)
it[this.completed] = completed
it[this.updatedAt] = updatedAt
}
}
}
}
fun forUser(userName: String, limit: Int = 100): List<LongformProgressRecord> = transaction {
LongformProgressTable.selectAll()
.where { LongformProgressTable.userName eq userName }
.orderBy(LongformProgressTable.updatedAt to SortOrder.DESC)
.limit(limit.coerceIn(1, 500))
.map {
LongformProgressRecord(
songId = it[LongformProgressTable.songId],
positionSeconds = it[LongformProgressTable.positionSeconds],
durationSeconds = it[LongformProgressTable.durationSeconds],
completed = it[LongformProgressTable.completed],
updatedAt = it[LongformProgressTable.updatedAt],
)
}
}
}
@@ -345,6 +345,28 @@ object MoodRepository {
}
}
/** Links a downloaded recommendation to the scanned song and accepts it. */
fun attachDownloadedSong(moodId: String, videoId: String, songId: String) {
transaction {
val row = MoodRecommendationsTable.selectAll()
.where {
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
}
.singleOrNull()
?: return@transaction
MoodRecommendationsTable.update({
(MoodRecommendationsTable.moodId eq moodId) and
(MoodRecommendationsTable.videoId eq videoId)
}) {
it[matchedSongId] = songId
it[state] = "accepted"
it[timesAdded] = row[MoodRecommendationsTable.timesAdded] + 1
it[lastSeen] = Instant.now().toString()
}
}
}
fun markDiscovered(id: String) {
transaction {
MoodsTable.update({ MoodsTable.id eq id }) {
@@ -234,6 +234,34 @@ object SongAffinityTable : Table("song_affinity") {
override val primaryKey = PrimaryKey(userName, songId)
}
object DownloadJobsTable : Table("download_jobs") {
val id = varchar("id", 32)
val userName = reference("user_name", UsersTable.name, onDelete = ReferenceOption.CASCADE)
val moodId = reference("mood_id", MoodsTable.id, onDelete = ReferenceOption.CASCADE)
val videoId = varchar("video_id", 64)
val title = text("title")
val artist = text("artist")
val state = varchar("state", 16).default("queued")
val attempts = integer("attempts").default(0)
val error = text("error").nullable()
val songId = reference("song_id", SongsTable.id, onDelete = ReferenceOption.SET_NULL).nullable()
val createdAt = varchar("created_at", 40)
val updatedAt = varchar("updated_at", 40)
override val primaryKey = PrimaryKey(id)
}
object LongformProgressTable : Table("longform_progress") {
val userName = reference("user_name", UsersTable.name, onDelete = ReferenceOption.CASCADE)
val songId = reference("song_id", SongsTable.id, onDelete = ReferenceOption.CASCADE)
val positionSeconds = integer("position_seconds").default(0)
val durationSeconds = integer("duration_seconds").default(0)
val completed = bool("completed").default(false)
val updatedAt = varchar("updated_at", 40)
override val primaryKey = PrimaryKey(userName, songId)
}
object LibraryTombstonesTable : Table("library_tombstones") {
val entity = varchar("entity", 20)
val entityId = varchar("entity_id", 32)
@@ -0,0 +1,136 @@
package win.taptap.musicmanager.download
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption
import org.slf4j.LoggerFactory
import win.taptap.musicmanager.config.FolderType
import win.taptap.musicmanager.config.LibraryConfig
import win.taptap.musicmanager.config.UsersConfig
import win.taptap.musicmanager.db.DownloadJobRecord
import win.taptap.musicmanager.db.DownloadRepository
import win.taptap.musicmanager.db.LibraryRepository
import win.taptap.musicmanager.db.MoodRepository
import win.taptap.musicmanager.discovery.artistMatches
import win.taptap.musicmanager.discovery.matchKey
fun interface AudioDownloader {
/** Downloads [videoId] and returns the resulting MP3 file. */
fun download(videoId: String, title: String, artist: String, targetDir: Path): Path
}
/**
* Downloads accepted recommendations that are not in the library: fetch the
* MP3, put it into the music folder, run a scan and promote the new song.
*/
class DownloadService(
private val users: UsersConfig,
private val library: LibraryConfig,
private val downloader: AudioDownloader,
private val tempDir: Path,
private val startScan: () -> Boolean,
private val isScanning: () -> Boolean,
private val folderId: Int? = null,
private val maxAttempts: Int = 3,
private val sleepMillis: (Long) -> Unit = { Thread.sleep(it) },
) {
private val log = LoggerFactory.getLogger(DownloadService::class.java)
/** Processes at most one job. Returns false when the queue is empty. */
fun processOne(): Boolean {
val job = DownloadRepository.claimNext() ?: return false
try {
process(job)
} catch (t: Throwable) {
val message = t.message ?: t::class.simpleName ?: "Download failed"
log.warn("Download {} failed (attempt {}): {}", job.videoId, job.attempts, message)
if (job.attempts >= maxAttempts) {
DownloadRepository.markFailed(job.id, message)
} else {
DownloadRepository.retryLater(job.id, message)
}
}
return true
}
private fun process(job: DownloadJobRecord) {
val user = users.byName(job.userName)
?: throw IllegalStateException("Unknown user ${job.userName}")
val folders = library.folders
.asSequence()
.filter { it.type == FolderType.MUSIC && it.id in user.folders }
.associateBy { it.id }
val folder = folderId?.let { folders[it] }
?: folders.values.firstOrNull()
?: throw IllegalStateException("No writable music folder for ${job.userName}")
val importDir = folder.path.resolve(IMPORT_DIR)
Files.createDirectories(importDir)
val working = tempDir.resolve(job.id)
Files.createDirectories(working)
try {
val downloaded = downloader.download(job.videoId, job.title, job.artist, working)
val extension = downloaded.fileName.toString().substringAfterLast('.', "mp3")
val target = uniqueTarget(importDir, job.artist, job.title, extension)
Files.move(downloaded, target, StandardCopyOption.REPLACE_EXISTING)
log.info("Downloaded {} to {}", job.videoId, target)
awaitScan()
val songId = matchSong(job, folders.keys, user.name)
?: throw IllegalStateException("Downloaded file was not indexed by the scanner")
DownloadRepository.markDone(job.id, songId)
MoodRepository.attachDownloadedSong(job.moodId, job.videoId, songId)
MoodRepository.addSeed(job.moodId, job.userName, songId, "yt")
} finally {
runCatching { working.toFile().deleteRecursively() }
}
}
private fun awaitScan() {
// If another scan is already running, wait for that one instead.
startScan()
val deadline = System.nanoTime() + SCAN_TIMEOUT_MINUTES * 60_000_000_000L
while (isScanning()) {
if (System.nanoTime() > deadline) {
throw IllegalStateException("Library scan did not finish in time")
}
sleepMillis(250)
}
}
private fun matchSong(job: DownloadJobRecord, folders: Set<Int>, userName: String): String? {
val titleKey = matchKey(job.title)
if (titleKey.isEmpty()) return null
return LibraryRepository.searchSongs(job.title, 25, 0, folders, userName)
.firstOrNull {
artistMatches(job.artist, it.displayArtist) && matchKey(it.title) == titleKey
}
?.id
}
private fun uniqueTarget(dir: Path, artist: String, title: String, extension: String): Path {
val base = "${sanitize(artist)} - ${sanitize(title)}"
val suffix = extension.ifBlank { "mp3" }.take(10)
var candidate = dir.resolve("$base.$suffix")
var counter = 2
while (Files.exists(candidate)) {
candidate = dir.resolve("$base ($counter).$suffix")
counter += 1
}
return candidate
}
private fun sanitize(value: String): String =
value.replace(Regex("[\\\\/:*?\"<>|\\p{Cntrl}]"), "_")
.replace(Regex("\\s+"), " ")
.trim()
.take(120)
.ifEmpty { "track" }
private companion object {
const val IMPORT_DIR = "AudioManager Imports"
const val SCAN_TIMEOUT_MINUTES = 10L
}
}
@@ -0,0 +1,67 @@
package win.taptap.musicmanager.download
import java.nio.file.Files
import java.nio.file.Path
import java.util.concurrent.TimeUnit
import kotlin.concurrent.thread
/**
* Downloads a single YouTube video as an MP3 with embedded metadata, so the
* scanner can derive artist and title from the file itself.
*/
class YtDlpDownloader(
private val executable: String,
private val timeoutSeconds: Long = 600,
) : AudioDownloader {
override fun download(videoId: String, title: String, artist: String, targetDir: Path): Path {
Files.createDirectories(targetDir)
val process = ProcessBuilder(
executable,
"-x",
"--audio-format",
"mp3",
"--audio-quality",
"0",
"--embed-metadata",
"--no-playlist",
"--no-cache-dir",
"--no-progress",
"--print",
"after_move:filepath",
"-o",
targetDir.resolve("%(id)s.%(ext)s").toString(),
"https://www.youtube.com/watch?v=$videoId",
)
.redirectErrorStream(true)
.start()
val output = StringBuilder()
val reader = thread(isDaemon = true, name = "yt-dlp-download") {
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 download timed out after ${timeoutSeconds}s")
}
reader.join(2_000)
if (process.exitValue() != 0) {
throw IllegalStateException(
"yt-dlp exited with ${process.exitValue()}: ${output.toString().take(400).trim()}",
)
}
val printed = output.lines()
.map { it.trim() }
.lastOrNull { it.endsWith(".mp3", ignoreCase = true) }
val file = printed?.let { Path.of(it) } ?: targetDir.resolve("$videoId.mp3")
if (!Files.isRegularFile(file)) {
throw IllegalStateException("yt-dlp did not produce an MP3 file")
}
return file
}
}
@@ -0,0 +1,33 @@
-- Accepted YT recommendations that are not in the library are downloaded into
-- the first visible music folder and indexed by a scan.
CREATE TABLE download_jobs (
id TEXT PRIMARY KEY,
user_name TEXT NOT NULL REFERENCES users(name) ON DELETE CASCADE,
mood_id TEXT NOT NULL REFERENCES moods(id) ON DELETE CASCADE,
video_id TEXT NOT NULL,
title TEXT NOT NULL,
artist TEXT NOT NULL,
state TEXT NOT NULL DEFAULT 'queued'
CHECK (state IN ('queued', 'downloading', 'done', 'failed')),
attempts INTEGER NOT NULL DEFAULT 0,
error TEXT,
song_id TEXT REFERENCES songs(id) ON DELETE SET NULL,
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
UNIQUE (user_name, mood_id, video_id)
);
CREATE INDEX idx_download_jobs_state ON download_jobs (state, created_at);
-- Long-form resume points, separate from mood playback.
CREATE TABLE longform_progress (
user_name TEXT NOT NULL REFERENCES users(name) ON DELETE CASCADE,
song_id TEXT NOT NULL REFERENCES songs(id) ON DELETE CASCADE,
position_seconds INTEGER NOT NULL DEFAULT 0,
duration_seconds INTEGER NOT NULL DEFAULT 0,
completed BOOLEAN NOT NULL DEFAULT 0,
updated_at TEXT NOT NULL,
PRIMARY KEY (user_name, song_id)
);
CREATE INDEX idx_longform_progress_updated ON longform_progress (user_name, updated_at);