From 231af119f6619c8a2f5619ecad25ee55c694a75d Mon Sep 17 00:00:00 2001 From: vfsfitvnm Date: Fri, 12 Aug 2022 13:26:01 +0200 Subject: [PATCH] Fix #203 --- .../vimusic/ui/screens/AlbumScreen.kt | 2 +- .../it/vfsfitvnm/youtubemusic/YouTube.kt | 42 ++++--------------- 2 files changed, 9 insertions(+), 35 deletions(-) diff --git a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt index 4fb57c0..9d5dcfc 100644 --- a/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt +++ b/app/src/main/kotlin/it/vfsfitvnm/vimusic/ui/screens/AlbumScreen.kt @@ -415,7 +415,7 @@ private suspend fun fetchAlbum(browseId: String): Result? { shareUrl = youtubeAlbum.url, timestamp = System.currentTimeMillis() ).also(Database::upsert).also { - youtubeAlbum.withAudioSources().items?.forEachIndexed { position, albumItem -> + youtubeAlbum.withAudioSources()?.getOrNull()?.items?.forEachIndexed { position, albumItem -> albumItem.toMediaItem(browseId, youtubeAlbum)?.let { mediaItem -> Database.insert(mediaItem) Database.upsert( diff --git a/youtube-music/src/main/kotlin/it/vfsfitvnm/youtubemusic/YouTube.kt b/youtube-music/src/main/kotlin/it/vfsfitvnm/youtubemusic/YouTube.kt index f7a7cd8..2dff732 100644 --- a/youtube-music/src/main/kotlin/it/vfsfitvnm/youtubemusic/YouTube.kt +++ b/youtube-music/src/main/kotlin/it/vfsfitvnm/youtubemusic/YouTube.kt @@ -13,6 +13,7 @@ import io.ktor.client.request.parameter import io.ktor.client.request.post import io.ktor.client.request.setBody import io.ktor.http.ContentType +import io.ktor.http.Url import io.ktor.http.contentType import io.ktor.serialization.kotlinx.json.json import it.vfsfitvnm.youtubemusic.models.BrowseResponse @@ -878,43 +879,16 @@ object YouTube { ?.continuation ).next() } - }?.recoverIfCancelled()?.getOrNull() + }.recoverIfCancelled()?.getOrNull() } ?: this } - suspend fun withAudioSources(): PlaylistOrAlbum { - @Serializable - data class RelatedStream( - val url: String, - val title: String - ) - - @Serializable - data class Response( - val relatedStreams: List - ) - - return url?.replace("https://music.youtube.com/playlist?list=", "https://pipedapi.kavin.rocks/playlists/")?.let { url -> - val sources = client.get(url).body().relatedStreams - - copy( - items = items?.mapIndexed { index, item -> - if (item.info.endpoint?.type != "MUSIC_VIDEO_TYPE_ATV") { - sources.getOrNull(index)?.let { source -> - item.copy( - info = item.info.copy( - endpoint = item.info.endpoint?.copy( - videoId = source.url.removePrefix("/watch?v=") - ) - ) - ) - } ?: item - } else { - item - } - } - ) - } ?: this + suspend fun withAudioSources(): Result? { + return url?.let { Url(it).parameters["list"] }?.let { playlistId -> + playlistOrAlbum("VL$playlistId")?.map { playlist -> + copy(items = playlist.items) + } + } } }