feat(歌曲元数据): 添加对Tunehub和音乐平台URL的解析支持

新增parseTunehubParams和parsePageUrlParams函数用于解析不同音乐平台的URL参数,并添加buildSongMetaFromTunehub函数构建歌曲元数据。当URL匹配时优先从Tunehub获取歌曲信息,否则回退到原有逻辑。
This commit is contained in:
史悦
2026-01-07 18:15:33 +08:00
parent f31885deb8
commit 3264149137

View File

@@ -1,6 +1,62 @@
const logger = require('consola');
const { getMetaWithUrl } = require('../service/media_fetcher');
const { matchUrlFromStr } = require('../utils/regex');
const { getSongInfo, buildSongUrl } = require('../service/music_platform/tunehub');
function parseTunehubParams(url) {
try {
const parsed = new URL(url);
if (!parsed.hostname.includes('music-dl.sayqz.com')) {
return null;
}
const source = parsed.searchParams.get('source');
const id = parsed.searchParams.get('id');
if (!source || !id) {
return null;
}
return { source, id };
} catch (err) {
return null;
}
}
function parsePageUrlParams(url) {
if (!url) {
return null;
}
if (url.indexOf('music.163.com') >= 0 || url.indexOf('m.music.163.com') >= 0) {
const match = url.match(/id=(\d+)/);
if (match && match[1]) {
return { source: 'netease', id: match[1] };
}
}
if (url.indexOf('y.qq.com') >= 0) {
const match = url.match(/songDetail\/([A-Za-z0-9]+)/);
if (match && match[1]) {
return { source: 'qq', id: match[1] };
}
}
return null;
}
function buildSongMetaFromTunehub(source, info, pageUrl, songId) {
return {
songName: info.name || '',
artist: info.artist || '',
album: info.album || '',
duration: 0,
coverUrl: info.pic || '',
pageUrl,
audios: [
{
url: info.url || buildSongUrl(source, songId),
}
],
fromMusicPlatform: true,
resourceForbidden: false,
source,
};
}
async function getMeta(req, res) {
const query = req.query;
@@ -15,8 +71,18 @@ async function getMeta(req, res) {
return;
}
const songMeta = await getMetaWithUrl(url);
let songMeta = null;
const params = parseTunehubParams(url) || parsePageUrlParams(url);
if (params) {
const tunehubInfo = await getSongInfo(params.source, params.id);
if (tunehubInfo) {
songMeta = buildSongMetaFromTunehub(params.source, tunehubInfo, url, params.id);
}
}
if (!songMeta) {
songMeta = await getMetaWithUrl(url);
songMeta && (songMeta.pageUrl = url);
}
res.send({
status: songMeta ? 0 : 1,