Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -549,9 +549,9 @@ internal object YouTubeMusicParser {
return null
}
return Regex(
pattern = "(\\d+)\\s*(?:首歌|首歌曲?|首|曲|集|songs?|tracks?|videos?|episodes?)",
pattern = "([0-9][0-9,]*)\\s*(?:首歌|首歌曲?|首|曲|集|songs?|tracks?|videos?|episodes?)",
option = RegexOption.IGNORE_CASE
).find(normalized)?.groupValues?.getOrNull(1)?.toIntOrNull()
).find(normalized)?.groupValues?.getOrNull(1)?.replace(",", "")?.toIntOrNull()
}

private fun findLibraryGridRenderer(root: JSONObject): JSONObject? {
Expand Down Expand Up @@ -1498,11 +1498,7 @@ internal suspend fun collectYouTubeMusicPlaylistDetail(
)
val distinctTracks = tracks.distinctBy { it.videoId }
val loadedTrackCount = distinctTracks.size.takeIf { it > 0 }
val resolvedTrackCount = when {
baseDetail.trackCount != null && loadedTrackCount != null -> maxOf(baseDetail.trackCount, loadedTrackCount)
baseDetail.trackCount != null -> baseDetail.trackCount
else -> loadedTrackCount
}
val resolvedTrackCount = baseDetail.trackCount ?: loadedTrackCount
return baseDetail.copy(
trackCount = resolvedTrackCount,
tracks = distinctTracks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,46 @@ class YouTubeMusicParserTest {
assertEquals(37, YouTubeMusicParser.parsePlaylistTrackCount(root))
}

@Test
fun parsePlaylistTrackCount_readsHeaderDeclaredPlaylistTotal() {
val cases = listOf(
"1,033 songs • 7+ hours" to 1033,
"1,033 首歌曲 • 超过 7 小时" to 1033,
"381 views • 43 tracks • 2 hours, 49 minutes" to 43,
"Auto playlist • 2026" to null
)

cases.forEach { (secondSubtitle, expectedCount) ->
val root = playlistHeaderRoot(secondSubtitle)

assertEquals(
"Unexpected count for $secondSubtitle",
expectedCount,
YouTubeMusicParser.parsePlaylistTrackCount(root)
)
}
}

@Test
fun parsePlaylistDetail_usesHeaderDeclaredCommaFormattedTrackCount() {
val root = playlistDetailRoot(
secondSubtitle = "1,033 songs • 7+ hours",
videoId = "video-1",
title = "Song 1"
)

val detail = YouTubeMusicParser.parsePlaylistDetail(
root = root,
browseId = "VLLM",
fallbackTitle = "",
fallbackSubtitle = "",
fallbackCoverUrl = ""
)

assertEquals(1033, detail.trackCount)
assertEquals(1, detail.tracks.size)
}

@Test
fun parsePlaylistTracks_findsShelfRendererBeyondFirstSection() {
val root = JSONObject(
Expand Down Expand Up @@ -476,6 +516,86 @@ class YouTubeMusicParserTest {
)
}

private fun playlistHeaderRoot(secondSubtitle: String): JSONObject {
return JSONObject(
"""
{
"contents": {
"twoColumnBrowseResultsRenderer": {
"tabs": [
{
"tabRenderer": {
"content": {
"sectionListRenderer": {
"contents": [
{
"musicResponsiveHeaderRenderer": {
"title": { "simpleText": "Virtual Playlist" },
"secondSubtitle": { "simpleText": "$secondSubtitle" }
}
}
]
}
}
}
}
]
}
}
}
""".trimIndent()
)
}

private fun playlistDetailRoot(secondSubtitle: String, videoId: String, title: String): JSONObject {
return JSONObject(
"""
{
"contents": {
"twoColumnBrowseResultsRenderer": {
"tabs": [
{
"tabRenderer": {
"content": {
"sectionListRenderer": {
"contents": [
{
"musicResponsiveHeaderRenderer": {
"title": { "simpleText": "Virtual Playlist" },
"secondSubtitle": {
"runs": [
{ "text": "$secondSubtitle" }
]
}
}
}
]
}
}
}
}
],
"secondaryContents": {
"sectionListRenderer": {
"contents": [
{
"musicPlaylistShelfRenderer": {
"playlistId": "LM",
"contents": [
${playlistItemJson(videoId, title)}
]
}
}
]
}
}
}
}
}
""".trimIndent()
)
}

private fun createContinuationPlaylistPageRoot(
videoId: String,
title: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,42 +38,71 @@ class YouTubeMusicPlaylistDetailPaginationTest {
}

@Test
fun getPlaylistDetail_doesNotReportTrackCountBelowLoadedTracks() = runBlocking {
val responses = listOf(
initialPageRoot(
videoId = "video-1",
title = "Song 1",
continuation = "token-a",
headerTrackCount = 1
),
continuationPageRoot(videoId = "video-2", title = "Song 2", continuation = "token-b"),
continuationPageRoot(videoId = "video-3", title = "Song 3", continuation = null)
fun getPlaylistDetail_usesHeaderTrackCountEvenWhenLoadedTrackCountDiffers() = runBlocking {
val highHeaderDetail = collectDetail(
responses = listOf(
initialPageRoot(
videoId = "video-1",
title = "Song 1",
continuation = null,
headerTrackCountText = "1,033 songs"
)
)
)
var nextResponse = 0

val detail = collectYouTubeMusicPlaylistDetail(
assertEquals(1, highHeaderDetail.tracks.size)
assertEquals(1033, highHeaderDetail.trackCount)

val lowHeaderDetail = collectDetail(
responses = listOf(
initialPageRoot(
videoId = "video-1",
title = "Song 1",
continuation = "token-a",
headerTrackCountText = "1 song"
),
continuationPageRoot(videoId = "video-2", title = "Song 2", continuation = "token-b"),
continuationPageRoot(videoId = "video-3", title = "Song 3", continuation = null)
)
)

assertEquals(3, lowHeaderDetail.tracks.size)
assertEquals(1, lowHeaderDetail.trackCount)

val missingHeaderDetail = collectDetail(
responses = listOf(
initialPageRoot(videoId = "video-1", title = "Song 1", continuation = "token-a"),
continuationPageRoot(videoId = "video-2", title = "Song 2", continuation = "token-b"),
continuationPageRoot(videoId = "video-3", title = "Song 3", continuation = null)
)
)

assertEquals(3, missingHeaderDetail.tracks.size)
assertEquals(3, missingHeaderDetail.trackCount)
}

private suspend fun collectDetail(responses: List<JSONObject>): YouTubeMusicPlaylistDetail {
var nextResponse = 0
return collectYouTubeMusicPlaylistDetail(
browseId = "VLTEST",
fallbackTitle = "Fallback",
fallbackSubtitle = "",
fallbackCoverUrl = "",
pageLimit = 10
) { responses[nextResponse++] }

assertEquals(3, detail.tracks.size)
assertEquals(3, detail.trackCount)
}

private fun initialPageRoot(
videoId: String,
title: String,
continuation: String?,
headerTrackCount: Int? = null
headerTrackCountText: String? = null
): JSONObject {
val secondSubtitle = headerTrackCount?.let { count ->
val secondSubtitle = headerTrackCountText?.let { count ->
"""
,
"secondSubtitle": {
"runs": [ { "text": "$count 首歌" } ]
"runs": [ { "text": "$count" } ]
}
""".trimIndent()
}.orEmpty()
Expand Down
1 change: 1 addition & 0 deletions backup.bin

Large diffs are not rendered by default.

Loading