feat: add Deezer as the default download source
- 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:
1 parent
81a47f4cd2
commit
141698282d
43 files changed
+2072
-605
No files matched your search
@@ -18,9 +18,10 @@ import win.taptap.musicmanager.db.ConfigRepository
|
||||
import win.taptap.musicmanager.db.DatabaseFactory
|
||||
import win.taptap.musicmanager.db.DatabaseHandle
|
||||
import win.taptap.musicmanager.db.DownloadRepository
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.MoodRecord
|
||||
import win.taptap.musicmanager.db.MoodRepository
|
||||
import win.taptap.musicmanager.db.RecommendationUpsert
|
||||
import win.taptap.musicmanager.db.SongRecord
|
||||
import win.taptap.musicmanager.db.SongRepository
|
||||
import win.taptap.musicmanager.db.SongUpsert
|
||||
import win.taptap.musicmanager.media.MediaGcService
|
||||
@@ -28,6 +29,7 @@ import win.taptap.musicmanager.media.MediaStore
|
||||
import win.taptap.musicmanager.scanner.CoverArtService
|
||||
import win.taptap.musicmanager.scanner.Mp3TagExtractor
|
||||
import win.taptap.musicmanager.scanner.SongIndexer
|
||||
import win.taptap.musicmanager.source.MusicSource
|
||||
|
||||
class DownloadServiceTest {
|
||||
|
||||
@@ -39,7 +41,12 @@ class DownloadServiceTest {
|
||||
TestAudio.assumeFfmpeg()
|
||||
withService { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
DownloadRepository.enqueue("vid-manual", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-manual",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -51,20 +58,56 @@ class DownloadServiceTest {
|
||||
assertEquals(listOf(songId), MoodRepository.songIds(mood.id))
|
||||
|
||||
val song = assertNotNull(SongRepository.byId(songId))
|
||||
assertEquals("vid-manual", song.videoId)
|
||||
assertEquals(MusicSource.YOUTUBE, song.source)
|
||||
assertEquals("vid-manual", song.externalId)
|
||||
assertEquals("Song vid-manual", song.title)
|
||||
assertEquals("Artist", song.artist)
|
||||
assertEquals("vid-manual/Song vid-manual.mp3", song.relativePath)
|
||||
assertEquals("youtube-vid-manual/Song vid-manual.mp3", song.relativePath)
|
||||
assertTrue(Files.isRegularFile(assertNotNull(mediaStore.file(song))))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a Deezer job uses the Deezer downloader and stores its tags`() {
|
||||
var called = false
|
||||
val youtube = AudioDownloader { _, _ -> error("YouTube downloader must not run") }
|
||||
val deezer = AudioDownloader { externalId, dir ->
|
||||
called = true
|
||||
assertEquals("67238732", externalId)
|
||||
mp3("deezer-$externalId", dir, title = "Instant Crush", artist = "Daft Punk")
|
||||
}
|
||||
withService(youtube = youtube, deezer = deezer) { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0, source = MusicSource.DEEZER)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.DEEZER,
|
||||
"67238732",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
assertTrue(called)
|
||||
|
||||
val song = assertNotNull(SongRepository.byExternalId(MusicSource.DEEZER, "67238732"))
|
||||
assertEquals("Daft Punk", song.artist)
|
||||
assertEquals("deezer-67238732/Instant Crush.mp3", song.relativePath)
|
||||
assertTrue(Files.isRegularFile(assertNotNull(mediaStore.file(song))))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an already downloaded song attaches without another download`() {
|
||||
withService(downloader = AudioDownloader { _, _ -> error("downloader must not run") }) { service, mediaStore, _ ->
|
||||
withService(
|
||||
youtube = AudioDownloader { _, _ -> error("downloader must not run") },
|
||||
) { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
val song = insertSong(mediaStore, "vid-existing")
|
||||
DownloadRepository.enqueue("vid-existing", mood.id, DownloadRepository.KIND_SONG)
|
||||
val song = insertSong(mediaStore, MusicSource.YOUTUBE, "vid-existing")
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-existing",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -76,11 +119,23 @@ class DownloadServiceTest {
|
||||
fun `the same video is downloaded once for two moods`() {
|
||||
TestAudio.assumeFfmpeg()
|
||||
var downloads = 0
|
||||
withService(downloader = AudioDownloader { videoId, dir -> downloads++; mp3(videoId, dir) }) { service, _, _ ->
|
||||
withService(
|
||||
youtube = AudioDownloader { externalId, dir -> downloads++; mp3(externalId, dir) },
|
||||
) { service, _, _ ->
|
||||
val first = mood(recommendationCount = 0, name = "First")
|
||||
val second = mood(recommendationCount = 0, name = "Second")
|
||||
DownloadRepository.enqueue("vid-shared", first.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue("vid-shared", second.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-shared",
|
||||
first.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-shared",
|
||||
second.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
assertTrue(service.processOne())
|
||||
@@ -97,9 +152,12 @@ class DownloadServiceTest {
|
||||
withService { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
val promoted = MoodRepository.promoteCandidates(mood.id, 1).single()
|
||||
val promoted = MoodRepository
|
||||
.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
.single()
|
||||
DownloadRepository.enqueue(
|
||||
promoted.videoId,
|
||||
promoted.source,
|
||||
promoted.externalId,
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
)
|
||||
@@ -121,8 +179,9 @@ class DownloadServiceTest {
|
||||
withService { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -143,8 +202,9 @@ class DownloadServiceTest {
|
||||
withService { service, mediaStore, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-rec")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -154,18 +214,19 @@ class DownloadServiceTest {
|
||||
assertTrue(service.processOne())
|
||||
|
||||
assertTrue(MoodRepository.songIds(mood.id).isEmpty())
|
||||
assertNull(SongRepository.byVideoId("vid-rec"))
|
||||
assertTrue(Files.notExists(mediaStore.root.resolve("vid-rec")))
|
||||
assertNull(SongRepository.byExternalId(MusicSource.YOUTUBE, "vid-rec"))
|
||||
assertTrue(Files.notExists(mediaStore.root.resolve("youtube-vid-rec")))
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `failures retry three times and leave the recommendation failed`() {
|
||||
withService(downloader = AudioDownloader { _, _ -> error("network down") }) { service, _, _ ->
|
||||
withService(youtube = AudioDownloader { _, _ -> error("network down") }) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
storeRecommendation(mood.id, "vid-broken")
|
||||
MoodRepository.promoteCandidates(mood.id, 1)
|
||||
MoodRepository.promoteCandidates(mood.id, MusicSource.YOUTUBE, 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-broken",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
@@ -176,7 +237,11 @@ class DownloadServiceTest {
|
||||
assertTrue(service.processOne())
|
||||
|
||||
val job = assertNotNull(
|
||||
DownloadRepository.find(mood.id, "vid-broken", DownloadRepository.KIND_RECOMMENDATION),
|
||||
DownloadRepository.find(
|
||||
mood.id,
|
||||
"vid-broken",
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
),
|
||||
)
|
||||
assertEquals("failed", job.state)
|
||||
assertEquals(3, job.attempts)
|
||||
@@ -191,10 +256,15 @@ class DownloadServiceTest {
|
||||
@Test
|
||||
fun `permission errors fail without burning retries`() {
|
||||
withService(
|
||||
downloader = AudioDownloader { _, _ -> throw AccessDeniedException("/data/media") },
|
||||
youtube = AudioDownloader { _, _ -> throw AccessDeniedException("/data/media") },
|
||||
) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 0)
|
||||
DownloadRepository.enqueue("vid-denied", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-denied",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
@@ -207,41 +277,76 @@ class DownloadServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unconfigured Deezer account fails permanently`() {
|
||||
val deezer = DeezerDownloader(arl = null)
|
||||
withService(deezer = deezer) { service, _, _ ->
|
||||
val mood = mood(recommendationCount = 0, source = MusicSource.DEEZER)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.DEEZER,
|
||||
"67238732",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
assertTrue(service.processOne())
|
||||
|
||||
val job = assertNotNull(
|
||||
DownloadRepository.find(mood.id, "67238732", DownloadRepository.KIND_SONG),
|
||||
)
|
||||
assertEquals("failed", job.state)
|
||||
assertEquals(1, job.attempts)
|
||||
assertTrue(job.error?.contains("Deezer is not configured") == true)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `manual adds outrank recommendation downloads`() {
|
||||
withDatabase { _, _ ->
|
||||
val mood = mood(recommendationCount = 1)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-rec",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_RECOMMENDATION,
|
||||
)
|
||||
DownloadRepository.enqueue("vid-manual", mood.id, DownloadRepository.KIND_SONG)
|
||||
DownloadRepository.enqueue(
|
||||
MusicSource.YOUTUBE,
|
||||
"vid-manual",
|
||||
mood.id,
|
||||
DownloadRepository.KIND_SONG,
|
||||
)
|
||||
|
||||
val claimed = assertNotNull(DownloadRepository.claimNext())
|
||||
assertEquals("vid-manual", claimed.videoId)
|
||||
assertEquals("vid-manual", claimed.externalId)
|
||||
assertEquals(DownloadRepository.KIND_SONG, claimed.kind)
|
||||
}
|
||||
}
|
||||
|
||||
private fun mood(recommendationCount: Int, name: String = "Focus"): MoodRecord =
|
||||
MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
private fun mood(
|
||||
recommendationCount: Int,
|
||||
name: String = "Focus",
|
||||
source: String = MusicSource.YOUTUBE,
|
||||
): MoodRecord = MoodRepository.create(
|
||||
owner = "alice",
|
||||
name = name,
|
||||
description = null,
|
||||
query = null,
|
||||
genres = emptyList(),
|
||||
source = source,
|
||||
collectionPercent = 50,
|
||||
recommendationCount = recommendationCount,
|
||||
refreshHours = 24,
|
||||
)
|
||||
|
||||
private fun storeRecommendation(moodId: String, videoId: String) {
|
||||
private fun storeRecommendation(moodId: String, externalId: String) {
|
||||
MoodRepository.storeCandidates(
|
||||
moodId,
|
||||
MusicSource.YOUTUBE,
|
||||
listOf(
|
||||
RecommendationUpsert(
|
||||
videoId = videoId,
|
||||
source = MusicSource.YOUTUBE,
|
||||
externalId = externalId,
|
||||
title = "Recommended",
|
||||
artist = "Artist",
|
||||
album = null,
|
||||
@@ -254,7 +359,8 @@ class DownloadServiceTest {
|
||||
}
|
||||
|
||||
private fun withService(
|
||||
downloader: AudioDownloader = AudioDownloader { videoId, dir -> mp3(videoId, dir) },
|
||||
youtube: AudioDownloader = AudioDownloader { externalId, dir -> mp3(externalId, dir) },
|
||||
deezer: AudioDownloader = AudioDownloader { externalId, dir -> mp3("deezer-$externalId", dir) },
|
||||
block: (DownloadService, MediaStore, MediaGcService) -> Unit,
|
||||
) {
|
||||
withDatabase { handle, store ->
|
||||
@@ -262,7 +368,12 @@ class DownloadServiceTest {
|
||||
val indexer = SongIndexer(store, coverArt)
|
||||
val gc = MediaGcService(store, coverArt)
|
||||
val service = DownloadService(
|
||||
downloader = downloader,
|
||||
downloaders = SourceDownloaders(
|
||||
mapOf(
|
||||
MusicSource.YOUTUBE to youtube,
|
||||
MusicSource.DEEZER to deezer,
|
||||
),
|
||||
),
|
||||
mediaStore = store,
|
||||
indexer = indexer,
|
||||
gc = gc,
|
||||
@@ -271,8 +382,8 @@ class DownloadServiceTest {
|
||||
)
|
||||
try {
|
||||
block(service, store, gc)
|
||||
} catch (t: Throwable) {
|
||||
throw t
|
||||
} finally {
|
||||
handle.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -288,26 +399,36 @@ class DownloadServiceTest {
|
||||
}
|
||||
}
|
||||
|
||||
private fun mp3(videoId: String, dir: Path): Path {
|
||||
val file = dir.resolve("$videoId.mp3")
|
||||
private fun mp3(
|
||||
externalId: String,
|
||||
dir: Path,
|
||||
title: String = "Song $externalId",
|
||||
artist: String = "Artist",
|
||||
): Path {
|
||||
val file = dir.resolve("$externalId.mp3")
|
||||
TestAudio.createMp3(
|
||||
file,
|
||||
metadata = mapOf(
|
||||
"title" to "Song $videoId",
|
||||
"artist" to "Artist",
|
||||
"title" to title,
|
||||
"artist" to artist,
|
||||
"album" to "Album",
|
||||
),
|
||||
)
|
||||
return file
|
||||
}
|
||||
|
||||
private fun insertSong(mediaStore: MediaStore, videoId: String): win.taptap.musicmanager.db.SongRecord {
|
||||
val file = mediaStore.target(videoId, "Existing")
|
||||
private fun insertSong(
|
||||
mediaStore: MediaStore,
|
||||
source: String,
|
||||
externalId: String,
|
||||
): SongRecord {
|
||||
val file = mediaStore.target(source, externalId, "Existing")
|
||||
Files.createDirectories(file.parent)
|
||||
Files.write(file, "fake-mp3".toByteArray())
|
||||
return SongRepository.upsert(
|
||||
SongUpsert(
|
||||
videoId = videoId,
|
||||
source = source,
|
||||
externalId = externalId,
|
||||
title = "Existing",
|
||||
artist = "Artist",
|
||||
album = null,
|
||||
|
||||
Reference in new issue
Block a user