refactor(音乐同步): 优化歌曲匹配逻辑,优先使用提供的songId

当songId存在时,跳过冗余的网易云匹配流程,直接使用songId进行精确匹配
如果精确匹配失败,再回退到基于名称的匹配方式
This commit is contained in:
史悦
2026-01-08 15:24:08 +08:00
parent 288016a95f
commit 916a595a63
2 changed files with 46 additions and 26 deletions

View File

@@ -93,21 +93,15 @@ async function createJob(req, res) {
} }
if (songId) { if (songId) {
const songFromWyCloud = await findTheBestMatchFromWyCloud(req.account.uid, { // 直接传递songId给后续处理避免冗余的网易云匹配
songName: meta.songName, // 如果后续需要可以根据songId进行精确匹配
artist: meta.artist, meta.songFromWyCloud = {
album: meta.album, songId: songId,
musicPlatformSongId: songId, songName: meta.songName || "",
}); artist: meta.artist || "",
if (!songFromWyCloud) { album: meta.album || "",
logger.error(`song not found in wycloud`); };
res.status(412).send({ logger.info(`[sync_jobs] Using provided songId: ${songId}, will handle matching later if needed`);
status: 1,
message: "can not find song in wycloud with your songId",
});
return;
}
meta.songFromWyCloud = songFromWyCloud;
} }
// create job // create job

View File

@@ -154,10 +154,29 @@ module.exports = async function syncSingleSongWithUrl(uid, url, {
await updateJobIfNeed(uid, jobId, songInfo, jobType); await updateJobIfNeed(uid, jobId, songInfo, jobType);
// step 2. find the best match from wycloud // step 2. find the best match from wycloud
if (songFromWyCloud === null) { if (songFromWyCloud && songFromWyCloud.songId) {
// 如果已经提供了songId优先使用它进行精确匹配
logger.info(`[syncSingleSongWithUrl] Using provided songId: ${songFromWyCloud.songId}`);
try {
const wySongInfo = await getWySongInfo(uid, songFromWyCloud.songId);
if (wySongInfo) {
songFromWyCloud = buildSongInfoFromWyCloud(wySongInfo);
logger.info(`[syncSingleSongWithUrl] Found exact match via songId: ${wySongInfo.songName} - ${wySongInfo.artists?.[0] || 'Unknown'}`);
} else {
logger.warn(`[syncSingleSongWithUrl] songId ${songFromWyCloud.songId} not found in WyCloud, will try name-based matching`);
songFromWyCloud = null; // 回退到基于名称的匹配
}
} catch (error) {
logger.error(`[syncSingleSongWithUrl] Error matching songId: ${error.message}`);
songFromWyCloud = null;
}
}
// 如果没有songId或精确匹配失败进行基于名称的匹配
if (!songFromWyCloud) {
let findSongName, findArtist, findAlbum; let findSongName, findArtist, findAlbum;
if (songName !== "" && artist !== "") { if (songName !== "" && artist !== "") {
logger.info(`use the user input song name and artist, ${songName}, ${artist}, ${album}`); logger.info(`[syncSingleSongWithUrl] use the user input song name and artist, ${songName}, ${artist}, ${album}`);
findSongName = songName; findSongName = songName;
findArtist = artist; findArtist = artist;
findAlbum = album; findAlbum = album;
@@ -166,16 +185,23 @@ module.exports = async function syncSingleSongWithUrl(uid, url, {
findArtist = songInfo.artist; findArtist = songInfo.artist;
findAlbum = songInfo.album; findAlbum = songInfo.album;
} }
songFromWyCloud = await findTheBestMatchFromWyCloud(uid, { if (findSongName && findArtist) {
songName: findSongName, songFromWyCloud = await findTheBestMatchFromWyCloud(uid, {
artist: findArtist, songName: findSongName,
album: findAlbum, artist: findArtist,
}); album: findAlbum,
} else { });
logger.info(`use the songFromWyCloud by params`); if (songFromWyCloud) {
logger.info(`[syncSingleSongWithUrl] Found match via name-based search: ${songFromWyCloud.songName}`);
} else {
logger.info(`[syncSingleSongWithUrl] No match found in WyCloud, will upload as new song`);
}
} else {
logger.info(`[syncSingleSongWithUrl] No song info available for matching`);
}
} }
logger.info('songFromWyCloud:', songFromWyCloud); logger.info('[syncSingleSongWithUrl] songFromWyCloud:', songFromWyCloud);
// step 3. download // step 3. download
// should add meta tag if not matched song on wycloud // should add meta tag if not matched song on wycloud