Skip to content

Commit

Permalink
1、添加 搜索、获取歌词等 接口
Browse files Browse the repository at this point in the history
  • Loading branch information
MakcRe committed Jun 23, 2023
1 parent be0cc53 commit 74329a6
Show file tree
Hide file tree
Showing 8 changed files with 150 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,8 @@ $ set HOST=127.0.0.1 && npm run dev
5. 新碟上架
6. 专辑音乐列表
7. 专辑详情
8. 获取音乐URL
8. 获取音乐URL
9. 搜索
10. 综合搜索
11. 歌词搜索
12. 获取歌词
35 changes: 35 additions & 0 deletions module/lyric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 歌词获取
import { decodeLyrics, mapToObject } from '../util/util';

export const useModule = (params: UseModuleParams, useAxios: UseAxios) => {
const dataMap = new Map();
dataMap.set('ver', 1);
dataMap.set('client', 'android');
dataMap.set('id', params?.id); // 歌词id
dataMap.set('accesskey', params?.accesskey); // 歌词key
dataMap.set('fmt', params?.fmt || 'krc'); // 歌词类型
dataMap.set('charset', 'utf8');

return new Promise((resolve, reject) => {
useAxios({
baseURL: 'https://lyrics.kugou.com',
url: '/download',
method: 'GET',
params: mapToObject(dataMap),
cookie: params?.cookie || {},
encryptType: 'android',
}).then(res => {
console.log(params?.decode);
if (params?.decode) {
if (res.body?.content) {
res.body['decodeContent'] = params?.fmt == 'lrc' ? Buffer.from(res.body?.content, 'base64').toString() :decodeLyrics(res.body.content);
resolve(res);
return;
}
}
resolve(res);
}).catch(e => reject(e));
});
}

export default useModule;
24 changes: 24 additions & 0 deletions module/search.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 搜索
import { mapToObject } from '../util/util';

export const useModule = (params: UseModuleParams, useAxios: UseAxios) => {
const dataMap = new Map();
dataMap.set('platform', 'AndroidFilter');
dataMap.set('keyword', params?.keyword || '');
dataMap.set('page', params?.page || 1);
dataMap.set('pagesize', params?.pagesize || 30);
dataMap.set('category', 1);

const type = ['special', 'lyric', 'song', 'album', 'author', 'mv'].includes(params.type) ? params.type : 'song';

return useAxios({
url: `/${type === 'song' ? 'v2' : 'v1'}/search/${type}`,
method: 'GET',
params: mapToObject(dataMap),
encryptType: 'android',
headers: { 'x-router': 'complexsearch.kugou.com' },
cookie: params?.cookie || {},
});
}

export default useModule;
22 changes: 22 additions & 0 deletions module/search_complex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 综合搜索
import { mapToObject } from '../util/util';

export const useModule = (params: UseModuleParams, useAxios: UseAxios) => {
const dataMap = new Map();
dataMap.set('platform', 'AndroidFilter');
dataMap.set('keyword', params.keyword);
dataMap.set('page', params?.page || 1);
dataMap.set('pagesize', params?.pagesize || 30);
dataMap.set('cursor', 0);

return useAxios({
baseURL: 'https://complexsearch.kugou.com',
url: '/v6/search/complex',
method: 'GET',
params: mapToObject(dataMap),
encryptType: 'android',
cookie: params?.cookie || {},
});
}

export default useModule;
27 changes: 27 additions & 0 deletions module/search_lyric.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 歌词搜索
import { mapToObject } from '../util/util';

export const useModule = (params: UseModuleParams, useAxios: UseAxios) => {
const dataMap = new Map();
dataMap.set('album_audio_id', params?.album_audio_id || 0);
dataMap.set('appid', 1005);
dataMap.set('clientver', 11309);
dataMap.set('duration', 0);
dataMap.set('hash', params?.hash || '');
dataMap.set('keyword', params?.keyword || '');
dataMap.set('lrctxt', 1);
dataMap.set('man', 'no');

return useAxios({
baseURL: 'https://lyrics.kugou.com',
url: '/v1/search',
method: 'GET',
params: mapToObject(dataMap),
cookie: params?.cookie || {},
encryptType: 'android',
clearDefaultParams: true,
notSign: true
});
}

export default useModule;
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"devDependencies": {
"@types/express": "^4.17.17",
"@types/node": "^18.11.9",
"@types/pako": "^2.0.0",
"@types/qrcode": "^1.5.0",
"nodemon": "^2.0.20",
"prettier": "^2.7.1",
Expand All @@ -21,6 +22,7 @@
"dependencies": {
"axios": "^1.1.3",
"express": "^4.18.2",
"pako": "^2.1.0",
"qrcode": "^1.5.3",
"safe-decode-uri-component": "^1.2.1"
}
Expand Down
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions util/util.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import pako from 'pako';
export const randomString = (len = 16): string => {
const keyString = '1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ';
const _key: string[] = [];
Expand Down Expand Up @@ -38,4 +39,24 @@ export const cookieToJson = (cookie: string) => {
obj[arr[0]] = arr[1]
})
return obj
}

export const decodeLyrics = (val: string | Uint8Array | Buffer) => {
let bytes: Uint8Array | null = null;
if (val instanceof Uint8Array) bytes = val;
if (Buffer.isBuffer(val)) bytes = new Uint8Array(val);
if (typeof val === 'string') bytes = new Uint8Array(Buffer.from(val, 'base64'));
if (bytes === null) return '';
const enKey = [64, 71, 97, 119, 94, 50, 116, 71, 81, 54, 49, 45, 206, 210, 110, 105];
const krcBytes = bytes.slice(4);
const len = krcBytes.byteLength;
for (let index = 0; index < len; index += 1) {
krcBytes[index] = krcBytes[index] ^ enKey[index % enKey.length];
}
try {
const inflate = pako.inflate(krcBytes);
return Buffer.from(inflate).toString('utf8');
} catch {
return '';
}
}

0 comments on commit 74329a6

Please sign in to comment.