qqmusic-api-enhanced 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,29 +1,157 @@
1
1
  import { request, type ApiResponse } from "../util/request.js";
2
+ import { cookieToJson } from "../util/cookie.js";
3
+ import user_detail_fn from "./user_detail.js";
4
+
5
+ export interface NormalizedVipInfo {
6
+ vipStatus: "vip" | "nonVip" | "unknown";
7
+ vipTier?: "svip" | "vip";
8
+ vipLevel?: number;
9
+ vipYear?: boolean;
10
+ vipExpireTime?: number;
11
+ }
12
+
13
+ export function parseVipFromIcons(iconList: any[] = [], icons: any[] = []): Partial<NormalizedVipInfo> | null {
14
+ for (const item of [...iconList, ...icons]) {
15
+ if (!item) continue;
16
+ const url = String(item.srcUrl || item.IconURL || "");
17
+ const helptxt = String(item.Helptxt || item.Title || "");
18
+ const m = url.match(/(?:^|\/)(n)?(s?vip)(\d+)\.(?:png|webp)/i);
19
+ if (m) {
20
+ const isYear = Boolean(m[1]) || helptxt.includes("年费");
21
+ const tier = m[2].toLowerCase() === "svip" ? "svip" : "vip";
22
+ const level = parseInt(m[3], 10);
23
+ return {
24
+ vipStatus: "vip",
25
+ vipTier: tier,
26
+ vipLevel: level > 0 ? Math.min(Math.max(level, 1), 10) : 1,
27
+ vipYear: isYear,
28
+ };
29
+ }
30
+ }
31
+ return null;
32
+ }
33
+
34
+ export function deriveVipInfo(body: any): NormalizedVipInfo {
35
+ if (!body) {
36
+ return { vipStatus: "unknown" };
37
+ }
38
+ const vipData = body.data || body;
39
+ const identity = vipData.identity || {};
40
+ const svip = Number(vipData.svip || 0);
41
+ const isVip = Number(identity.HugeVip || identity.vip || 0);
42
+ const level = Number(identity.level || 0);
43
+ const isYear = Boolean(
44
+ Number(identity.HugeYearFlag || identity.yearflag || identity.is_year || identity.year_vip || 0) > 0 ||
45
+ Number(vipData.ystar || vipData.year || 0) > 0
46
+ );
47
+
48
+ let vipStatus: "vip" | "nonVip" | "unknown" = "nonVip";
49
+ let vipTier: "svip" | "vip" | undefined = undefined;
50
+ let vipLevel: number | undefined = undefined;
51
+ let vipYear: boolean | undefined = undefined;
52
+ let vipExpireTime: number | undefined = undefined;
53
+
54
+ if (svip > 0) {
55
+ vipStatus = "vip";
56
+ vipTier = "svip";
57
+ vipLevel = level > 0 ? Math.min(Math.max(level, 1), 10) : 1;
58
+ vipYear = isYear;
59
+ } else if (isVip > 0) {
60
+ vipStatus = "vip";
61
+ vipTier = "vip";
62
+ vipLevel = level > 0 ? Math.min(Math.max(level, 1), 10) : 1;
63
+ vipYear = isYear;
64
+ }
65
+
66
+ // 若基本 identity 未命中 VIP,尝试从附加的 Info / NewIconInfo / Icons 解析
67
+ if (vipStatus === "nonVip") {
68
+ const info = vipData.Info || vipData.info || {};
69
+ const iconList = info.NewIconInfo?.iconlist || vipData.NewIconInfo?.iconlist || [];
70
+ const icons = info.Icons || vipData.Icons || [];
71
+ const iconVip = parseVipFromIcons(iconList, icons);
72
+ if (iconVip && iconVip.vipStatus === "vip") {
73
+ vipStatus = "vip";
74
+ vipTier = iconVip.vipTier;
75
+ vipLevel = iconVip.vipLevel;
76
+ vipYear = iconVip.vipYear;
77
+ }
78
+ }
79
+
80
+ const expireTs = Number(vipData.userinfo?.expire || 0);
81
+ if (expireTs > 0) {
82
+ vipExpireTime = expireTs * 1000;
83
+ } else if (identity.HugeVipEnd) {
84
+ const parsed = Date.parse(identity.HugeVipEnd);
85
+ if (!isNaN(parsed)) vipExpireTime = parsed;
86
+ } else if (identity.twelveEnd) {
87
+ const parsed = Date.parse(identity.twelveEnd);
88
+ if (!isNaN(parsed)) vipExpireTime = parsed;
89
+ }
90
+
91
+ return {
92
+ vipStatus,
93
+ vipTier,
94
+ vipLevel,
95
+ vipYear,
96
+ vipExpireTime,
97
+ };
98
+ }
2
99
 
