mirror of
https://github.com/ZHANGTIANYAO1/teamspeak-music-bot.git
synced 2026-11-30 18:02:50 +08:00
fix(netease): count visible cloud tracks in playlist cards
Use playlist track IDs for account-specific counts when cloud tracks are reported. Keep other cards available if an individual detail lookup fails.
This commit is contained in:
3 files changed
+108
-33
No files matched your search
@@ -399,6 +399,8 @@ QQ 音源启用时,`QQ_MUSIC_MANAGED_SESSION` 默认为 `1`。在 **设置 →
|
||||
| **已存队列** | 保存当前队列为清单、加载(替换)/ 追加 / 删除已保存清单(仅在管理员开启「保存/加载播放清单」后出现) |
|
||||
| **设置** | 账户(修改自己密码) / 主题切换 / 机器人管理 / 行为设置(空闲超时、频道无人自动暂停、保存/加载播放清单、单曲直接播放不清空队列) / 多平台账号登录(网易云 / QQ / 酷狗 / B站) / 音质选择 / 命令前缀 / 用户管理(仅管理员,含成员能力与机器人白名单)/ 操作审计(仅管理员) |
|
||||
|
||||
网易云歌单卡片的曲目数包含当前网易云账号可见的云盘歌曲。含云盘曲目的歌单若暂时无法取得详情,卡片会显示网易云列表返回的数量。
|
||||
|
||||
### TeamSpeak 文字命令
|
||||
|
||||
在 TeamSpeak 频道中发送文字消息控制机器人:
|
||||
|
||||
@@ -237,6 +237,65 @@ describe("NeteaseProvider.search pagination", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("NetEase playlist counts (#201)", () => {
|
||||
const summaries = [
|
||||
{ id: 1, trackCount: 2, cloudTrackCount: 233 },
|
||||
{ id: 2, trackCount: 337, cloudTrackCount: 7 },
|
||||
{ id: 3, trackCount: 1360, cloudTrackCount: 3 },
|
||||
{ id: 4, trackCount: 162, trackIds: [] },
|
||||
{ id: 5, trackCount: 2, cloudTrackCount: 2, trackIds: [] },
|
||||
{ id: 6, trackCount: 1, cloudTrackCount: 1, trackIds: [{ id: 10 }, { id: 20 }] },
|
||||
];
|
||||
const visibleCounts = [235, 344, 1360, 162, 0];
|
||||
|
||||
function fixture(cookie: string) {
|
||||
const provider = new NeteaseProvider("http://x").withCookie(cookie);
|
||||
const get = vi.fn(async (path: string, { params }: { params: Record<string, unknown> }) => {
|
||||
expect(params.cookie).toBe(cookie);
|
||||
switch (path) {
|
||||
case "/login/status": return { data: { data: { profile: { userId: 1 } } } };
|
||||
case "/user/playlist": return { data: { playlist: summaries } };
|
||||
case "/personalized": return { data: { result: summaries } };
|
||||
case "/cloudsearch": return { data: { result: { playlists: summaries } } };
|
||||
case "/playlist/detail": {
|
||||
const index = Number(params.id) - 1;
|
||||
const count = cookie === "account=owner" ? visibleCounts[index] : summaries[index].trackCount;
|
||||
return { data: { playlist: { ...summaries[index], trackIds: Array(count).fill({}) } } };
|
||||
}
|
||||
default: throw new Error("Unexpected endpoint: " + path);
|
||||
}
|
||||
});
|
||||
(provider as any).api = { get };
|
||||
return { provider, get };
|
||||
}
|
||||
|
||||
it.each(["library", "recommendations", "search"])("counts visible cloud tracks in %s cards", async entry => {
|
||||
const { provider, get } = fixture("account=owner");
|
||||
let playlists;
|
||||
if (entry === "library") playlists = await provider.getUserPlaylists();
|
||||
else if (entry === "recommendations") playlists = await provider.getRecommendPlaylists();
|
||||
else playlists = (await provider.search("music")).playlists;
|
||||
expect(playlists.map(p => p.songCount)).toEqual([235, 344, 1360, 162, 0, 2]);
|
||||
const lookups = get.mock.calls.filter(([path]) => path === "/playlist/detail");
|
||||
expect(lookups.map(([, { params }]) => params.id)).toEqual(["1", "2", "3", "5"]);
|
||||
});
|
||||
|
||||
it("respects the viewing account in playlist details and library cards", async () => {
|
||||
const owner = fixture("account=owner").provider;
|
||||
const guest = fixture("account=other").provider;
|
||||
expect((await owner.getPlaylistDetail("1"))?.songCount).toBe(235);
|
||||
expect((await guest.getPlaylistDetail("1"))?.songCount).toBe(2);
|
||||
expect((await guest.getUserPlaylists())[0].songCount).toBe(2);
|
||||
});
|
||||
|
||||
it("keeps the card's original count if its detail request fails", async () => {
|
||||
const { provider } = fixture("account=owner");
|
||||
vi.spyOn(provider, "getPlaylistDetail").mockRejectedValueOnce(new Error("Unavailable"));
|
||||
const playlists = await provider.getUserPlaylists();
|
||||
expect(playlists.map(p => p.songCount)).toEqual([2, 344, 1360, 162, 0, 2]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("mapNeteaseArtists (artist search + detail)", () => {
|
||||
it("maps cloudsearch type=100 artist entries", () => {
|
||||
const out = mapNeteaseArtists([
|
||||
|
||||
+47
-33
@@ -229,6 +229,16 @@ function getPlaybackUserAgent(cookie: string): string {
|
||||
}
|
||||
}
|
||||
|
||||
interface NeteasePlaylistSummary {
|
||||
id: string | number;
|
||||
name: string;
|
||||
coverImgUrl?: string;
|
||||
picUrl?: string;
|
||||
trackCount?: number;
|
||||
cloudTrackCount?: number;
|
||||
trackIds?: { id: string | number }[] | null;
|
||||
}
|
||||
|
||||
export class NeteaseProvider implements MusicProvider {
|
||||
readonly platform = "netease" as const;
|
||||
private api: AxiosInstance;
|
||||
@@ -256,13 +266,35 @@ export class NeteaseProvider implements MusicProvider {
|
||||
return this.cookie ? { cookie: this.cookie } : {};
|
||||
}
|
||||
|
||||
private async mapPlaylistCards(rows: NeteasePlaylistSummary[], cookie: Record<string, string>): Promise<Playlist[]> {
|
||||
const cards: Playlist[] = [];
|
||||
for (const row of rows) {
|
||||
const id = String(row.id);
|
||||
let songCount = row.trackCount ?? 0;
|
||||
if (Array.isArray(row.trackIds) && row.trackIds.length > 0) {
|
||||
songCount = row.trackIds.length;
|
||||
} else if ((row.cloudTrackCount ?? 0) > 0) {
|
||||
// Cloud tracks can overlap the reported count or be hidden from this account.
|
||||
try {
|
||||
const detail = await this.getPlaylistDetail(id, cookie);
|
||||
songCount = detail?.songCount ?? songCount;
|
||||
} catch {
|
||||
// Keep the list count if this playlist's detail is unavailable.
|
||||
}
|
||||
}
|
||||
cards.push({ id, name: row.name, coverUrl: row.coverImgUrl ?? row.picUrl ?? "", songCount, platform: "netease" });
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
|
||||
async search(query: string, limit = 20, offset = 0): Promise<SearchResult> {
|
||||
const cookie = this.cookieParams;
|
||||
// /cloudsearch supports offset for every type. Songs, playlists (type 1000)
|
||||
// and albums (type 10) are all limit/offset-driven so the web can page past
|
||||
// the first page (playlists/albums were previously hardcoded to limit: 10).
|
||||
const [songRes, playlistRes, albumRes, artistRes] = await Promise.all([
|
||||
this.api.get("/cloudsearch", {
|
||||
params: { keywords: query, type: 1, limit, offset, ...this.cookieParams },
|
||||
params: { keywords: query, type: 1, limit, offset, ...cookie },
|
||||
}),
|
||||
this.api.get("/cloudsearch", {
|
||||
params: {
|
||||
@@ -270,28 +302,20 @@ export class NeteaseProvider implements MusicProvider {
|
||||
type: 1000,
|
||||
limit,
|
||||
offset,
|
||||
...this.cookieParams,
|
||||
...cookie,
|
||||
},
|
||||
}),
|
||||
this.api.get("/cloudsearch", {
|
||||
params: { keywords: query, type: 10, limit, offset, ...this.cookieParams },
|
||||
params: { keywords: query, type: 10, limit, offset, ...cookie },
|
||||
}),
|
||||
this.api.get("/cloudsearch", {
|
||||
params: { keywords: query, type: 100, limit, offset, ...this.cookieParams },
|
||||
params: { keywords: query, type: 100, limit, offset, ...cookie },
|
||||
}),
|
||||
]);
|
||||
|
||||
const songs: Song[] = mapNeteaseSongs(songRes.data?.result?.songs);
|
||||
|
||||
const playlists: Playlist[] = (
|
||||
playlistRes.data?.result?.playlists ?? []
|
||||
).map((p: any) => ({
|
||||
id: String(p.id),
|
||||
name: p.name,
|
||||
coverUrl: p.coverImgUrl ?? "",
|
||||
songCount: p.trackCount ?? 0,
|
||||
platform: "netease",
|
||||
}));
|
||||
const playlists = await this.mapPlaylistCards(playlistRes.data?.result?.playlists ?? [], cookie);
|
||||
|
||||
const albums = mapNeteaseAlbums(albumRes.data?.result?.albums);
|
||||
|
||||
@@ -363,16 +387,11 @@ export class NeteaseProvider implements MusicProvider {
|
||||
}
|
||||
|
||||
async getRecommendPlaylists(): Promise<Playlist[]> {
|
||||
const cookie = this.cookieParams;
|
||||
const res = await this.api.get("/personalized", {
|
||||
params: { limit: 10, ...this.cookieParams },
|
||||
params: { limit: 10, ...cookie },
|
||||
});
|
||||
return (res.data?.result ?? []).map((p: any) => ({
|
||||
id: String(p.id),
|
||||
name: p.name,
|
||||
coverUrl: p.picUrl ?? "",
|
||||
songCount: p.trackCount ?? 0,
|
||||
platform: "netease",
|
||||
}));
|
||||
return this.mapPlaylistCards(res.data?.result ?? [], cookie);
|
||||
}
|
||||
|
||||
async getAlbumSongs(albumId: string): Promise<Song[]> {
|
||||
@@ -575,9 +594,9 @@ export class NeteaseProvider implements MusicProvider {
|
||||
return mapNeteaseSongs(res.data?.data?.dailySongs);
|
||||
}
|
||||
|
||||
async getPlaylistDetail(playlistId: string): Promise<PlaylistDetail | null> {
|
||||
async getPlaylistDetail(playlistId: string, cookie = this.cookieParams): Promise<PlaylistDetail | null> {
|
||||
const res = await this.api.get("/playlist/detail", {
|
||||
params: { id: playlistId, ...this.cookieParams },
|
||||
params: { id: playlistId, ...cookie },
|
||||
});
|
||||
const p = res.data?.playlist;
|
||||
if (!p) return null;
|
||||
@@ -586,27 +605,22 @@ export class NeteaseProvider implements MusicProvider {
|
||||
name: p.name ?? "",
|
||||
description: p.description ?? "",
|
||||
coverUrl: p.coverImgUrl ?? "",
|
||||
songCount: p.trackCount ?? 0,
|
||||
songCount: Array.isArray(p.trackIds) ? p.trackIds.length : p.trackCount ?? 0,
|
||||
};
|
||||
}
|
||||
|
||||
async getUserPlaylists(): Promise<Playlist[]> {
|
||||
const cookie = this.cookieParams;
|
||||
// First get user ID from login status
|
||||
const statusRes = await this.api.get("/login/status", {
|
||||
params: { ...this.cookieParams },
|
||||
params: { ...cookie },
|
||||
});
|
||||
const uid = statusRes.data?.data?.profile?.userId;
|
||||
if (!uid) return [];
|
||||
|
||||
const res = await this.api.get("/user/playlist", {
|
||||
params: { uid, ...this.cookieParams },
|
||||
params: { uid, ...cookie },
|
||||
});
|
||||
return (res.data?.playlist ?? []).map((p: any) => ({
|
||||
id: String(p.id),
|
||||
name: p.name,
|
||||
coverUrl: p.coverImgUrl ?? "",
|
||||
songCount: p.trackCount ?? 0,
|
||||
platform: "netease",
|
||||
}));
|
||||
return this.mapPlaylistCards(res.data?.playlist ?? [], cookie);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user