diff --git a/src/music/netease.test.ts b/src/music/netease.test.ts index ecc2202..fc50ffd 100644 --- a/src/music/netease.test.ts +++ b/src/music/netease.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from "vitest"; -import { parseLyrics, mapNeteaseAlbums } from "./netease.js"; +import { parseLyrics, mapNeteaseAlbums, mapNeteaseSongs } from "./netease.js"; describe("NetEase adapter", () => { it("parses LRC format lyrics", () => { @@ -56,4 +56,25 @@ describe("NetEase adapter", () => { expect(mapNeteaseAlbums(null as any)).toEqual([]); expect(mapNeteaseAlbums(undefined as any)).toEqual([]); }); + + it("mapNeteaseSongs maps fee to vip flag (1/4 = vip, 0/8 = free)", () => { + const raw = [ + { id: 1, name: "VIP", ar: [{ name: "A" }], al: { name: "Al", picUrl: "p" }, dt: 180000, fee: 1 }, + { id: 2, name: "Album-only", ar: [], al: { name: "Al", picUrl: "" }, dt: 0, fee: 4 }, + { id: 3, name: "Free", ar: [], al: {}, dt: 0, fee: 0 }, + { id: 4, name: "Free low-quality", ar: [], al: {}, dt: 0, fee: 8 }, + ]; + const out = mapNeteaseSongs(raw); + expect(out[0].vip).toBe(true); + expect(out[1].vip).toBe(true); + expect(out[2].vip).toBe(false); + expect(out[3].vip).toBe(false); // fee=8 plays in full (low quality), NOT vip + }); + + it("mapNeteaseSongs accepts artists/album/duration aliases (personal_fm shape)", () => { + const out = mapNeteaseSongs([ + { id: 9, name: "FM", artists: [{ name: "B" }], album: { name: "Al2", picUrl: "p2" }, duration: 200000, fee: 0 }, + ]); + expect(out[0]).toMatchObject({ artist: "B", album: "Al2", coverUrl: "p2", vip: false }); + }); }); diff --git a/src/music/netease.ts b/src/music/netease.ts index a9060b4..6701ed6 100644 --- a/src/music/netease.ts +++ b/src/music/netease.ts @@ -68,6 +68,21 @@ export function mapNeteaseAlbums(raw: any[] | null | undefined): Album[] { })); } +export function mapNeteaseSongs(raw: any[] | null | undefined): Song[] { + if (!Array.isArray(raw)) return []; + return raw.map((s: any) => ({ + id: String(s.id), + name: s.name, + artist: (s.ar ?? s.artists ?? []).map((a: any) => a.name).join(" / "), + album: s.al?.name ?? s.album?.name ?? "", + duration: Math.round((s.dt ?? s.duration ?? 0) / 1000), + coverUrl: s.al?.picUrl ?? s.album?.picUrl ?? "", + platform: "netease", + // fee: 0=free, 1=VIP, 4=album-only, 8=free low-quality (plays in full, NOT vip) + vip: s.fee === 1 || s.fee === 4, + })); +} + // NetEase quality levels: standard(128k) higher(192k) exhigh(320k) lossless(flac) hires(hi-res) jyeffect jymaster export const NETEASE_QUALITY_LEVELS = [ { value: "standard", label: "标准 (128kbps)", bitrate: 128 }, @@ -121,17 +136,7 @@ export class NeteaseProvider implements MusicProvider { }), ]); - const songs: Song[] = (songRes.data?.result?.songs ?? []).map( - (s: any) => ({ - id: String(s.id), - name: s.name, - artist: (s.ar ?? []).map((a: any) => a.name).join(" / "), - album: s.al?.name ?? "", - duration: Math.round((s.dt ?? 0) / 1000), - coverUrl: s.al?.picUrl ?? "", - platform: "netease", - }) - ); + const songs: Song[] = mapNeteaseSongs(songRes.data?.result?.songs); const playlists: Playlist[] = ( playlistRes.data?.result?.playlists ?? [] @@ -160,32 +165,14 @@ export class NeteaseProvider implements MusicProvider { const res = await this.api.get("/song/detail", { params: { ids: songId, ...this.cookieParams }, }); - const s = res.data?.songs?.[0]; - if (!s) return null; - return { - id: String(s.id), - name: s.name, - artist: (s.ar ?? []).map((a: any) => a.name).join(" / "), - album: s.al?.name ?? "", - duration: Math.round((s.dt ?? 0) / 1000), - coverUrl: s.al?.picUrl ?? "", - platform: "netease", - }; + return mapNeteaseSongs(res.data?.songs)[0] ?? null; } async getPlaylistSongs(playlistId: string): Promise { const res = await this.api.get("/playlist/track/all", { params: { id: playlistId, ...this.cookieParams }, }); - return (res.data?.songs ?? []).map((s: any) => ({ - id: String(s.id), - name: s.name, - artist: (s.ar ?? []).map((a: any) => a.name).join(" / "), - album: s.al?.name ?? "", - duration: Math.round((s.dt ?? 0) / 1000), - coverUrl: s.al?.picUrl ?? "", - platform: "netease", - })); + return mapNeteaseSongs(res.data?.songs); } async getRecommendPlaylists(): Promise { @@ -205,15 +192,7 @@ export class NeteaseProvider implements MusicProvider { const res = await this.api.get("/album", { params: { id: albumId, ...this.cookieParams }, }); - return (res.data?.songs ?? []).map((s: any) => ({ - id: String(s.id), - name: s.name, - artist: (s.ar ?? []).map((a: any) => a.name).join(" / "), - album: s.al?.name ?? "", - duration: Math.round((s.dt ?? 0) / 1000), - coverUrl: s.al?.picUrl ?? "", - platform: "netease", - })); + return mapNeteaseSongs(res.data?.songs); } async getLyrics(songId: string): Promise { @@ -312,30 +291,14 @@ export class NeteaseProvider implements MusicProvider { const res = await this.api.get("/personal_fm", { params: { ...this.cookieParams }, }); - return (res.data?.data ?? []).map((s: any) => ({ - id: String(s.id), - name: s.name, - artist: (s.artists ?? []).map((a: any) => a.name).join(" / "), - album: s.album?.name ?? "", - duration: Math.round((s.duration ?? 0) / 1000), - coverUrl: s.album?.picUrl ?? "", - platform: "netease", - })); + return mapNeteaseSongs(res.data?.data); } async getDailyRecommendSongs(): Promise { const res = await this.api.get("/recommend/songs", { params: { ...this.cookieParams }, }); - return (res.data?.data?.dailySongs ?? []).map((s: any) => ({ - id: String(s.id), - name: s.name, - artist: (s.ar ?? []).map((a: any) => a.name).join(" / "), - album: s.al?.name ?? "", - duration: Math.round((s.dt ?? 0) / 1000), - coverUrl: s.al?.picUrl ?? "", - platform: "netease", - })); + return mapNeteaseSongs(res.data?.data?.dailySongs); } async getPlaylistDetail(playlistId: string): Promise { diff --git a/src/music/provider.ts b/src/music/provider.ts index 566ffba..28560c3 100644 --- a/src/music/provider.ts +++ b/src/music/provider.ts @@ -6,6 +6,9 @@ export interface Song { duration: number; // seconds coverUrl: string; platform: "netease" | "qq" | "bilibili" | "youtube"; + /** VIP / copyright-restricted: non-VIP users can only play a trial fragment + * (NetEase fee=1 VIP / fee=4 album-only, or QQ pay.payplay/paytrackprice=1). */ + vip?: boolean; } export interface SongWithUrl extends Song { diff --git a/src/music/qq.test.ts b/src/music/qq.test.ts index fb1c6ca..38ba697 100644 --- a/src/music/qq.test.ts +++ b/src/music/qq.test.ts @@ -22,10 +22,24 @@ describe("QQ adapter", () => { duration: 243, coverUrl: "https://y.gtimg.cn/music/photo_new/T002R300x300M000alb001.jpg", platform: "qq", + vip: false, }, ]); }); + it("mapQqSongs maps pay field to vip flag", () => { + const out = mapQqSongs([ + { mid: "v1", name: "VIP playplay", singer: [], album: {}, interval: 100, pay: { payplay: 1, paytrackprice: 0 } }, + { mid: "v2", name: "VIP trackprice", singer: [], album: {}, interval: 100, pay: { payplay: 0, paytrackprice: 1 } }, + { mid: "f1", name: "Free", singer: [], album: {}, interval: 100, pay: { payplay: 0, paytrackprice: 0 } }, + { mid: "f2", name: "No pay field", singer: [], album: {}, interval: 100 }, + ]); + expect(out[0].vip).toBe(true); + expect(out[1].vip).toBe(true); + expect(out[2].vip).toBe(false); + expect(out[3].vip).toBe(false); + }); + it("mapQqAlbums maps albumMID-style raw entries", () => { const raw = [ { diff --git a/src/music/qq.ts b/src/music/qq.ts index 312ca8b..63f3fbc 100644 --- a/src/music/qq.ts +++ b/src/music/qq.ts @@ -53,6 +53,7 @@ export function mapQqSongs(raw: any[] | null | undefined): Song[] { ? `https://y.gtimg.cn/music/photo_new/T002R300x300M000${albumMid}.jpg` : "", platform: "qq" as const, + vip: s.pay?.payplay === 1 || s.pay?.paytrackprice === 1 || false, }; }).filter((s) => s.id); } @@ -520,6 +521,7 @@ export class QQMusicProvider implements MusicProvider { ? `https://y.gtimg.cn/music/photo_new/T002R300x300M000${s.album.mid}.jpg` : "", platform: "qq", + vip: s.pay?.payplay === 1 || s.pay?.paytrackprice === 1 || false, })); } catch { return [];