mirror of
https://github.com/GiriNeko/YesPlayMusic.git
synced 2025-12-16 21:28:06 +00:00
feat: Use actively maintained unblockNeteaseMusic (#1105)
* refactor: use @unblockneteasemusic/server we also use ipcMain.handle for unblock-music. * refactor(utils/nativeAlert): remove the deprecated "remote" * refactor(ipcMain): use our default sources * style(config/vue): prettier * feat(README): update for new UNM
This commit is contained in:
parent
3bc9d24677
commit
16c3613267
7 changed files with 335 additions and 380 deletions
|
|
@ -1,5 +1,5 @@
|
|||
import { app, dialog, globalShortcut, ipcMain } from 'electron';
|
||||
import match from '@revincx/unblockneteasemusic';
|
||||
import match from '@unblockneteasemusic/server';
|
||||
import { registerGlobalShortcut } from '@/electron/globalShortcut';
|
||||
import cloneDeep from 'lodash/cloneDeep';
|
||||
import shortcuts from '@/utils/shortcuts';
|
||||
|
|
@ -12,28 +12,79 @@ const log = text => {
|
|||
|
||||
const client = require('discord-rich-presence')('818936529484906596');
|
||||
|
||||
/**
|
||||
* Make data a Buffer.
|
||||
*
|
||||
* @param {?} data The data to convert.
|
||||
* @returns {import("buffer").Buffer} The converted data.
|
||||
*/
|
||||
function toBuffer(data) {
|
||||
if (data instanceof Buffer) {
|
||||
return data;
|
||||
} else {
|
||||
return Buffer.from(data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the file URI from bilivideo.
|
||||
*
|
||||
* @param {string} url The URL to fetch.
|
||||
* @returns {Promise<string>} The file URI.
|
||||
*/
|
||||
async function getBiliVideoFile(url) {
|
||||
const axios = await import('axios').then(m => m.default);
|
||||
const response = await axios.get(url, {
|
||||
headers: {
|
||||
Referer: 'https://www.bilibili.com/',
|
||||
'User-Agent': 'okhttp/3.4.1',
|
||||
},
|
||||
responseType: 'arraybuffer',
|
||||
});
|
||||
|
||||
const buffer = toBuffer(response.data);
|
||||
const encodedData = buffer.toString('base64');
|
||||
|
||||
return `data:application/octet-stream;base64,${encodedData}`;
|
||||
}
|
||||
|
||||
export function initIpcMain(win, store) {
|
||||
ipcMain.on('unblock-music', (event, track) => {
|
||||
ipcMain.handle('unblock-music', async (_, track) => {
|
||||
// 兼容 unblockneteasemusic 所使用的 api 字段
|
||||
track.alias = track.alia || [];
|
||||
track.duration = track.dt || 0;
|
||||
track.album = track.al || [];
|
||||
track.artists = track.ar || [];
|
||||
|
||||
const matchPromise = match(track.id, ['qq', 'kuwo', 'migu'], track);
|
||||
const timeoutPromise = new Promise((_, reject) => {
|
||||
setTimeout(() => {
|
||||
reject('timeout');
|
||||
}, 3000);
|
||||
}, 5000);
|
||||
});
|
||||
Promise.race([matchPromise, timeoutPromise])
|
||||
.then(res => {
|
||||
event.returnValue = res;
|
||||
})
|
||||
.catch(err => {
|
||||
log('unblock music error: ', err);
|
||||
event.returnValue = null;
|
||||
});
|
||||
|
||||
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),
|
||||
timeoutPromise,
|
||||
]);
|
||||
|
||||
if (!matchedAudio || !matchedAudio.url) {
|
||||
throw new Error('no such a song found');
|
||||
}
|
||||
|
||||
// bilibili's audio file needs some special treatment
|
||||
if (matchedAudio.url.includes('bilivideo.com')) {
|
||||
matchedAudio.url = await getBiliVideoFile(matchedAudio.url);
|
||||
}
|
||||
|
||||
return matchedAudio;
|
||||
} catch (err) {
|
||||
const errorMessage = err instanceof Error ? `${err.message}` : `${err}`;
|
||||
log(`UnblockNeteaseMusic failed: ${errorMessage}`);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on('close', e => {
|
||||
|
|
|
|||
|
|
@ -269,7 +269,7 @@ export default class {
|
|||
});
|
||||
}
|
||||
}
|
||||
_getAudioSourceFromUnblockMusic(track) {
|
||||
async _getAudioSourceFromUnblockMusic(track) {
|
||||
console.debug(`[debug][Player.js] _getAudioSourceFromUnblockMusic`);
|
||||
if (
|
||||
process.env.IS_ELECTRON !== true ||
|
||||
|
|
@ -277,7 +277,7 @@ export default class {
|
|||
) {
|
||||
return null;
|
||||
}
|
||||
const source = ipcRenderer.sendSync('unblock-music', track);
|
||||
const source = await ipcRenderer.invoke('unblock-music', track);
|
||||
if (store.state.settings.automaticallyCacheSongs && source?.url) {
|
||||
// TODO: 将unblockMusic字样换成真正的来源(比如酷我咪咕等)
|
||||
cacheTrackSource(track, source.url, 128000, 'unblockMusic');
|
||||
|
|
|
|||
|
|
@ -13,14 +13,12 @@
|
|||
*/
|
||||
const nativeAlert = (() => {
|
||||
if (process.env.IS_ELECTRON === true) {
|
||||
const {
|
||||
remote: { dialog },
|
||||
} = require('electron');
|
||||
const { dialog } = require('electron');
|
||||
if (dialog) {
|
||||
return message => {
|
||||
var options = {
|
||||
type: 'warning',
|
||||
detail: message,
|
||||
message,
|
||||
};
|
||||
dialog.showMessageBoxSync(null, options);
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue