From 0b3bb0c3dd3b5fc49318a7804c04fea99e9d4e36 Mon Sep 17 00:00:00 2001 From: yhxglsa2016 <1960945492@qq.com> Date: Sat, 28 Nov 2026 09:49:35 +0800 Subject: [PATCH] fix(netease): load playlists beyond 1000 tracks --- README.md | 2 +- src/music/netease.test.ts | 45 +++++++++++++++++++++++++++++++++++++++ src/music/netease.ts | 26 +++++++++++++++++++--- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 83f339e..8f664ce 100644 --- a/README.md +++ b/README.md @@ -423,7 +423,7 @@ QQ 音源启用时,`QQ_MUSIC_MANAGED_SESSION` 默认为 `1`。在 **设置 → | `!queue` | 查看播放队列 | | `!remove <位置>` | 从队列中删除指定位置的歌曲(位置从 1 开始,见 `!queue`) | | `!mode ` | 切换播放模式 | -| `!playlist <歌单名或ID>` | 加载歌单(支持名称模糊搜索和 ID;Jellyfin 歌单 GUID 也可直接粘贴) | +| `!playlist <歌单名或ID>` | 加载歌单(支持名称模糊搜索和 ID;Jellyfin 歌单 GUID 也可直接粘贴)。超过 1000 首的网易云歌单会按原顺序分批加载,网页歌单详情和「播放全部」同样支持 | | `!playlist -q <歌单名>` | 从 QQ 音乐搜索并加载歌单 | | `!playlist <歌单链接>` | 直接粘贴网易云 / QQ 音乐 / YouTube 歌单链接加载,平台由链接自动识别,无需加 `-q` 等标志;也可直接粘贴 App 的分享文案或短链(`163cn.tv`、`c6.y.qq.com`) | | `!album <专辑名或ID>` | 加载专辑(支持名称搜索 / 数字 ID / Jellyfin GUID) | diff --git a/src/music/netease.test.ts b/src/music/netease.test.ts index 3c0dbb1..fafa95e 100644 --- a/src/music/netease.test.ts +++ b/src/music/netease.test.ts @@ -237,6 +237,51 @@ describe("NeteaseProvider.search pagination", () => { }); }); +describe("NeteaseProvider.getPlaylistSongs (#200)", () => { + function playlistFixture(size: number) { + const ids = Array.from({ length: size }, (_, i) => i + 1); + const get = vi.fn().mockResolvedValue({ + data: { playlist: { trackIds: ids.map(id => ({ id })) } }, + }); + const post = vi.fn(async (_path: string, body: { ids: string }) => ({ + data: { songs: body.ids.split(",").reverse().map(id => ({ id: Number(id) })) }, + })); + const provider = new NeteaseProvider("http://x"); + provider.setCookie("MUSIC_U=test"); + (provider as any).api = { get, post }; + return { provider, ids, get, post }; + } + + it.each([0, 12, 1000, 1360, 2000])("loads %i tracks in playlist order", async size => { + const { provider, ids, get, post } = playlistFixture(size); + const songs = await provider.getPlaylistSongs("920292129"); + expect(songs.map(song => song.id)).toEqual(ids.map(String)); + expect(get.mock.calls).toEqual([["/playlist/detail", { + params: { id: "920292129", cookie: "MUSIC_U=test" }, + }]]); + expect(post).toHaveBeenCalledTimes(Math.ceil(size / 1000)); + for (let offset = 0; offset < size; offset += 1000) { + expect(post).toHaveBeenNthCalledWith(offset / 1000 + 1, "/song/detail", { + ids: ids.slice(offset, offset + 1000).join(","), cookie: "MUSIC_U=test", + }, { headers: { "x-apicache-bypass": "1" } }); + } + }); + + it("continues past unavailable tracks in an earlier batch", async () => { + const { provider, post } = playlistFixture(1002); + post.mockResolvedValueOnce({ data: { songs: [{ id: 1 }] } }); + const songs = await provider.getPlaylistSongs("920292129"); + expect(songs.map(song => song.id)).toEqual(["1", "1001", "1002"]); + }); + + it("propagates a failed batch instead of returning a partial playlist", async () => { + const { provider, post } = playlistFixture(1360); + post.mockResolvedValueOnce({ data: { songs: [{ id: 1 }] } }) + .mockRejectedValueOnce(new Error("sidecar unavailable")); + await expect(provider.getPlaylistSongs("920292129")).rejects.toThrow("sidecar unavailable"); + }); +}); + describe("mapNeteaseArtists (artist search + detail)", () => { it("maps cloudsearch type=100 artist entries", () => { const out = mapNeteaseArtists([ diff --git a/src/music/netease.ts b/src/music/netease.ts index a8fc2c6..0d4cd80 100644 --- a/src/music/netease.ts +++ b/src/music/netease.ts @@ -356,10 +356,30 @@ export class NeteaseProvider implements MusicProvider { } async getPlaylistSongs(playlistId: string): Promise { - const res = await this.api.get("/playlist/track/all", { - params: { id: playlistId, ...this.cookieParams }, + const cookie = this.cookieParams; + const res = await this.api.get("/playlist/detail", { + params: { id: playlistId, ...cookie }, }); - return mapNeteaseSongs(res.data?.songs); + const trackIds: { id: number | string }[] = res.data?.playlist?.trackIds; + if (!Array.isArray(trackIds)) throw new Error("网易云返回的歌单曲目列表无效"); + // Song detail accepts at most 1000 IDs. Page over the playlist snapshot + // so missing details for unavailable tracks cannot hide a later batch. + const batchSize = 1000; + const songs: Song[] = []; + for (let offset = 0; offset < trackIds.length; offset += batchSize) { + const ids = trackIds.slice(offset, offset + batchSize).map(track => String(track.id)); + // The sidecar's URL-based cache cannot distinguish POST bodies. + const details = await this.api.post("/song/detail", { ids: ids.join(","), ...cookie }, { + headers: { "x-apicache-bypass": "1" }, + }); + if (!Array.isArray(details.data?.songs)) throw new Error("网易云返回的歌曲详情无效"); + const byId = new Map(mapNeteaseSongs(details.data.songs).map(song => [song.id, song])); + for (const id of ids) { + const song = byId.get(id); + if (song) songs.push(song); + } + } + return songs; } async getRecommendPlaylists(): Promise {