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');
|
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} = {}) {
|
module.exports = async function findTheBestMatchFromWyCloud(uid, {songName, artist, album, musicPlatformSongId} = {}) {
|
||||||
if (musicPlatformSongId) {
|
if (musicPlatformSongId) {
|
||||||
const songInfo = await getSongInfo('netease', musicPlatformSongId);
|
const songInfo = await getSongInfo(uid, musicPlatformSongId);
|
||||||
|
|
||||||
if (songInfo) {
|
if (songInfo) {
|
||||||
return {
|
return songInfo;
|
||||||
songId: musicPlatformSongId,
|
|
||||||
songName: songInfo.name || songName,
|
|
||||||
artists: splitArtists(songInfo.artist),
|
|
||||||
album: songInfo.album || album,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
if (songName && artist) {
|
if (songName && artist) {
|
||||||
return {
|
return {
|
||||||
@@ -37,24 +23,12 @@ module.exports = async function findTheBestMatchFromWyCloud(uid, {songName, arti
|
|||||||
if (songName === "" || artist === "") {
|
if (songName === "" || artist === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
const searchData = await searchSongs(`${songName} ${artist}`, {
|
const searchLists = await searchSong(uid, songName, artist);
|
||||||
source: 'netease',
|
logger.info('searchLists', searchLists);
|
||||||
limit: 20,
|
if (searchLists === false) {
|
||||||
aggregate: false,
|
|
||||||
});
|
|
||||||
if (searchData === false || !searchData.results) {
|
|
||||||
logger.warn(`search song failed, no matter, go on`);
|
logger.warn(`search song failed, no matter, go on`);
|
||||||
return null;
|
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;
|
let matchSongAndArtist = null;
|
||||||
for (const searchItem of searchLists) {
|
for (const searchItem of searchLists) {
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ const utilFs = require('../../utils/fs');
|
|||||||
const { downloadFromLocalTmpPath } = require('./download_to_local');
|
const { downloadFromLocalTmpPath } = require('./download_to_local');
|
||||||
const uploadWithRetryThenMatch = require('./upload_to_wycloud_disk_with_retry_then_match');
|
const uploadWithRetryThenMatch = require('./upload_to_wycloud_disk_with_retry_then_match');
|
||||||
const { getSongInfo, buildSongUrl } = require('../music_platform/tunehub');
|
const { getSongInfo, buildSongUrl } = require('../music_platform/tunehub');
|
||||||
|
const { getSongInfo: getWySongInfo } = require('../music_platform/wycloud');
|
||||||
|
|
||||||
function parseTunehubParams(url) {
|
function parseTunehubParams(url) {
|
||||||
try {
|
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, {
|
module.exports = async function syncSingleSongWithUrl(uid, url, {
|
||||||
songName = "",
|
songName = "",
|
||||||
artist = "",
|
artist = "",
|
||||||
@@ -72,23 +104,38 @@ module.exports = async function syncSingleSongWithUrl(uid, url, {
|
|||||||
// step 1. fetch song info
|
// step 1. fetch song info
|
||||||
let songInfo = null;
|
let songInfo = null;
|
||||||
let downloadUrl = url;
|
let downloadUrl = url;
|
||||||
let usedTunehub = false;
|
let useTunehubDownload = false;
|
||||||
const tunehubParams = parseTunehubParams(url) || parsePageUrlParams(url);
|
const tunehubParams = parseTunehubParams(url) || parsePageUrlParams(url);
|
||||||
if (tunehubParams) {
|
if (tunehubParams) {
|
||||||
|
useTunehubDownload = true;
|
||||||
|
downloadUrl = buildSongUrl(tunehubParams.source, tunehubParams.id);
|
||||||
const tunehubInfo = await getSongInfo(tunehubParams.source, tunehubParams.id);
|
const tunehubInfo = await getSongInfo(tunehubParams.source, tunehubParams.id);
|
||||||
if (tunehubInfo) {
|
if (tunehubInfo) {
|
||||||
usedTunehub = true;
|
|
||||||
songInfo = buildSongInfoFromTunehub(tunehubParams.source, tunehubInfo);
|
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) {
|
if (!songInfo) {
|
||||||
|
if (!tunehubParams) {
|
||||||
songInfo = await getMetaWithUrl(url);
|
songInfo = await getMetaWithUrl(url);
|
||||||
logger.info(songInfo);
|
logger.info(songInfo);
|
||||||
if (songInfo === false || songInfo.isTrial) {
|
if (songInfo === false || songInfo.isTrial) {
|
||||||
logger.error(`fetch song info failed or it's a trial song. ${JSON.stringify(songInfo)}`);
|
logger.error(`fetch song info failed or it's a trial song. ${JSON.stringify(songInfo)}`);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
songInfo = buildFallbackSongInfo({
|
||||||
|
songName,
|
||||||
|
artist,
|
||||||
|
album,
|
||||||
|
source: tunehubParams.source,
|
||||||
|
downloadUrl,
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await updateJobIfNeed(uid, jobId, songInfo, jobType);
|
await updateJobIfNeed(uid, jobId, songInfo, jobType);
|
||||||
@@ -120,7 +167,7 @@ module.exports = async function syncSingleSongWithUrl(uid, url, {
|
|||||||
// 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
|
||||||
let path = false;
|
let path = false;
|
||||||
if (usedTunehub && downloadUrl) {
|
if (useTunehubDownload && downloadUrl) {
|
||||||
path = await downloadViaSourceUrl(downloadUrl);
|
path = await downloadViaSourceUrl(downloadUrl);
|
||||||
}
|
}
|
||||||
if (path === false) {
|
if (path === false) {
|
||||||
|
|||||||
Reference in New Issue
Block a user