Skip to content

Commit

Permalink
Merge branch 'feature/promise-any-and-ytdl' into enhanced
Browse files Browse the repository at this point in the history
Signed-off-by: Tianling Shen <[email protected]>
  • Loading branch information
1715173329 committed Jun 5, 2021
2 parents 5145a86 + dd7e3d1 commit a0e070b
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 27 deletions.
36 changes: 36 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

# Use 4 spaces for the Python files
[*.py]
indent_size = 4
max_line_length = 80

# The JSON files contain newlines inconsistently
[*.json]
insert_final_newline = ignore

# Minified JavaScript files shouldn't be changed
[**.min.js]
indent_style = ignore
insert_final_newline = ignore

# Makefiles always use tabs for indentation
[Makefile]
indent_style = tab

# Batch files use tabs for indentation
[*.bat]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

4 changes: 2 additions & 2 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ if (config.forceHost && require('net').isIP(config.forceHost) === 0) {
process.exit(1)
}
if (config.matchOrder) {
const provider = new Set(['netease', 'qq', 'baidu', 'kugou', 'kuwo', 'migu', 'joox', 'youtube', 'bilibili', 'pyncmd'])
const provider = Object.keys(require("./consts").PROVIDERS);
const candidate = config.matchOrder
if (candidate.some((key, index) => index != candidate.indexOf(key))) {
console.log('Please check the duplication in match order.')
process.exit(1)
}
else if (candidate.some(key => !provider.has(key))) {
else if (candidate.some(key => !provider.includes(key))) {
console.log('Please check the availability of match sources.')
process.exit(1)
}
Expand Down
18 changes: 18 additions & 0 deletions src/consts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const DEFAULT_SOURCE = ["netease", "kuwo", "migu", "bilibili", "pyncmd"];
const PROVIDERS = {
netease: require('./provider/netease'),
qq: require('./provider/netease'),
baidu: require('./provider/baidu'),
kugou: require('./provider/kugou'),
kuwo: require('./provider/kuwo'),
migu: require('./provider/migu'),
joox: require('./provider/joox'),
youtube: require('./provider/youtube'),
bilibili: require('./provider/bilibili'),
pyncmd: require('./provider/pyncmd')
}

module.exports = {
DEFAULT_SOURCE,
PROVIDERS,
};
22 changes: 22 additions & 0 deletions src/polyfills.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Polyfills
*/

/**
* The polyfill of Promise.any.
*
* commit e1e75a1410490906d2da1bda0d4aafb1788f2195
* file index.js
*
* Thanks to https://github.com/ungap/promise-any
*/
Promise.any = (Promise.any || function ($) {
return new Promise(function (D, E, A, L) {
A = [];
L = $.map(function ($, i) {
return Promise.resolve($).then(D, function (O) {
return ((A[i] = O), --L) || E({ errors: A });
});
}).length;
});
}).bind(Promise);
37 changes: 13 additions & 24 deletions src/provider/match.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,26 @@
require("../polyfills");

const find = require('./find')
const request = require('../request')

const provider = {
netease: require('./netease'),
qq: require('./qq'),
baidu: require('./baidu'),
kugou: require('./kugou'),
kuwo: require('./kuwo'),
migu: require('./migu'),
joox: require('./joox'),
youtube: require('./youtube'),
bilibili: require('./bilibili'),
pyncmd: require('./pyncmd')
}
const consts = require("../consts")
const providers = consts.PROVIDERS;
const defaultSrc = consts.DEFAULT_SOURCE;

const match = (id, source, data) => {
let meta = {}
const candidate = (source || global.source || ['qq', 'kuwo', 'migu']).filter(name => name in provider)
const candidate = (source || global.source || defaultSrc).filter(name => name in providers)
return find(id, data)
.then(info => {
meta = info
return Promise.all(candidate.map(name => provider[name].check(info).catch(() => {})))
})
.then(urls => {
urls = urls.filter(url => url)
return Promise.all(urls.map(url => check(url)))
return Promise.any(candidate.map(name => providers[name].check(info).then(data => data ? data : Promise.reject())));
})
.then(songs => {
songs = songs.filter(song => song.url)
if (!songs.length) return Promise.reject()
console.log(`[${meta.id}] ${meta.name}\n${songs[0].url}`)
return songs[0]
.then(url => {
return check(url).then(song => song.url ? song : Promise.reject());
})
.then(song => {
console.log(`[${meta.id}] ${meta.name}\n${song.url}`)
return song
});
}

const check = url => {
Expand Down
2 changes: 1 addition & 1 deletion src/provider/youtube.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const search = info => {

const track = id => {
const url =
`https://www.youtube.com/get_video_info?video_id=${id}&el=detailpage`
`https://www.youtube.com/get_video_info?video_id=${id}&el=detailpage&html5=1`

return request('GET', url, {}, null, proxy)
.then(response => response.body())
Expand Down

0 comments on commit a0e070b

Please sign in to comment.