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:
pan93412 2021-12-20 18:56:26 +08:00 committed by GitHub
parent 3bc9d24677
commit 16c3613267
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 335 additions and 380 deletions

View file

@ -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 => {

View file

@ -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');

View file

@ -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);
};