feat: search YouTube Music and poll for the first recommendation batch
CI / build (push) Successful in 12m2s
CI / play (push) Successful in 3m18s

- 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
This commit is contained in:
TapTap committed 2026-11-16 18:38:58 +00:00
1 parent c896f81306
commit 96ab25a91d
6 files changed
+32 -15

No files matched your search

+8 -5
View File
@@ -5,7 +5,7 @@ self-hosted music server.
The client and server are designed together. There is no OpenSubsonic The client and server are designed together. There is no OpenSubsonic
compatibility layer: the app speaks the native `/api/v1` API, where playback is 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 ## 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. - **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 - **Songs** belong to the mood's permanent collection. A mood needs at least one
song before it can play. song before it can play.
- **Search YouTube** from inside a mood or globally with a mood picker. Tracks - **Search YouTube Music** from inside a mood or globally with a mood picker.
over 10 minutes are hidden. Results that are already downloaded are marked and 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. add instantly; other videos are queued for server-side download.
- **Paste a YouTube URL** to add a specific video. - **Paste a YouTube URL** to add a specific video.
- **Recommendations** are downloaded automatically and play in the radio without - **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. - **Moods** — grid with song and pick counts, create, global search.
- **Mood Detail** — songs, add music, recommendations, next batch, settings. - **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. - **Downloads** — offline files and storage usage.
- **Settings** — account, server URL, offline storage, sign out. - **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 - **Mood sync** — GET `/moods` on login/refresh; details are fetched when a mood
is opened and cached. is opened and cached.
- **Mood cache** — cached details power offline mood lists and downloads. - **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 - **Radio** — one session per mood, batches of 12, prefetch when the queue is
short, no repeats within a session. short, no repeats within a session.
- **Downloads** — stored under the platform app directory, keyed by the shared - **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. 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 - 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. 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. - No iOS target yet. The shared module is ready for one.
@@ -179,9 +179,19 @@ class ApiClient(private val http: HttpClient) {
suspend fun preview(moodId: String, limit: Int = 20): List<Song> = suspend fun preview(moodId: String, limit: Int = 20): List<Song> =
get<TracksResponse>("/moods/$moodId/preview?limit=$limit").songs get<TracksResponse>("/moods/$moodId/preview?limit=$limit").songs
suspend fun search(query: String, limit: Int = 20, moodId: String? = null): List<SearchResult> { suspend fun search(query: String, limit: Int = 10, moodId: String? = null): List<SearchResult> {
val mood = moodId?.let { "&moodId=${it.encodeURLParameter()}" }.orEmpty() val mood = moodId?.let { "&moodId=${it.encodeURLParameter()}" }.orEmpty()
return get<SearchResponse>("/search?q=${query.encodeURLParameter()}&limit=$limit$mood").results return parse<SearchResponse>(
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 = suspend fun startSession(moodId: String, limit: Int = 12): Session =
@@ -363,7 +363,7 @@ class AppStore(
delay(SEARCH_DEBOUNCE_MS) delay(SEARCH_DEBOUNCE_MS)
_state.update { it.copy(searching = true) } _state.update { it.copy(searching = true) }
try { 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) } _state.update { it.copy(searchResults = results) }
} catch (t: CancellationException) { } catch (t: CancellationException) {
throw t throw t
@@ -1357,5 +1357,6 @@ private const val DOWNLOAD_ATTEMPTS = 3
private const val PROGRESS_POLL_TICKS = 12 private const val PROGRESS_POLL_TICKS = 12
private const val MAX_HISTORY = 50 private const val MAX_HISTORY = 50
private const val SEARCH_DEBOUNCE_MS = 300L 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_POLL_MS = 4_000L
private const val DOWNLOAD_POLLS = 60 private const val DOWNLOAD_POLLS = 60
@@ -84,9 +84,12 @@ fun MoodDetailScreen(state: AppState, store: AppStore) {
detail.recommendations.count { detail.recommendations.count {
it.download?.state == "queued" || it.download?.state == "downloading" 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 var polls = 0
while (inFlight > 0 && polls < 60) { while ((inFlight > 0 || waitingForFirstBatch) && polls < 60) {
kotlinx.coroutines.delay(5_000) kotlinx.coroutines.delay(5_000)
store.refreshSelectedMood() store.refreshSelectedMood()
polls += 1 polls += 1
@@ -99,7 +99,7 @@ fun SearchScreen(state: AppState, store: AppStore) {
OutlinedTextField( OutlinedTextField(
value = state.searchQuery, value = state.searchQuery,
onValueChange = store::setSearchQuery, onValueChange = store::setSearchQuery,
label = { Text("Search YouTube") }, label = { Text("Search YouTube Music") },
singleLine = true, singleLine = true,
leadingIcon = { Icon(Icons.Rounded.Search, contentDescription = null) }, leadingIcon = { Icon(Icons.Rounded.Search, contentDescription = null) },
modifier = Modifier.fillMaxWidth(), modifier = Modifier.fillMaxWidth(),
@@ -137,7 +137,7 @@ fun SearchScreen(state: AppState, store: AppStore) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp) CircularProgressIndicator(Modifier.size(18.dp), strokeWidth = 2.dp)
Spacer(Modifier.width(10.dp)) Spacer(Modifier.width(10.dp))
Text("Searching YouTube…", style = MaterialTheme.typography.bodySmall) Text("Searching YouTube Music…", style = MaterialTheme.typography.bodySmall)
} }
} }
} }
+3 -3
View File
@@ -10,7 +10,7 @@ Read that first. This file only records the client-side consequences.
## What the client becomes ## What the client becomes
- Moods are the only content model. No Library, no Longform, no mounted music. - 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. adds songs to a mood.
- A mood has **songs** (collection) and **recommendations** (auto-downloaded, - A mood has **songs** (collection) and **recommendations** (auto-downloaded,
playable in radio, not permanent until added). 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 / - **MoodDetail**: songs, add music, recommendations (Add / Not interested /
Retry), next batch, mood settings (query, genres, collectionPercent, Retry), next batch, mood settings (query, genres, collectionPercent,
recommendationCount 0–30, refreshHours). 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. current or a chosen mood, already-downloaded/in-mood badges, paste URL.
- **Downloads**: device-side offline files, delete/clear, storage usage. - **Downloads**: device-side offline files, delete/clear, storage usage.
- **Settings**: server/account/devices/logout (no scan UI). - **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 - `AppCache` (version 2) stores moods and details only; old cache shapes are
discarded. Session and playback snapshots are kept. discarded. Session and playback snapshots are kept.
- `AppStore` has `Moods | MoodDetail | Search | Downloads | Settings`, remote - `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. playback from device downloads, and no longform/library paths.
- The UI has a dedicated Search screen (mood picker, paste URL, downloaded - The UI has a dedicated Search screen (mood picker, paste URL, downloaded
badges) and a Downloads screen. Mood Detail shows songs, add music, badges) and a Downloads screen. Mood Detail shows songs, add music,