feat(ipcMain/unm): allow passing customized source

This commit is contained in:
pan93412 2021-12-27 19:27:01 +08:00
parent c73da5c5ad
commit c280221a44
No known key found for this signature in database
GPG Key ID: 42154B1B1CFE3377

View File

@ -48,8 +48,18 @@ async function getBiliVideoFile(url) {
return `data:application/octet-stream;base64,${encodedData}`;
}
/**
* Parse the source string (`a, b`) to source list `['a', 'b']`.
*
* @param {string} sourceString The source string.
* @returns {string[]} The source list.
*/
function parseSourceStringToList(sourceString) {
return sourceString.split(',').map(s => s.trim());
}
export function initIpcMain(win, store) {
ipcMain.handle('unblock-music', async (_, track) => {
ipcMain.handle('unblock-music', async (_, track, source) => {
// 兼容 unblockneteasemusic 所使用的 api 字段
track.alias = track.alia || [];
track.duration = track.dt || 0;
@ -62,11 +72,15 @@ export function initIpcMain(win, store) {
}, 5000);
});
const sourceList =
typeof source === 'string' ? parseSourceStringToList(source) : null;
log(`[UNM] using source: ${sourceList || '<default>'}`);
try {
const matchedAudio = await Promise.race([
// TODO: tell users to install yt-dlp.
// we passed "null" to source, to let UNM choose the default source.
match(track.id, null, track),
match(track.id, sourceList, track),
timeoutPromise,
]);