Skip to content

Commit

Permalink
feat(review): song pic
Browse files Browse the repository at this point in the history
  • Loading branch information
AsyncFox authored and ZTL-UwU committed Oct 7, 2024
1 parent 2386a3e commit 3e10e0e
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 64 deletions.
42 changes: 12 additions & 30 deletions server/api/search/song.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getMusicURL_v2 } from '~/server/lib/song';
import { getMusicURL } from '~/server/lib/song';
import type { TSearchResponse } from '~~/types';
import { searchSchema } from '~~/types';

Expand All @@ -7,26 +7,14 @@ export default eventHandler(async (event) => {
if (!paramsParse.success)
throw createError({ message: 'Invalid params', status: 400 });

const searchBaseURL = 'https://c6.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg';
const searchBaseURL = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp';

const res = await $fetch<TSearchResponse>(searchBaseURL, {
method: 'GET',
params: {
_: Date.now(),
cv: '4747474',
ct: '24',
w: paramsParse.data.key,
n: 5,
format: 'json',
inCharset: 'utf-8',
outCharset: 'utf-8',
notice: '0',
platform: 'yqq.json',
needNewCode: '1',
uin: '0',
g_tk_new_20200303: '5381',
g_tk: '5381',
hostUin: '0',
is_xml: '0',
key: paramsParse.data.key,
},
parseResponse(responseText) {
try {
Expand All @@ -36,20 +24,14 @@ export default eventHandler(async (event) => {
}
},
});
const songMidArray = res.data.song.itemlist.map(item => item.mid);
const urls: Record<string, string> = {};

for (const mid of songMidArray) {
const b = await getMusicURL_v2(mid);
urls[mid] = b;
}

// const urls = await getMusicURL(songMidArray);
const songList = res.data.song.itemlist.map(item => ({
mid: item.mid,
name: item.name,
singer: item.singer,
url: urls[item.mid],
const songMidArray = res.data.song.list.map(item => item.songmid).filter(item => item !== '');
const urls = await getMusicURL(songMidArray);
const songList = res.data.song.list.map(item => ({
mid: item.songmid,
name: item.songname,
singer: item.singer.map(singer => singer.name).join(', ').trim(),
url: urls[item.songmid],
pic: `https://y.qq.com/music/photo_new/T002R300x300M000${item.albummid}.jpg`,
}));
return songList;
});
36 changes: 13 additions & 23 deletions server/lib/song.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { TStreamResponse } from '~~/types/stream';

export async function getMusicURL(midArray: string[]) {
async function _getMusicURL(midArray: string[]) {
const musicBaseURL = 'https://u6.y.qq.com/cgi-bin/musicu.fcg';
const guid = Math.round(2147483647 * Math.random()) * Date.now() % 1e10;
const res = await $fetch<TStreamResponse>(musicBaseURL, {
Expand Down Expand Up @@ -48,31 +48,21 @@ export async function getMusicURL(midArray: string[]) {
},
});
const data: Record<string, string> = {};
const [filename, keyString] = res.req_1.data.testfile2g.split('?');
if (res.code !== 0 || res.req_1.code !== 0)
throw createError({ message: 'Failed to get music URL', status: 500 });
for (const item of res.req_1.data.midurlinfo) {
if (!item.purl)
data[item.songmid] = '';
else
data[item.songmid] = `https://ws.stream.qqmusic.qq.com/${item.purl}`;
}
for (const item of res.req_1.data.midurlinfo)
data[item.songmid] = `https://ws.stream.qqmusic.qq.com/${item.filename}?${keyString}&src=${filename}`;
return data;
}

export async function getMusicURL_v2(mid: string) {
const res = await $fetch<any>(
`https://u.y.qq.com/cgi-bin/musicu.fcg?format=json&data=%7B%22req_0%22%3A%7B%22module%22%3A%22vkey.GetVkeyServer%22%2C%22method%22%3A%22CgiGetVkey%22%2C%22param%22%3A%7B%22guid%22%3A%22358840384%22%2C%22songmid%22%3A%5B%22${mid}%22%5D%2C%22songtype%22%3A%5B0%5D%2C%22uin%22%3A%221443481947%22%2C%22loginflag%22%3A1%2C%22platform%22%3A%2220%22%7D%7D%2C%22comm%22%3A%7B%22uin%22%3A%2218585073516%22%2C%22format%22%3A%22json%22%2C%22ct%22%3A24%2C%22cv%22%3A0%7D%7D`,
{
parseResponse(responseText) {
try {
return JSON.parse(responseText);
} catch {
return responseText;
}
},
},
);

const url_b = res.req_0.data.testfile2g.split('?');
return `https://ws.stream.qqmusic.qq.com/${res.req_0.data.midurlinfo[0].filename}?${url_b[1]}&src=${url_b[0]}`;
export async function getMusicURL(midArray: string[]) {
let result: Record<string, string> = {};
// split into chunks of 5
for (let i = 0; i < midArray.length; i += 5) {
const midArrayChunk = midArray.slice(i, i + 5);
const data = await _getMusicURL(midArrayChunk);
result = { ...result, ...data };
}
return result;
}
18 changes: 7 additions & 11 deletions types/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,20 @@ export interface TItem {
mid: string;
name: string;
singer: string;
pic?: string;
}

export interface TSearchDataItem {
count: number;
itemlist: TItem[];
name: string;
order: number;
type: number;
albummid: string;
singer: { name: string }[];
songmid: string;
songname: string;
}

export interface TSearchResponse {
code: number;
data: {
album: TSearchDataItem;
mv: TSearchDataItem;
singer: TSearchDataItem;
song: TSearchDataItem;
song: {
list: TSearchDataItem[];
};
};
subcode: number;
}
2 changes: 2 additions & 0 deletions types/stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ export interface TStreamResponse {
req_1: {
code: number;
data: {
testfilewifi: string;
testfile2g: string;
midurlinfo: TMidUrlInfoItem[];
};
};
Expand Down

0 comments on commit 3e10e0e

Please sign in to comment.