Add the ability to open YouTube playlist links

This commit is contained in:
vfsfitvnm
2022-06-06 20:27:23 +02:00
parent 756eb48176
commit c3b2435623
7 changed files with 235 additions and 146 deletions

View File

@@ -63,7 +63,8 @@ object YouTube {
@Serializable
data class GetQueueBody(
val context: Context,
val videoIds: List<String>
val videoIds: List<String>?,
val playlistId: String?,
)
@Serializable
@@ -407,41 +408,65 @@ object YouTube {
}.bodyCatching()
}
suspend fun getQueue(videoId: String): Outcome<Item.Song?> {
private suspend fun getQueue(body: GetQueueBody): Outcome<List<Item.Song>?> {
return client.postCatching("/youtubei/v1/music/get_queue") {
contentType(ContentType.Application.Json)
setBody(
GetQueueBody(
context = Context.DefaultWeb,
videoIds = listOf(videoId)
)
)
setBody(body)
parameter("key", Key)
parameter("prettyPrint", false)
}
.bodyCatching<GetQueueResponse>()
.map { body ->
body.queueDatas?.firstOrNull()?.content?.playlistPanelVideoRenderer?.let { renderer ->
Item.Song(
info = Info(
name = renderer.title.text,
endpoint = renderer.navigationEndpoint.watchEndpoint
),
authors = renderer.longBylineText?.splitBySeparator()?.getOrNull(0)
?.map { run ->
Info.from(run)
} ?: emptyList(),
album = renderer.longBylineText?.splitBySeparator()?.getOrNull(1)?.get(0)
?.let { run ->
Info.from(run)
},
thumbnail = renderer.thumbnail.thumbnails[0],
durationText = renderer.lengthText.text
)
body.queueDatas?.mapNotNull { queueData ->
queueData.content?.playlistPanelVideoRenderer?.let { renderer ->
Item.Song(
info = Info(
name = renderer
.title
.text,
endpoint = renderer
.navigationEndpoint
.watchEndpoint
),
authors = renderer
.longBylineText
?.splitBySeparator()
?.getOrNull(0)
?.map { Info.from(it) } ?: emptyList(),
album = renderer
.longBylineText
?.splitBySeparator()
?.getOrNull(1)
?.get(0)
?.let { Info.from(it) },
thumbnail = renderer.thumbnail.thumbnails[0],
durationText = renderer.lengthText.text
)
}
}
}
}
suspend fun song(videoId: String): Outcome<Item.Song?> {
return getQueue(
GetQueueBody(
context = Context.DefaultWeb,
videoIds = listOf(videoId),
playlistId = null
)
).map { it?.firstOrNull() }
}
suspend fun queue(playlistId: String): Outcome<List<Item.Song>?> {
return getQueue(
GetQueueBody(
context = Context.DefaultWeb,
videoIds = null,
playlistId = playlistId
)
)
}
suspend fun next(
videoId: String,
playlistId: String?,