From d6f31c2024c98e1b7d14d39153d3d527dd3634ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=8F=B2=E6=82=A6?= Date: Wed, 7 Jan 2026 17:54:05 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E9=9F=B3=E4=B9=90=E5=B9=B3=E5=8F=B0?= =?UTF-8?q?=E9=9B=86=E6=88=90):=20=E9=87=8D=E6=9E=84=E7=BD=91=E6=98=93?= =?UTF-8?q?=E4=BA=91=E9=9F=B3=E4=B9=90=E6=8E=A5=E5=8F=A3=E8=B0=83=E7=94=A8?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 将tunehub的音乐平台接口替换为直接调用wycloud接口 - 简化歌曲信息处理逻辑,移除冗余的splitArtists函数 - 在同步歌曲时增加对网易云音乐的直接支持 - 添加歌曲信息构建的辅助函数,提高代码复用性 --- .../find_the_best_match_from_wycloud.js | 40 ++---------- .../sync_music/sync_single_song_with_url.js | 65 ++++++++++++++++--- 2 files changed, 63 insertions(+), 42 deletions(-) diff --git a/backend/src/service/search_songs/find_the_best_match_from_wycloud.js b/backend/src/service/search_songs/find_the_best_match_from_wycloud.js index a12d0ab..fc84723 100644 --- a/backend/src/service/search_songs/find_the_best_match_from_wycloud.js +++ b/backend/src/service/search_songs/find_the_best_match_from_wycloud.js @@ -1,26 +1,12 @@ -const { getSongInfo, searchSongs } = require('../music_platform/tunehub'); +const { searchSong, getSongInfo } = require('../music_platform/wycloud'); const logger = require('consola'); -function splitArtists(artist) { - if (!artist) { - return []; - } - return artist - .split(/[、/]/) - .map(item => item.trim()) - .filter(Boolean); -} - module.exports = async function findTheBestMatchFromWyCloud(uid, {songName, artist, album, musicPlatformSongId} = {}) { if (musicPlatformSongId) { - const songInfo = await getSongInfo('netease', musicPlatformSongId); + const songInfo = await getSongInfo(uid, musicPlatformSongId); + if (songInfo) { - return { - songId: musicPlatformSongId, - songName: songInfo.name || songName, - artists: splitArtists(songInfo.artist), - album: songInfo.album || album, - }; + return songInfo; } if (songName && artist) { return { @@ -37,24 +23,12 @@ module.exports = async function findTheBestMatchFromWyCloud(uid, {songName, arti if (songName === "" || artist === "") { return null; } - const searchData = await searchSongs(`${songName} ${artist}`, { - source: 'netease', - limit: 20, - aggregate: false, - }); - if (searchData === false || !searchData.results) { + const searchLists = await searchSong(uid, songName, artist); + logger.info('searchLists', searchLists); + if (searchLists === false) { logger.warn(`search song failed, no matter, go on`); return null; } - const searchLists = searchData.results.map(item => { - return { - songId: item.id, - songName: item.name, - artists: splitArtists(item.artist), - album: item.album, - }; - }); - logger.info('searchLists', searchLists); let matchSongAndArtist = null; for (const searchItem of searchLists) { diff --git a/backend/src/service/sync_music/sync_single_song_with_url.js b/backend/src/service/sync_music/sync_single_song_with_url.js index 9b3238b..89a6e47 100644 --- a/backend/src/service/sync_music/sync_single_song_with_url.js +++ b/backend/src/service/sync_music/sync_single_song_with_url.js @@ -12,6 +12,7 @@ const utilFs = require('../../utils/fs'); const { downloadFromLocalTmpPath } = require('./download_to_local'); const uploadWithRetryThenMatch = require('./upload_to_wycloud_disk_with_retry_then_match'); const { getSongInfo, buildSongUrl } = require('../music_platform/tunehub'); +const { getSongInfo: getWySongInfo } = require('../music_platform/wycloud'); function parseTunehubParams(url) { try { @@ -63,6 +64,37 @@ function buildSongInfoFromTunehub(source, info) { }; } +function buildSongInfoFromWyCloud(info) { + if (!info) { + return null; + } + return { + songName: info.songName || '', + artist: info.artists && info.artists.length > 0 ? info.artists[0] : '', + album: info.album || '', + coverUrl: info.cover || '', + duration: info.duration || 0, + fromMusicPlatform: true, + resourceForbidden: false, + source: 'netease', + audios: [], + }; +} + +function buildFallbackSongInfo({ songName, artist, album, source, downloadUrl }) { + return { + songName: songName || 'Unknown', + artist: artist || '', + album: album || '', + coverUrl: '', + duration: 0, + fromMusicPlatform: true, + resourceForbidden: false, + source: source || '', + audios: downloadUrl ? [{ url: downloadUrl }] : [], + }; +} + module.exports = async function syncSingleSongWithUrl(uid, url, { songName = "", artist = "", @@ -72,22 +104,37 @@ module.exports = async function syncSingleSongWithUrl(uid, url, { // step 1. fetch song info let songInfo = null; let downloadUrl = url; - let usedTunehub = false; + let useTunehubDownload = false; const tunehubParams = parseTunehubParams(url) || parsePageUrlParams(url); if (tunehubParams) { + useTunehubDownload = true; + downloadUrl = buildSongUrl(tunehubParams.source, tunehubParams.id); const tunehubInfo = await getSongInfo(tunehubParams.source, tunehubParams.id); if (tunehubInfo) { - usedTunehub = true; songInfo = buildSongInfoFromTunehub(tunehubParams.source, tunehubInfo); - downloadUrl = tunehubInfo.url || buildSongUrl(tunehubParams.source, tunehubParams.id); + } else if (tunehubParams.source === 'netease') { + const wyInfo = await getWySongInfo(uid, tunehubParams.id); + if (wyInfo) { + songInfo = buildSongInfoFromWyCloud(wyInfo); + } } } if (!songInfo) { - songInfo = await getMetaWithUrl(url); - logger.info(songInfo); - if (songInfo === false || songInfo.isTrial) { - logger.error(`fetch song info failed or it's a trial song. ${JSON.stringify(songInfo)}`); - return false; + if (!tunehubParams) { + songInfo = await getMetaWithUrl(url); + logger.info(songInfo); + if (songInfo === false || songInfo.isTrial) { + logger.error(`fetch song info failed or it's a trial song. ${JSON.stringify(songInfo)}`); + return false; + } + } else { + songInfo = buildFallbackSongInfo({ + songName, + artist, + album, + source: tunehubParams.source, + downloadUrl, + }); } } @@ -120,7 +167,7 @@ module.exports = async function syncSingleSongWithUrl(uid, url, { // step 3. download // should add meta tag if not matched song on wycloud let path = false; - if (usedTunehub && downloadUrl) { + if (useTunehubDownload && downloadUrl) { path = await downloadViaSourceUrl(downloadUrl); } if (path === false) {