Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main,renderer: add UnblockNeteaseMusic support #184

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
},
"dependencies": {
"@rocka/muse-ui": "^3.0.10",
"@unblockneteasemusic/server": "^0.27.8-patch.1",
"cookiejar": "^2.1.4",
"dbus-next": "^0.10.2",
"debug": "^4.3.7",
Expand Down
14 changes: 10 additions & 4 deletions src/main/api/downloader.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,18 @@ class Downloader {
async download(metadata, quality) {
d('Started to download id=%d, quality=%s', metadata.id, quality);
try {

const urlRes = await getMusicUrlE(metadata.id, quality);
if (urlRes.code !== 200 || urlRes.data[0].code !== 200) {
let urlRes;
try {
urlRes = await getMusicUrlE(metadata.id, quality);
} catch(e) {
throw new Error('获取下载链接失败');
}
const dlUrl = urlRes.data[0].url.replace(/^http:/, 'https:');
let dlUrl;
if (urlRes.data[0].isUnm == true){
dlUrl = urlRes.data[0].url;
} else {
dlUrl = urlRes.data[0].url.replace(/^http:/, 'https:');
}
const dlRes = await fetch(dlUrl);
if (dlRes.status !== 200) {
throw new Error(`下载失败 ${dlRes.status}`);
Expand Down
42 changes: 40 additions & 2 deletions src/main/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { app } from 'electron';

import { Lrc } from 'lrc-kit';
import { decodeHTML } from 'entities';
import debug from 'debug';
import match from '@unblockneteasemusic/server';

import Cache from './cache';
import migrate from './migrate';
Expand All @@ -16,6 +18,7 @@ import MusicServer from './musicServer';
import { getDiskUsage, clearDirectory } from '../util/fs';
import Downloader from './downloader';

const d = debug('API');
const BaseURL = 'https://music.163.com';
const client = new HttpClient();

Expand Down Expand Up @@ -298,15 +301,50 @@ export function getMusicUrlL(idOrIds, quality) {
* @param {Types.MusicQuality} quality
* @returns {Promise<Types.MusicUrlRes>}
*/
export function getMusicUrlE(idOrIds, quality) {
export async function getMusicUrlE(idOrIds, quality) {
if (!QualityMap[quality]) throw new Error(`Quality type '${quality}' is not in [ex,h,m,l]`);
let ids;
if (Array.isArray(idOrIds)) ids = idOrIds;
else ids = [idOrIds];
return client.postE('/song/enhance/player/url', {
let res = await client.postE('/song/enhance/player/url', {
ids,
br: QualityMap[quality],
});
let statusCode;
if (res.code !== 200 || res.data[0].code !== 200) {
statusCode = 2;
d('Cannot get music URL from Netease!');
} else if (res.data[0].fee === 1 && res.data[0].payed === 0) {
statusCode = 13;
hatateaya marked this conversation as resolved.
Show resolved Hide resolved
d('This music requires VIP privillege that we don\'t have.');
} else {
return res;
}
try {
const settings = await Settings.get();
if (settings.enableUnblock == true) {
d('Trying get from other source using UnblockNeteaseMusic...');
const unmData = await match(ids[0], ['qq', 'kugou', 'kuwo']);
res.data[0].size = unmData.size;
res.data[0].br = unmData.br;
res.data[0].url = unmData.url;
res.data[0].isUnm = true;
} else {
throw res;
}
} catch(e) {
if (e instanceof Error) {
console.error(e);
d('UnblockNeteaseMusic cannot find any fit music source for this music!');
}
if (statusCode == 13) {
d('Fallback to this music\'s trial version.');
res.data[0].isTrial = true;
} else {
throw e;
}
}
return res;
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/main/api/musicServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ class MusicServer {
async getMusicUrl(id, quality) {
const res = await getMusicUrlE(id, quality);
d('res: %o', res);
if (res.code !== 200 || res.data[0].code !== 200) throw res;
return res.data[0];
}

Expand Down Expand Up @@ -151,7 +150,12 @@ class MusicServer {
try {
const music = await this.getMusicUrl(Number.parseInt(id, 10), quality);
d('Got URL for music id=%d', id);
const musicRes = await this.cache.fetch(music.url.replace(/^http:/, 'https:'));
let musicRes;
if(music.isUnm == true){
musicRes = await this.cache.fetch(music.url);
} else {
musicRes = await this.cache.fetch(music.url.replace(/^http:/, 'https:'));
}
// TODO: write file only md5 matches
musicRes.body.pipe(fs.createWriteStream(this.cache.internalPath(fileName)));

Expand All @@ -175,10 +179,10 @@ class MusicServer {
});
musicRes.body.on('end', () => {
const md5 = checksum.digest('hex');
if (md5 === music.md5.toLowerCase()) {
if (music.isTrial != true && (music.isUnm == true || md5 === music.md5.toLowerCase())) {
d('Finish downloading music id=%d, md5=%s', id, md5);
} else {
d('Download music id=%d hash mismatch, delete it ...', id);
d('Download music id=%d hash mismatch or it\'s trial version, delete it ...', id);
this.cache.rm(fileName);
}
res.end();
Expand Down
3 changes: 2 additions & 1 deletion src/main/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export const defaultSettings = {
themeVariety: 'auto',
autoReplacePlaylist: false,
lyricTranslation: 'translation',
titleBarShowsTrackName: true
titleBarShowsTrackName: true,
enableUnblock: true
};

/**
Expand Down
5 changes: 5 additions & 0 deletions src/renderer/page/Settings/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ export const Entries = [
{ label: '翻译', value: 'translation' },
{ label: '罗马音', value: 'romaji' }
]
},
{
type: 'toggle',
title: '启用UnblockNeteaseMusic',
prop: 'enableUnblock'
}
]
},
Expand Down
3 changes: 2 additions & 1 deletion src/renderer/store/modules/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const DefaultSettings = {
themeVariety: 'auto',
autoReplacePlaylist: false,
lyricTranslation: 'translation',
titleBarShowsTrackName: true
titleBarShowsTrackName: true,
enableUnblock: true
};

/**
Expand Down
Loading