diff --git a/README.MD b/README.MD index 4f8f946..1540fa8 100644 --- a/README.MD +++ b/README.MD @@ -19,17 +19,17 @@ api.search('年度之歌') # api ## search ```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 ```js -api.lrc(id:Number, [lv:Number default:-1]) +api.lrc(id:Number, [callback:function,lv:Number default:-1]) ``` ## song ```js -api.song(id:Number) +api.song(id:Number,[callback:function]) ``` [github](https://github.com/Binaryify/NeteaseCloudMusicApi) diff --git a/app.js b/app.js index 64e4535..e8d2077 100644 --- a/app.js +++ b/app.js @@ -13,34 +13,42 @@ let globalOption = { } let api = { - search: (name = null, limit = 3, offset = 0) => { + search: (name = null,callback=null, limit = 3, offset = 0) => { let option = deepCopy(globalOption); let url = origin + '/api/search/suggest/web' let body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset let method = 'POST' 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 url = origin + '/api/song/detail?ids=%5B' + id + '%5d' let method = 'GET' 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 url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id let method = 'GET' Object.assign(option, { url, method }) - request(option, callback); - } -} -function callback(error, response, body) { - // console.log('callback') - if (!error && response.statusCode == 200) { - var info = JSON.parse(body); - console.log(JSON.stringify(info, '', 2)) + request(option, (error, response, body)=>{ + if (!error && response.statusCode == 200) { + let info = JSON.parse(body); + callback&&callback(JSON.stringify(info, '', 2)) + } + }) } } export {api} \ No newline at end of file diff --git a/build/app.js b/build/app.js index 0d5d1ce..84ec02a 100644 --- a/build/app.js +++ b/build/app.js @@ -27,38 +27,50 @@ var globalOption = { var api = { search: function search() { var name = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; - var limit = arguments.length <= 1 || arguments[1] === undefined ? 3 : arguments[1]; - var offset = arguments.length <= 2 || arguments[2] === undefined ? 0 : arguments[2]; + var callback = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; + 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 url = origin + '/api/search/suggest/web'; var body = 's=' + name + '&limit=' + limit + '&type=1&offset=' + offset; var method = 'POST'; 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) { + var callback = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1]; + var option = deepCopy(globalOption); var url = origin + '/api/song/detail?ids=%5B' + id + '%5d'; var method = 'GET'; 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) { - 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 url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id; var method = 'GET'; 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)); + } + }); } }; -function callback(error, response, body) { - // console.log('callback') - if (!error && response.statusCode == 200) { - var info = JSON.parse(body); - console.log(JSON.stringify(info, '', 2)); - } -} exports.api = api; diff --git a/package.json b/package.json index af02d67..81ea76f 100644 --- a/package.json +++ b/package.json @@ -1,14 +1,14 @@ { "name": "NeteaseCloudMusicApi", - "version": "1.0.1", - "description": "", + "version": "1.0.8", + "description": "网易云音乐nodejs版接口模块", "main": "build/app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "babel app.js -o build/app.js", "check": "node build/test.js" }, - "keywords": [], + "keywords": ["NeteaseCloudMusic","网易云音乐"], "author": "traveller", "license": "ISC", "dependencies": { diff --git a/test.js b/test.js index 74635ea..115bfac 100644 --- a/test.js +++ b/test.js @@ -1,4 +1,10 @@ import {api }from './app.js' -api.search("远方") -api.song('5243023') -api.lrc('5243023') \ No newline at end of file +api.search("年度之歌",(data)=>{ + console.log(data) +}) +api.song('308169',(data)=>{ + console.log(data) +}) +api.lrc('5243023',(data)=>{ + console.log(data) +}) \ No newline at end of file