-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use linolabs/qq-music-api for music searching
Signed-off-by: ZTL-UwU <zhangtianli2006@163.com>
- Loading branch information
Showing
8 changed files
with
180 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { getMusicURL } from '~/server/lib/song'; | ||
import type { TSearchResponse } from '~~/types'; | ||
import { searchSchema } from '~~/types'; | ||
|
||
export default eventHandler(async (event) => { | ||
const paramsParse = await getValidatedQuery(event, params => searchSchema.safeParse(params)); | ||
if (!paramsParse.success) | ||
throw createError({ message: 'Invalid params', status: 400 }); | ||
|
||
const searchBaseURL = 'https://c6.y.qq.com/splcloud/fcgi-bin/smartbox_new.fcg'; | ||
|
||
const res = await $fetch<TSearchResponse>(searchBaseURL, { | ||
method: 'GET', | ||
params: { | ||
_: Date.now(), | ||
cv: '4747474', | ||
ct: '24', | ||
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 { | ||
return JSON.parse(responseText); | ||
} catch { | ||
return responseText; | ||
} | ||
}, | ||
}); | ||
const songMidArray = res.data.song.itemlist.map(item => item.mid); | ||
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], | ||
})); | ||
return songList; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { getMusicURL } from '~/server/lib/song'; | ||
import { streamSchema } from '~~/types/stream'; | ||
|
||
export default eventHandler(async (event) => { | ||
const paramsParse = await getValidatedQuery(event, params => streamSchema.safeParse(params)); | ||
if (!paramsParse.success) | ||
throw createError({ message: 'Invalid params', status: 400 }); | ||
return await getMusicURL([paramsParse.data.songMid]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { TStreamResponse } from '~~/types/stream'; | ||
|
||
export 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, { | ||
method: 'POST', | ||
params: { | ||
_: Date.now(), | ||
}, | ||
body: ` | ||
{ | ||
"comm": { | ||
"cv": 4747474, | ||
"ct": 24, | ||
"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 | ||
}, | ||
"req_1": { | ||
"module": "vkey.GetVkeyServer", | ||
"method": "CgiGetVkey", | ||
"param": { | ||
"guid": "${guid}", | ||
"songmid": ${JSON.stringify(midArray)}, | ||
"songtype": [ | ||
0 | ||
], | ||
"uin": "0", | ||
"loginflag": 1, | ||
"platform": "20" | ||
} | ||
} | ||
} | ||
`, | ||
parseResponse(responseText) { | ||
try { | ||
return JSON.parse(responseText); | ||
} catch { | ||
return responseText; | ||
} | ||
}, | ||
}); | ||
const data: Record<string, string> = {}; | ||
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}`; | ||
} | ||
return data; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { z } from 'zod'; | ||
|
||
export const searchSchema = z.object({ | ||
key: z.string(), | ||
}); | ||
|
||
export interface TItem { | ||
docid: string; | ||
id: string; | ||
mid: string; | ||
name: string; | ||
singer: string; | ||
pic?: string; | ||
} | ||
|
||
export interface TSearchDataItem { | ||
count: number; | ||
itemlist: TItem[]; | ||
name: string; | ||
order: number; | ||
type: number; | ||
} | ||
|
||
export interface TSearchResponse { | ||
code: number; | ||
data: { | ||
album: TSearchDataItem; | ||
mv: TSearchDataItem; | ||
singer: TSearchDataItem; | ||
song: TSearchDataItem; | ||
}; | ||
subcode: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { z } from 'zod'; | ||
|
||
export const streamSchema = z.object({ | ||
songMid: z.string(), | ||
}); | ||
export interface TMidUrlInfoItem { | ||
filename: string; | ||
purl: string; | ||
songmid: string; | ||
vkey: string; | ||
} | ||
export interface TStreamResponse { | ||
code: number; | ||
req_1: { | ||
code: number; | ||
data: { | ||
midurlinfo: TMidUrlInfoItem[]; | ||
}; | ||
}; | ||
} |