3
100
  /**
4
101
  * 获取当前登录用户的 VIP 会员信息及身份明细.
5
102
  *
6
103
  * @param query.cookie 登录 Cookie
104
+ * @param query.uin 用户 QQ/UIN
7
105
  */
8
106
  export default async function user_vip_info(
9
- query: { cookie?: any } = {},
107
+ query: { cookie?: any; uin?: string | number } = {},
10
108
  req = request
11
- ): Promise<ApiResponse> {
12
- const cgiRes = await req(
13
- "VipLogin.VipLoginInter",
14
- "vip_login_base",
15
- {},
16
- {
17
- cookie: query.cookie,
18
- platform: "web",
19
- }
20
- );
109
+ ): Promise<ApiResponse<{ code: number; data: any; vip: NormalizedVipInfo }>> {
110
+ const cookieObj = cookieToJson(query.cookie);
111
+ const uin =
112
+ query.uin?.toString() ||
113
+ cookieObj.uin ||
114
+ cookieObj.qqmusic_uin ||
115
+ cookieObj.musicid ||
116
+ "0";
117
+
118
+ let cgiRes: ApiResponse<any> = { status: 200, body: {} };
119
+ try {
120
+ cgiRes = await req(
121
+ "VipLogin.VipLoginInter",
122
+ "vip_login_base",
123
+ {},
124
+ {
125
+ uin,
126
+ cookie: query.cookie,
127
+ platform: "android",
128
+ }
129
+ );
130
+ } catch {}
131
+
132
+ let vip = deriveVipInfo(cgiRes.body);
133
+
134
+ // 若 vip_login_base 无法推断出有效 VIP,自动通过主页头部接口获取勋章母版特征
135
+ if (vip.vipStatus === "nonVip" && uin && uin !== "0") {
136
+ try {
137
+ const detailRes = await user_detail_fn({ uin, cookie: query.cookie }, req);
138
+ const detailData = detailRes.body?.data || {};
139
+ const fallbackVip = deriveVipInfo(detailData);
140
+ if (fallbackVip.vipStatus === "vip") {
141
+ vip = {
142
+ ...vip,
143
+ ...fallbackVip,
144
+ };
145
+ }
146
+ } catch {}
147
+ }
21
148
 
22
149
  return {
23
150
  status: cgiRes.status,
24
151
  body: {
25
152
  code: cgiRes.body?.code ?? 200,
26
153
  data: cgiRes.body,
154
+ vip,
27
155
  },
28
156
  cookie: cgiRes.cookie,
29
157
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qqmusic-api-enhanced",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "QQ音乐增强版API,支持扫码登录/凭证刷新/直链提取/歌词解密/歌单歌手检索 || QQ Music Enhanced API for Node.js & Bun",
5
5
  "type": "module",
6
6
  "main": "main.js",
@@ -4,6 +4,8 @@ export interface MobileLoginState {
4
4
  message: string;
5
5
  uin?: string;
6
6
  musickey?: string;
7
+ nickname?: string;
8
+ avatarUrl?: string;
7
9
  cookieStr?: string;
8
10
  createdAt: number;
9
11
  client?: mqtt.MqttClient;
@@ -4,7 +4,8 @@ import mqtt from "mqtt";
4
4
  import { requestCgi } from "./request.js";
5
5
  import { cookieToString, parseSetCookie } from "./cookie.js";
6
6
  // Polyfill ws.createWebSocketStream for Bun runtime
7
- if (typeof ws.createWebSocketStream !== "function") {
7
+ const isBun = typeof globalThis.Bun !== "undefined";
8
+ if (isBun || typeof ws.createWebSocketStream !== "function") {
8
9
  ws.createWebSocketStream = function (socket) {
9
10
  const duplex = new Duplex({
10
11
  read() { },
@@ -27,6 +28,12 @@ const mobileSessions = new Map();
27
28
  export function registerMobileSession(qrcodeId) {
28
29
  if (!qrcodeId)
29
30
  return;
31
+ const existing = mobileSessions.get(qrcodeId);
32
+ if (existing && existing.code === 200)
33
+ return;
34
+ if (existing && Date.now() - existing.createdAt < 120_000 && existing.client && !existing.client.disconnected) {
35
+ return;
36
+ }
30
37
  const now = Date.now();
31
38
  for (const [key, s] of mobileSessions.entries()) {
32
39
  if (now - s.createdAt > 300_000) {
@@ -109,26 +116,66 @@ export function registerMobileSession(qrcodeId) {
109
116
  }
110
117
  else if (type === "cookies") {
111
118
  const cookies = payload?.cookies || {};
112
- const uin = cookies.qqmusic_uin?.value;
113
- const key = cookies.qqmusic_key?.value;
119
+ const uin = cookies.qqmusic_uin?.value || cookies.uin?.value || payload?.uin;
120
+ const key = cookies.qqmusic_key?.value || cookies.qm_keyst?.value || payload?.musickey;
114
121
  if (uin && key) {
115
- const loginRes = await requestCgi("music.login.LoginServer", "Login", {
116
- musicid: Number(uin),
117
- qrCodeID: qrcodeId,
118
- token: String(key),
119
- }, { platform: "android" });
120
- const resultCookies = parseSetCookie(loginRes.cookie || []);
122
+ let loginData = null;
123
+ let resultCookies = {};
124
+ try {
125
+ const loginRes = await requestCgi("music.login.LoginServer", "Login", {
126
+ musicid: Number(uin),
127
+ qrCodeID: qrcodeId,
128
+ token: String(key),
129
+ }, {
130
+ platform: "android",
131
+ comm: { tmeLoginType: 6 },
132
+ });
133
+ loginData = loginRes.body;
134
+ resultCookies = parseSetCookie(loginRes.cookie || []);
135
+ }
136
+ catch { }
137
+ const realMusickey = String(loginData?.musickey || key);
138
+ const realUin = String(loginData?.musicid || uin);
139
+ const nick = String(loginData?.nick || "").trim();
140
+ const logo = String(loginData?.logo || "").trim();
141
+ const encryptUin = String(loginData?.encryptUin || cookies.euin?.value || "");
142
+ const refreshToken = String(loginData?.refresh_token || "");
143
+ const accessToken = String(loginData?.access_token || "");
144
+ const loginType = String(loginData?.loginType || 2);
121
145
  const cookieObj = {
122
- uin: String(uin),
123
- qqmusic_uin: String(uin),
124
- qm_keyst: String(key),
125
- qqmusic_key: String(key),
126
- ...resultCookies,
146
+ uin: realUin,
147
+ qqmusic_uin: realUin,
148
+ musicid: realUin,
149
+ qm_keyst: realMusickey,
150
+ qqmusic_key: realMusickey,
151
+ musickey: realMusickey,
152
+ tmeAppID: "qqmusic",
153
+ tmeLoginType: loginType,
127
154
  };
155
+ if (encryptUin) {
156
+ cookieObj.euin = encryptUin;
157
+ cookieObj.encryptUin = encryptUin;
158
+ }
159
+ if (refreshToken) {
160
+ cookieObj.refresh_token = refreshToken;
161
+ }
162
+ if (accessToken) {
163
+ cookieObj.access_token = accessToken;
164
+ }
165
+ for (const [cookieName, cookieItem] of Object.entries(cookies)) {
166
+ if (cookieItem && typeof cookieItem.value === "string" && !cookieObj[cookieName]) {
167
+ cookieObj[cookieName] = cookieItem.value;
168
+ }
169
+ }
170
+ Object.assign(cookieObj, resultCookies);
128
171
  state.code = 200;
129
172
  state.message = "登录成功";
130
- state.uin = String(uin);
131
- state.musickey = String(key);
173
+ state.uin = realUin;
174
+ state.musickey = realMusickey;
175
+ if (nick)
176
+ state.nickname = nick;
177
+ if (logo)
178
+ state.avatarUrl = logo;
132
179
  state.cookieStr = cookieToString(cookieObj);
133
180
  try {
134
181
  client.end(true);
@@ -5,7 +5,8 @@ import { requestCgi } from "./request.js";
5
5
  import { cookieToString, parseSetCookie } from "./cookie.js";
6
6
 
7
7
  // Polyfill ws.createWebSocketStream for Bun runtime
8
- if (typeof (ws as any).createWebSocketStream !== "function") {
8
+ const isBun = typeof (globalThis as any).Bun !== "undefined";
9
+ if (isBun || typeof (ws as any).createWebSocketStream !== "function") {
9
10
  (ws as any).createWebSocketStream = function (socket: any) {
10
11
  const duplex = new Duplex({
11
12
  read() {},
@@ -29,6 +30,8 @@ export interface MobileLoginState {
29
30
  message: string;
30
31
  uin?: string;
31
32
  musickey?: string;
33
+ nickname?: string;
34
+ avatarUrl?: string;
32
35
  cookieStr?: string;
33
36
  createdAt: number;
34
37
  client?: mqtt.MqttClient;
@@ -38,6 +41,13 @@ const mobileSessions = new Map<string, MobileLoginState>();
38
41
 
39
42
  export function registerMobileSession(qrcodeId: string) {
40
43
  if (!qrcodeId) return;
44
+
45
+ const existing = mobileSessions.get(qrcodeId);
46
+ if (existing && existing.code === 200) return;
47
+ if (existing && Date.now() - existing.createdAt < 120_000 && existing.client && !existing.client.disconnected) {
48
+ return;
49
+ }
50
+
41
51
  const now = Date.now();
42
52
  for (const [key, s] of mobileSessions.entries()) {
43
53
  if (now - s.createdAt > 300_000) {
@@ -122,31 +132,74 @@ export function registerMobileSession(qrcodeId: string) {
122
132
  } catch {}
123
133
  } else if (type === "cookies") {
124
134
  const cookies = payload?.cookies || {};
125
- const uin = cookies.qqmusic_uin?.value;
126
- const key = cookies.qqmusic_key?.value;
135
+ const uin = cookies.qqmusic_uin?.value || cookies.uin?.value || payload?.uin;
136
+ const key = cookies.qqmusic_key?.value || cookies.qm_keyst?.value || payload?.musickey;
127
137
  if (uin && key) {
128
- const loginRes = await requestCgi(
129
- "music.login.LoginServer",
130
- "Login",
131
- {
132
- musicid: Number(uin),
133
- qrCodeID: qrcodeId,
134
- token: String(key),
135
- },
136
- { platform: "android" },
137
- );
138
- const resultCookies = parseSetCookie(loginRes.cookie || []);
138
+ let loginData: any = null;
139
+ let resultCookies: Record<string, string> = {};
140
+ try {
141
+ const loginRes = await requestCgi(
142
+ "music.login.LoginServer",
143
+ "Login",
144
+ {
145
+ musicid: Number(uin),
146
+ qrCodeID: qrcodeId,
147
+ token: String(key),
148
+ },
149
+ {
150
+ platform: "android",
151
+ comm: { tmeLoginType: 6 },
152
+ },
153
+ );
154
+ loginData = loginRes.body;
155
+ resultCookies = parseSetCookie(loginRes.cookie || []);
156
+ } catch {}
157
+
158
+ const realMusickey = String(loginData?.musickey || key);
159
+ const realUin = String(loginData?.musicid || uin);
160
+ const nick = String(loginData?.nick || "").trim();
161
+ const logo = String(loginData?.logo || "").trim();
162
+ const encryptUin = String(loginData?.encryptUin || cookies.euin?.value || "");
163
+ const refreshToken = String(loginData?.refresh_token || "");
164
+ const accessToken = String(loginData?.access_token || "");
165
+ const loginType = String(loginData?.loginType || 2);
166
+
139
167
  const cookieObj: Record<string, string> = {
140
- uin: String(uin),
141
- qqmusic_uin: String(uin),
142
- qm_keyst: String(key),
143
- qqmusic_key: String(key),
144
- ...resultCookies,
168
+ uin: realUin,
169
+ qqmusic_uin: realUin,
170
+ musicid: realUin,
171
+ qm_keyst: realMusickey,
172
+ qqmusic_key: realMusickey,
173
+ musickey: realMusickey,
174
+ tmeAppID: "qqmusic",
175
+ tmeLoginType: loginType,
145
176
  };
177
+
178
+ if (encryptUin) {
179
+ cookieObj.euin = encryptUin;
180
+ cookieObj.encryptUin = encryptUin;
181
+ }
182
+ if (refreshToken) {
183
+ cookieObj.refresh_token = refreshToken;
184
+ }
185
+ if (accessToken) {
186
+ cookieObj.access_token = accessToken;
187
+ }
188
+
189
+ for (const [cookieName, cookieItem] of Object.entries(cookies as Record<string, any>)) {
190
+ if (cookieItem && typeof cookieItem.value === "string" && !cookieObj[cookieName]) {
191
+ cookieObj[cookieName] = cookieItem.value;
192
+ }
193
+ }
194
+
195
+ Object.assign(cookieObj, resultCookies);
196
+
146
197
  state.code = 200;
147
198
  state.message = "登录成功";
148
- state.uin = String(uin);
149
- state.musickey = String(key);
199
+ state.uin = realUin;
200
+ state.musickey = realMusickey;
201
+ if (nick) state.nickname = nick;
202
+ if (logo) state.avatarUrl = logo;
150
203
  state.cookieStr = cookieToString(cookieObj);
151
204
  try {
152
205
  client.end(true);
package/util/option.d.ts CHANGED
@@ -10,6 +10,7 @@ export interface RequestOptions {
10
10
  musickey?: string;
11
11
  headers?: Record<string, string>;
12
12
  timeout?: number;
13
+ comm?: Record<string, any>;
13
14
  }
14
15
  export interface NormalizedOptions {
15
16
  platform: PlatformType;
@@ -22,6 +23,7 @@ export interface NormalizedOptions {
22
23
  musickey: string;
23
24
  headers: Record<string, string>;
24
25
  timeout: number;
26
+ comm?: Record<string, any>;
25
27
  }
26
28
  /**
27
29
  * 格式化并补全请求配置选项.
package/util/option.js CHANGED
@@ -47,6 +47,7 @@ export function createOption(query = {}, defaultPlatform = "web") {
47
47
  musickey,
48
48
  headers,
49
49
  timeout: query.timeout || 10000,
50
+ comm: query.comm,
50
51
  };
51
52
  }
52
53
  /**
@@ -54,8 +55,9 @@ export function createOption(query = {}, defaultPlatform = "web") {
54
55
  */
55
56
  export function buildComm(options) {
56
57
  const gTk = options.musickey ? getGtk(options.musickey) : 5381;
58
+ let baseComm;
57
59
  if (options.platform === "android") {
58
- return {
60
+ baseComm = {
59
61
  ct: 11,
60
62
  cv: 20090008,
61
63
  v: 20090008,
@@ -66,27 +68,40 @@ export function buildComm(options) {
66
68
  guid: options.guid,
67
69
  };
68
70
  }
69
- if (options.platform === "desktop") {
70
- return {
71
+ else if (options.platform === "desktop") {
72
+ baseComm = {
71
73
  ct: 19,
72
74
  cv: 2201,
73
75
  guid: options.guid,
74
76
  uin: Number(options.uin) || 0,
75
77
  };
76
78
  }
77
- // Web 平台
78
- return {
79
- ct: 24,
80
- cv: 4747474,
81
- platform: "yqq.json",
82
- chid: "0",
83
- uin: Number(options.uin) || 0,
84
- g_tk: gTk,
85
- g_tk_new_20200303: gTk,
86
- format: "json",
87
- inCharset: "utf-8",
88
- outCharset: "utf-8",
89
- notice: 0,
90
- need_new_code: 1,
91
- };
79
+ else {
80
+ // Web 平台
81
+ baseComm = {
82
+ ct: 24,
83
+ cv: 4747474,
84
+ platform: "yqq.json",
85
+ chid: "0",
86
+ uin: Number(options.uin) || 0,
87
+ g_tk: gTk,
88
+ g_tk_new_20200303: gTk,
89
+ format: "json",
90
+ inCharset: "utf-8",
91
+ outCharset: "utf-8",
92
+ notice: 0,
93
+ need_new_code: 1,
94
+ };
95
+ }
96
+ if (options.comm) {
97
+ for (const [k, v] of Object.entries(options.comm)) {
98
+ if (v === undefined || v === null) {
99
+ delete baseComm[k];
100
+ }
101
+ else {
102
+ baseComm[k] = v;
103
+ }
104
+ }
105
+ }
106
+ return baseComm;
92
107
  }
package/util/option.ts CHANGED
@@ -14,6 +14,7 @@ export interface RequestOptions {
14
14
  musickey?: string;
15
15
  headers?: Record<string, string>;
16
16
  timeout?: number;
17
+ comm?: Record<string, any>;
17
18
  }
18
19
 
19
20
  export interface NormalizedOptions {
@@ -27,6 +28,7 @@ export interface NormalizedOptions {
27
28
  musickey: string;
28
29
  headers: Record<string, string>;
29
30
  timeout: number;
31
+ comm?: Record<string, any>;
30
32
  }
31
33
 
32
34
  /**
@@ -88,6 +90,7 @@ export function createOption(
88
90
  musickey,
89
91
  headers,
90
92
  timeout: query.timeout || 10000,
93
+ comm: query.comm,
91
94
  };
92
95
  }
93
96
 
@@ -97,8 +100,9 @@ export function createOption(
97
100
  export function buildComm(options: NormalizedOptions): Record<string, any> {
98
101
  const gTk = options.musickey ? getGtk(options.musickey) : 5381;
99
102
 
103
+ let baseComm: Record<string, any>;
100
104
  if (options.platform === "android") {
101
- return {
105
+ baseComm = {
102
106
  ct: 11,
103
107
  cv: 20090008,
104
108
  v: 20090008,
@@ -108,30 +112,40 @@ export function buildComm(options: NormalizedOptions): Record<string, any> {
108
112
  tmeAppID: "qqmusic",
109
113
  guid: options.guid,
110
114
  };
111
- }
112
-
113
- if (options.platform === "desktop") {
114
- return {
115
+ } else if (options.platform === "desktop") {
116
+ baseComm = {
115
117
  ct: 19,
116
118
  cv: 2201,
117
119
  guid: options.guid,
118
120
  uin: Number(options.uin) || 0,
119
121
  };
122
+ } else {
123
+ // Web 平台
124
+ baseComm = {
125
+ ct: 24,
126
+ cv: 4747474,
127
+ platform: "yqq.json",
128
+ chid: "0",
129
+ uin: Number(options.uin) || 0,
130
+ g_tk: gTk,
131
+ g_tk_new_20200303: gTk,
132
+ format: "json",
133
+ inCharset: "utf-8",
134
+ outCharset: "utf-8",
135
+ notice: 0,
136
+ need_new_code: 1,
137
+ };
120
138
  }
121
139
 
122
- // Web 平台
123
- return {
124
- ct: 24,
125
- cv: 4747474,
126
- platform: "yqq.json",
127
- chid: "0",
128
- uin: Number(options.uin) || 0,
129
- g_tk: gTk,
130
- g_tk_new_20200303: gTk,
131
- format: "json",
132
- inCharset: "utf-8",
133
- outCharset: "utf-8",
134
- notice: 0,
135
- need_new_code: 1,
136
- };
140
+ if (options.comm) {
141
+ for (const [k, v] of Object.entries(options.comm)) {
142
+ if (v === undefined || v === null) {
143
+ delete baseComm[k];
144
+ } else {
145
+ baseComm[k] = v;
146
+ }
147
+ }
148
+ }
149
+
150
+ return baseComm;
137
151
  }