refactor(音乐平台集成): 重构网易云音乐接口调用逻辑
- 将tunehub的音乐平台接口替换为直接调用wycloud接口 - 简化歌曲信息处理逻辑,移除冗余的splitArtists函数 - 在同步歌曲时增加对网易云音乐的直接支持 - 添加歌曲信息构建的辅助函数,提高代码复用性
This commit is contained in:
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user