This commit is contained in:
vfsfitvnm
2022-07-17 22:14:49 +02:00
parent a491daebd2
commit 59c0a8d31c
3 changed files with 91 additions and 45 deletions

View File

@@ -84,7 +84,9 @@ fun PlaylistScreen(
val onLoad = relaunchableEffect(Unit) {
playlist = withContext(Dispatchers.IO) {
YouTube.playlistOrAlbum(browseId)
YouTube.playlistOrAlbum(browseId)?.map {
it.next()
}
}
}

View File

@@ -41,6 +41,11 @@ object YouTube {
}
}
@Serializable
data class EmptyBody(
val context: Context,
)
@Serializable
data class BrowseBody(
val context: Context,
@@ -706,7 +711,82 @@ object YouTube {
val durationText: String?,
val album: Info<NavigationEndpoint.Endpoint.Browse>?,
val thumbnail: ThumbnailRenderer.MusicThumbnailRenderer.Thumbnail.Thumbnail?,
)
) {
companion object {
fun from(renderer: MusicResponsiveListItemRenderer): Item? {
return Item(
info = renderer
.flexColumns
.getOrNull(0)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.getOrNull(0)
?.let { Info.from(it) } ?: return null,
authors = renderer
.flexColumns
.getOrNull(1)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.map { Info.from<NavigationEndpoint.Endpoint.Browse>(it) }
?.takeIf { it.isNotEmpty() },
durationText = renderer
.fixedColumns
?.getOrNull(0)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.getOrNull(0)
?.text,
album = renderer
.flexColumns
.getOrNull(2)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.firstOrNull()
?.let { Info.from(it) },
thumbnail = renderer
.thumbnail
?.musicThumbnailRenderer
?.thumbnail
?.thumbnails
?.firstOrNull()
)
}
}
}
suspend fun next(): PlaylistOrAlbum {
return continuation?.let {
runCatching {
client.post("/youtubei/v1/browse") {
contentType(ContentType.Application.Json)
setBody(EmptyBody(context = Context.DefaultWeb))
parameter("key", Key)
parameter("prettyPrint", false)
parameter("continuation", continuation)
}.body<ContinuationResponse>().let { continuationResponse ->
copy(
items = items?.plus(continuationResponse
.continuationContents
.musicShelfContinuation
?.contents
?.map(MusicShelfRenderer.Content::musicResponsiveListItemRenderer)
?.mapNotNull(Item.Companion::from) ?: emptyList()),
continuation = continuationResponse
.continuationContents
.musicShelfContinuation
?.continuations
?.firstOrNull()
?.nextRadioContinuationData
?.continuation
).next()
}
}?.recoverIfCancelled()?.getOrNull()
} ?: this
}
suspend fun withAudioSources(): PlaylistOrAlbum {
@Serializable
@@ -788,48 +868,7 @@ object YouTube {
?.musicShelfRenderer
?.contents
?.map(MusicShelfRenderer.Content::musicResponsiveListItemRenderer)
?.mapNotNull { renderer ->
PlaylistOrAlbum.Item(
info = renderer
.flexColumns
.getOrNull(0)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.getOrNull(0)
?.let { Info.from(it) } ?: return@mapNotNull null,
authors = renderer
.flexColumns
.getOrNull(1)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.map { Info.from<NavigationEndpoint.Endpoint.Browse>(it) }
?.takeIf { it.isNotEmpty() },
durationText = renderer
.fixedColumns
?.getOrNull(0)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.getOrNull(0)
?.text,
album = renderer
.flexColumns
.getOrNull(2)
?.musicResponsiveListItemFlexColumnRenderer
?.text
?.runs
?.firstOrNull()
?.let { Info.from(it) },
thumbnail = renderer
.thumbnail
?.musicThumbnailRenderer
?.thumbnail
?.thumbnails
?.firstOrNull()
)
}
?.mapNotNull(PlaylistOrAlbum.Item.Companion::from)
// ?.filter { it.info.endpoint != null }
,
url = body
@@ -844,6 +883,9 @@ object YouTube {
?.tabRenderer
?.content
?.sectionListRenderer
?.contents
?.firstOrNull()
?.musicShelfRenderer
?.continuations
?.firstOrNull()
?.nextRadioContinuationData

View File

@@ -1,6 +1,7 @@
package it.vfsfitvnm.youtubemusic.models
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
@Serializable
data class ContinuationResponse(
@@ -8,7 +9,8 @@ data class ContinuationResponse(
) {
@Serializable
data class ContinuationContents(
val musicShelfContinuation: MusicShelfRenderer
@JsonNames("musicPlaylistShelfContinuation")
val musicShelfContinuation: MusicShelfRenderer?
) {
// @Serializable
// data class MusicShelfContinuation(