This commit is contained in:
binaryify 2016-06-22 23:44:13 +08:00
parent 51bb402376
commit 774de65ca5
5 changed files with 61 additions and 35 deletions

View File

@ -19,17 +19,17 @@ api.search('年度之歌')
# api # api
## search ## search
```js ```js
api.search(name:String, [limit:Nnumber default:3, offset:Number default:0]) api.search(name:String, [callback:function,limit:Nnumber default:3, offset:Number default:0])
``` ```
## lrc ## lrc
```js ```js
api.lrc(id:Number, [lv:Number default:-1]) api.lrc(id:Number, [callback:function,lv:Number default:-1])
``` ```
## song ## song
```js ```js
api.song(id:Number) api.song(id:Number,[callback:function])
``` ```
[github](https://github.com/Binaryify/NeteaseCloudMusicApi) [github](https://github.com/Binaryify/NeteaseCloudMusicApi)

32
app.js
View File

@ -13,34 +13,42 @@ let globalOption = {
} }
let api = { let api = {
search: (name = null, limit = 3, offset = 0) => { search: (name = null,callback=null, limit = 3, offset = 0) => {
let option = deepCopy(globalOption); let option = deepCopy(globalOption);
let url = origin + '/api/search/suggest/web' let url = origin + '/api/search/suggest/web'
let body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset let body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset
let method = 'POST' let method = 'POST'
Object.assign(option, { url, body, method }) Object.assign(option, { url, body, method })
request(option, callback); request(option, (error, response, body)=>{
if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
callback&&callback(JSON.stringify(info, '', 2))
}
})
}, },
song: (id) => { song: (id,callback=null) => {
let option = deepCopy(globalOption); let option = deepCopy(globalOption);
let url = origin + '/api/song/detail?ids=%5B' + id + '%5d' let url = origin + '/api/song/detail?ids=%5B' + id + '%5d'
let method = 'GET' let method = 'GET'
Object.assign(option, { url, method }) Object.assign(option, { url, method })
request(option, callback); request(option, (error, response, body)=>{
if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
callback&&callback(JSON.stringify(info, '', 2))
}
})
}, },
lrc: (id, lv = -1) => { lrc: (id,callback=null, lv = -1) => {
let option = deepCopy(globalOption); let option = deepCopy(globalOption);
let url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id let url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id
let method = 'GET' let method = 'GET'
Object.assign(option, { url, method }) Object.assign(option, { url, method })
request(option, callback); request(option, (error, response, body)=>{
}
}
function callback(error, response, body) {
// console.log('callback')
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
var info = JSON.parse(body); let info = JSON.parse(body);
console.log(JSON.stringify(info, '', 2)) callback&&callback(JSON.stringify(info, '', 2))
}
})
} }
} }
export {api} export {api}

View File

@ -27,38 +27,50 @@ var globalOption = {
var api = { var api = {
search: function search() { search: function search() {
var name = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; var name = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0];
var limit = arguments.length <= 1 || arguments[1] === undefined ? 3 : arguments[1]; var callback = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var offset = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2]; var limit = arguments.length <= 2 || arguments[2] === undefined ? 3 : arguments[2];
var offset = arguments.length <= 3 || arguments[3] === undefined ? 0 : arguments[3];
var option = deepCopy(globalOption); var option = deepCopy(globalOption);
var url = origin + '/api/search/suggest/web'; var url = origin + '/api/search/suggest/web';
var body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset; var body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset;
var method = 'POST'; var method = 'POST';
Object.assign(option, { url: url, body: body, method: method }); Object.assign(option, { url: url, body: body, method: method });
(0, _request2.default)(option, callback); (0, _request2.default)(option, function (error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
callback && callback(JSON.stringify(info, '', 2));
}
});
}, },
song: function song(id) { song: function song(id) {
var callback = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var option = deepCopy(globalOption); var option = deepCopy(globalOption);
var url = origin + '/api/song/detail?ids=%5B' + id + '%5d'; var url = origin + '/api/song/detail?ids=%5B' + id + '%5d';
var method = 'GET'; var method = 'GET';
Object.assign(option, { url: url, method: method }); Object.assign(option, { url: url, method: method });
(0, _request2.default)(option, callback); (0, _request2.default)(option, function (error, response, body) {
if (!error && response.statusCode == 200) {
var info = JSON.parse(body);
callback && callback(JSON.stringify(info, '', 2));
}
});
}, },
lrc: function lrc(id) { lrc: function lrc(id) {
var lv = arguments.length <= 1 || arguments[1] === undefined ? -1 : arguments[1]; var callback = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
var lv = arguments.length <= 2 || arguments[2] === undefined ? -1 : arguments[2];
var option = deepCopy(globalOption); var option = deepCopy(globalOption);
var url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id; var url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id;
var method = 'GET'; var method = 'GET';
Object.assign(option, { url: url, method: method }); Object.assign(option, { url: url, method: method });
(0, _request2.default)(option, callback); (0, _request2.default)(option, function (error, response, body) {
}
};
function callback(error, response, body) {
// console.log('callback')
if (!error && response.statusCode == 200) { if (!error && response.statusCode == 200) {
var info = JSON.parse(body); var info = JSON.parse(body);
console.log(JSON.stringify(info, '', 2)); callback && callback(JSON.stringify(info, '', 2));
} }
});
} }
};
exports.api = api; exports.api = api;

View File

@ -1,14 +1,14 @@
{ {
"name": "NeteaseCloudMusicApi", "name": "NeteaseCloudMusicApi",
"version": "1.0.1", "version": "1.0.8",
"description": "", "description": "网易云音乐nodejs版接口模块",
"main": "build/app.js", "main": "build/app.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "echo \"Error: no test specified\" && exit 1",
"build": "babel app.js -o build/app.js", "build": "babel app.js -o build/app.js",
"check": "node build/test.js" "check": "node build/test.js"
}, },
"keywords": [], "keywords": ["NeteaseCloudMusic","网易云音乐"],
"author": "traveller", "author": "traveller",
"license": "ISC", "license": "ISC",
"dependencies": { "dependencies": {

12
test.js
View File

@ -1,4 +1,10 @@
import {api }from './app.js' import {api }from './app.js'
api.search("远方") api.search("年度之歌",(data)=>{
api.song('5243023') console.log(data)
api.lrc('5243023') })
api.song('308169',(data)=>{
console.log(data)
})
api.lrc('5243023',(data)=>{
console.log(data)
})