Skip to content
This repository has been archived by the owner on Jun 26, 2024. It is now read-only.

Commit

Permalink
Feature #173
Browse files Browse the repository at this point in the history
  • Loading branch information
trazyn committed Jul 23, 2018
1 parent 06fbe6c commit a4adcd8
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 91 deletions.
77 changes: 18 additions & 59 deletions server/router/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,49 +117,6 @@ async function getAlbumBySong(id) {
return albums;
}

async function getFlac(name, artists) {
var response = await axios.get(
'http://sug.music.baidu.com/info/suggestion',
{
params: {
word: [name].concat(artists.split(',')).join('+'),
version: 2,
from: 0,
}
}
);

if (response.data.errno) {
return false;
}

var songs = response.data.data.song;
var song = songs.find(e => artists.indexOf(e.artistname) > -1);

if (!song) {
return false;
}

var rp = require('request-promise-native');

response = await rp(
{
uri: 'http://music.taihe.com/data/music/fmlink',
qs: {
songIds: song.songid,
type: 'flac',
},
json: true,
}
);

if (+response.errorCode !== 22000 || !response.data.songList) {
return false;
}

return response.data.songList[0].songLink;
}

router.get('/subscribe/:id', async(req, res) => {
debug('Handle request for /player/subscribe');

Expand Down Expand Up @@ -224,25 +181,27 @@ router.get('/song/:id/:name/:artists/:flac?', cache('3 minutes', onlyStatus200),
debug('Params \'id\': %s, \'name\': %s, \'artists\': %s, \'flac\': %s', id, name, artists, flac);

try {
if (+flac) {
let src = await getFlac(name, artists);

if (src) {
res.send({
song: {
src,
isFlac: true,
}
});

return;
}
}

if (!process.env.APIONLY) {
var selector = require('../search');

// Search from other source
debug(chalk.underline.black.bgYellow(`🔎 ${name} - ${artists}`));
song = await require('../search').default(name, artists, id);

try {
// Get the highquality track
song = await selector.getFlac(name, artists, true);
} catch (ex) {
error(ex);
}

if (!song.src) {
if (flac === 1) {
// Only accept highquality
} else {
// Try to get the normal quality track
song = await selector.getMp3(name, artists, id);
}
}
}
} catch (ex) {
debug(chalk.red.underline.bold(`💔 Not found: "${name} - ${artists}"`));
Expand Down
8 changes: 6 additions & 2 deletions server/search/Baidu.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import chalk from 'chalk';
const debug = _debug('dev:plugin:Baidu');
const error = _debug('dev:plugin:Baidu:error');

export default async(request, keyword, artists) => {
export default async(request, keyword, artists, isFlac) => {
debug(chalk.black.bgGreen('💊 Loaded Baidu music.'));

try {
Expand All @@ -29,7 +29,7 @@ export default async(request, keyword, artists) => {
uri: 'http://music.taihe.com/data/music/fmlink',
qs: {
songIds: song.songid,
type: 'mp3',
type: isFlac ? 'flac' : 'mp3',
},
});

Expand All @@ -53,6 +53,10 @@ export default async(request, keyword, artists) => {
debug(chalk.black.bgGreen('🚚 Result >>>'));
debug(response.data.songList[0]);
debug(chalk.black.bgGreen('🚚 <<<'));

if (isFlac) {
song.isFlac = true;
}
}
} catch (ex) {
// Anti-warnning
Expand Down
11 changes: 8 additions & 3 deletions server/search/QQ.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const error = _debug('dev:plugin:QQ:error');

let rp;

async function getSong(mid) {
async function getSong(mid, isFlac) {
var currentMs = (new Date()).getUTCMilliseconds();
var guid = Math.round(2147483647 * Math.random()) * currentMs % 1e10;
var file = await genKey(mid);
Expand Down Expand Up @@ -49,6 +49,11 @@ async function getSong(mid) {
};
}

if (isFlac === true) {
// Not found flac track
return {};
}

if (file.size_320mp3) {
return {
bitRate: 320000,
Expand Down Expand Up @@ -106,7 +111,7 @@ function getURL(filename, key, guid) {
return `http://dl.stream.qqmusic.qq.com/${filename}?vkey=${key}&guid=${guid}&fromtag=53`;
}

export default async(request, keyword, artists) => {
export default async(request, keyword, artists, isFlac) => {
debug(chalk.black.bgGreen('💊 Loaded QQ music.'));

rp = request;
Expand Down Expand Up @@ -143,7 +148,7 @@ export default async(request, keyword, artists) => {
continue;
}

song = await getSong(e.media_mid);
song = await getSong(e.media_mid, isFlac);

if (!song.src) {
error(chalk.black.bgRed('🚧 Nothing.'));
Expand Down
64 changes: 39 additions & 25 deletions server/search/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,46 @@ async function getPreferences() {
});
}

export default async(keyword, artists, id /** This id is only work for netease music */) => {
async function exe(plugins, ...args) {
var preferences = await getPreferences();
var enginers = preferences.enginers;
var rpOptions = {
timeout: 10000,
json: true,
jar: true,
};

if (preferences.proxy) {
Object.assign(
rpOptions,
{
proxy: preferences.proxy,
}
);
}
var rp = require('request-promise-native').defaults(rpOptions);

return Promise.all(
plugins.map(e => {
// If a request failed will keep waiting for other possible successes, if a request successed,
// treat it as a rejection so Promise.all immediate break.
return e(rp, ...args).then(
val => Promise.reject(val),
err => Promise.resolve(err)
);
})
).then(
errs => Promise.reject(errs),
val => Promise.resolve(val),
);
}

async function getFlac(keyword, artists) {
return exe([Baidu, QQ], keyword, artists, true);
}

async function getMp3(keyword, artists, id /** This id is only work for netease music */) {
var preferences = await getPreferences();
var enginers = preferences.enginers;
var plugins = [Netease];

if (!enginers) {
Expand All @@ -37,15 +69,6 @@ export default async(keyword, artists, id /** This id is only work for netease m
};
}

if (preferences.proxy) {
Object.assign(
rpOptions,
{
proxy: preferences.proxy,
}
);
}

if (enginers['QQ']) {
plugins.push(QQ);
}
Expand All @@ -70,19 +93,10 @@ export default async(keyword, artists, id /** This id is only work for netease m
plugins.push(Kuwo);
}

var rp = require('request-promise-native').defaults(rpOptions);
return exe(plugins, keyword, artists, id);
}

return Promise.all(
plugins.map(e => {
// If a request failed will keep waiting for other possible successes, if a request successed,
// treat it as a rejection so Promise.all immediate break.
return e(rp, keyword, artists, id).then(
val => Promise.reject(val),
err => Promise.resolve(err)
);
})
).then(
errs => Promise.reject(errs),
val => Promise.resolve(val),
);
export {
getFlac,
getMp3,
};
5 changes: 4 additions & 1 deletion src/js/components/Preferences/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ class Preferences extends Component {
</label>

<label htmlFor="highquality">
<h4>High Quality Music</h4>
<div>
<h4>Only High Quality</h4>
<p>Only the high quality track accepted, Usually you not need enable this option.</p>
</div>

<Switch
defaultChecked={highquality}
Expand Down
2 changes: 1 addition & 1 deletion src/js/stores/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Preferences {
@observable naturalScroll = true;
@observable volume = 1;
@observable port = config.api.port;
@observable highquality = 1;
@observable highquality = 0;
@observable autoupdate = false;
@observable lastfm = {
username: '', // Your last.fm username
Expand Down

0 comments on commit a4adcd8

Please sign in to comment.