fix(netease): load playlists beyond 1000 tracks

This commit is contained in:
yhxglsa2016 committed 2026-11-28 09:49:35 +08:00
1 parent fc4d472685
commit 0b3bb0c3dd
3 files changed
+69 -4

No files matched your search

+1 -1
View File
@@ -423,7 +423,7 @@ QQ 音源启用时,`QQ_MUSIC_MANAGED_SESSION` 默认为 `1`。在 **设置 →
| `!queue` | 查看播放队列 |
| `!remove <位置>` | 从队列中删除指定位置的歌曲(位置从 1 开始,见 `!queue`) |
| `!mode <seq\|loop\|random\|rloop>` | 切换播放模式 |
| `!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) |
+45
View File
@@ -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([
+23 -3
View File
@@ -356,10 +356,30 @@ export class NeteaseProvider implements MusicProvider {
}
async getPlaylistSongs(playlistId: string): Promise<Song[]> {
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<Playlist[]> {