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

+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[]> {