From 96ab25a91d2df13caa03b64b91156de9047406c6 Mon Sep 17 00:00:00 2001 From: TapTap Date: Mon, 16 Nov 2026 18:38:58 +0000 Subject: [PATCH] feat: search YouTube Music and poll for the first recommendation batch - label and document the YouTube Music search source - allow up to 60s for a search and request ten results - keep refreshing a new mood until its first recommendations arrive --- README.md | 13 ++++++++----- .../taptap/musicmanager/client/api/ApiClient.kt | 14 ++++++++++++-- .../taptap/musicmanager/client/store/AppStore.kt | 3 ++- .../musicmanager/client/ui/MoodDetailScreen.kt | 7 +++++-- .../taptap/musicmanager/client/ui/SearchScreen.kt | 4 ++-- docs/mood-first-client.md | 6 +++--- 6 files changed, 32 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 936229b..a3d5f4a 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ self-hosted music server. The client and server are designed together. There is no OpenSubsonic compatibility layer: the app speaks the native `/api/v1` API, where playback is -a server-driven radio session and songs are added by searching YouTube. +a server-driven radio session and songs are added by searching YouTube Music. ## Targets @@ -22,8 +22,9 @@ a server-driven radio session and songs are added by searching YouTube. - **Moods** are the only content model. Each user sees only their own moods. - **Songs** belong to the mood's permanent collection. A mood needs at least one song before it can play. -- **Search YouTube** from inside a mood or globally with a mood picker. Tracks - over 10 minutes are hidden. Results that are already downloaded are marked and +- **Search YouTube Music** from inside a mood or globally with a mood picker. + Results are canonical songs with artist, album and duration; tracks over 10 + minutes are hidden. Results that are already downloaded are marked and add instantly; other videos are queued for server-side download. - **Paste a YouTube URL** to add a specific video. - **Recommendations** are downloaded automatically and play in the radio without @@ -69,7 +70,7 @@ a server-driven radio session and songs are added by searching YouTube. - **Moods** — grid with song and pick counts, create, global search. - **Mood Detail** — songs, add music, recommendations, next batch, settings. -- **Search** — mood picker, YouTube search, paste URL, downloaded badges. +- **Search** — mood picker, YouTube Music search, paste URL, downloaded badges. - **Downloads** — offline files and storage usage. - **Settings** — account, server URL, offline storage, sign out. @@ -204,7 +205,7 @@ it. It owns: - **Mood sync** — GET `/moods` on login/refresh; details are fetched when a mood is opened and cached. - **Mood cache** — cached details power offline mood lists and downloads. -- **Search** — debounced remote YouTube search with the selected target mood. +- **Search** — debounced remote YouTube Music search with the selected target mood. - **Radio** — one session per mood, batches of 12, prefetch when the queue is short, no repeats within a session. - **Downloads** — stored under the platform app directory, keyed by the shared @@ -266,4 +267,6 @@ scripts/build-ci-image.sh 3 in-app cover and the Android notification artwork survive without the server. - Search and downloads depend on `yt-dlp` on the server. YouTube bot checks can fail a search (`502`) or leave a recommendation in `failed` with Retry. + Music search resolves every result for its artist/album/duration, so a search + takes roughly 10–15 seconds for ten results. - No iOS target yet. The shared module is ready for one. diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt index 972263c..ffba131 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/api/ApiClient.kt @@ -179,9 +179,19 @@ class ApiClient(private val http: HttpClient) { suspend fun preview(moodId: String, limit: Int = 20): List = get("/moods/$moodId/preview?limit=$limit").songs - suspend fun search(query: String, limit: Int = 20, moodId: String? = null): List { + suspend fun search(query: String, limit: Int = 10, moodId: String? = null): List { val mood = moodId?.let { "&moodId=${it.encodeURLParameter()}" }.orEmpty() - return get("/search?q=${query.encodeURLParameter()}&limit=$limit$mood").results + return parse( + http.get(url("/search?q=${query.encodeURLParameter()}&limit=$limit$mood")) { + auth() + // yt-dlp resolves every YouTube Music hit to get artist, album + // and duration, so a search takes longer than a plain API call. + timeout { + requestTimeoutMillis = 60_000 + socketTimeoutMillis = 60_000 + } + }, + ).results } suspend fun startSession(moodId: String, limit: Int = 12): Session = diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt index c8b490f..f7a2d61 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt @@ -363,7 +363,7 @@ class AppStore( delay(SEARCH_DEBOUNCE_MS) _state.update { it.copy(searching = true) } try { - val results = api.search(query, 20, _state.value.searchTargetMoodId) + val results = api.search(query, SEARCH_LIMIT, _state.value.searchTargetMoodId) _state.update { it.copy(searchResults = results) } } catch (t: CancellationException) { throw t @@ -1357,5 +1357,6 @@ private const val DOWNLOAD_ATTEMPTS = 3 private const val PROGRESS_POLL_TICKS = 12 private const val MAX_HISTORY = 50 private const val SEARCH_DEBOUNCE_MS = 300L +private const val SEARCH_LIMIT = 10 private const val DOWNLOAD_POLL_MS = 4_000L private const val DOWNLOAD_POLLS = 60 diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt index 19cb452..c4ae192 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/MoodDetailScreen.kt @@ -84,9 +84,12 @@ fun MoodDetailScreen(state: AppState, store: AppStore) { detail.recommendations.count { it.download?.state == "queued" || it.download?.state == "downloading" } - LaunchedEffect(mood.id, inFlight) { + // A new mood gets its first batch from a background discovery run, which + // resolves the YouTube Music search. Keep refreshing until it lands. + val waitingForFirstBatch = mood.recommendationCount > 0 && detail.recommendations.isEmpty() + LaunchedEffect(mood.id, inFlight, waitingForFirstBatch) { var polls = 0 - while (inFlight > 0 && polls < 60) { + while ((inFlight > 0 || waitingForFirstBatch) && polls < 60) { kotlinx.coroutines.delay(5_000) store.refreshSelectedMood() polls += 1 diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/SearchScreen.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/SearchScreen.kt index f98fd7f..92457d5 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/SearchScreen.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/ui/SearchScreen.kt @@ -99,7 +99,7 @@ fun SearchScreen(state: AppState, store: AppStore) { OutlinedTextField( value = state.searchQuery, onValueChange = store::setSearchQuery, - label = { Text("Search YouTube") }, + label = { Text("Search YouTube Music") }, singleLine = true, leadingIcon = { Icon(Icons.Rounded.Search, contentDescription = null) }, modifier = Modifier.fillMaxWidth(), @@ -137,7 +137,7 @@ fun SearchScreen(state: AppState, store: AppStore) { Row(verticalAlignment = Alignment.CenterVertically) { CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp) Spacer(Modifier.width(10.dp)) - Text("Searching YouTube…", style = MaterialTheme.typography.bodySmall) + Text("Searching YouTube Music…", style = MaterialTheme.typography.bodySmall) } } } diff --git a/docs/mood-first-client.md b/docs/mood-first-client.md index 53a2b5e..267836f 100644 --- a/docs/mood-first-client.md +++ b/docs/mood-first-client.md @@ -10,7 +10,7 @@ Read that first. This file only records the client-side consequences. ## What the client becomes - Moods are the only content model. No Library, no Longform, no mounted music. -- All music is downloaded on the server from YouTube; the client searches and +- All music is downloaded on the server from YouTube Music; the client searches and adds songs to a mood. - A mood has **songs** (collection) and **recommendations** (auto-downloaded, playable in radio, not permanent until added). @@ -25,7 +25,7 @@ Read that first. This file only records the client-side consequences. - **MoodDetail**: songs, add music, recommendations (Add / Not interested / Retry), next batch, mood settings (query, genres, collectionPercent, recommendationCount 0–30, refreshHours). -- **Search**: remote YouTube search, results over 10 minutes hidden, add to the +- **Search**: remote YouTube Music search, results over 10 minutes hidden, add to the current or a chosen mood, already-downloaded/in-mood badges, paste URL. - **Downloads**: device-side offline files, delete/clear, storage usage. - **Settings**: server/account/devices/logout (no scan UI). @@ -59,7 +59,7 @@ Implemented. Concretely: - `AppCache` (version 2) stores moods and details only; old cache shapes are discarded. Session and playback snapshots are kept. - `AppStore` has `Moods | MoodDetail | Search | Downloads | Settings`, remote - search with a target mood, add/remove song, recommendation actions, offline + YouTube Music search with a target mood, add/remove song, recommendation actions, offline playback from device downloads, and no longform/library paths. - The UI has a dedicated Search screen (mood picker, paste URL, downloaded badges) and a Downloads screen. Mood Detail shows songs, add music,