From f4c1b6ccb5665a00fbfedf87194999343326f094 Mon Sep 17 00:00:00 2001 From: rccoder Date: Thu, 30 Jun 2016 23:53:38 +0800 Subject: [PATCH] Update README & Fix proxy bug & plan directory --- .editorconfig | 20 +++++++++++++++++ README.MD | 38 ++++++++++++++++++-------------- app.js | 55 ---------------------------------------------- build/app.js | 27 +++++++++-------------- build/config.js | 17 +++++++++++++++ build/util.js | 10 +++++++++ package.json | 4 ++-- src/app.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ src/config.js | 11 ++++++++++ src/util.js | 3 +++ test.js | 10 --------- test/test.js | 11 ++++++++++ 12 files changed, 164 insertions(+), 100 deletions(-) create mode 100644 .editorconfig delete mode 100644 app.js create mode 100644 build/config.js create mode 100644 build/util.js create mode 100644 src/app.js create mode 100644 src/config.js create mode 100644 src/util.js delete mode 100644 test.js create mode 100644 test/test.js diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..04ba039 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,20 @@ +# EditorConfig helps developers define and maintain consistent +# coding styles between different editors and IDEs +# editorconfig.org + +root = true + +[*] + +# Change these settings to your own preference +indent_style = space +indent_size = 2 + +# We recommend you to keep these unchanged +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true + +[*.md] +trim_trailing_whitespace = false diff --git a/README.MD b/README.MD index fcbbf32..27eefb4 100644 --- a/README.MD +++ b/README.MD @@ -1,41 +1,47 @@ -一个调用网易云音乐API的node模块 +# NeteaseCloudMusicApi + +一个调用网易云音乐 API 的 node 模块 + ![](http://binaryify.github.io/images/api.jpg) -# start -``` + +## Start +``` shell npm install NeteaseCloudMusicApi ``` -# usage -```js -var api=require('NeteaseCloudMusicApi').api +## Usage +``` javascript +var api = require('NeteaseCloudMusicApi').api api.search('年度之歌',function(data){ console.log(data) }) ``` or -```js +``` javascript import {api} from 'NeteaseCloudMusicApi' -api.search('年度之歌',(data)=>{ +api.search('年度之歌',(data) => { console.log(data) }) ``` -# api -## search - ```js +## API + +### search + ``` javascript api.search(name:String,[callback:function,limit:Nnumber default:3, offset:Number default:0]) ``` -## lrc - ```js +### lrc + ``` javascript api.lrc(id:Number,[callback:function,lv:Number default:-1]) ``` -## song - ```js +### song + ``` javascript api.song(id:Number,[callback:function]) ``` +## Download [github](https://github.com/Binaryify/NeteaseCloudMusicApi) -[npm](https://www.npmjs.com/package/NeteaseCloudMusicApi) \ No newline at end of file +[npm](https://www.npmjs.com/package/NeteaseCloudMusicApi) diff --git a/app.js b/app.js deleted file mode 100644 index a30b7b1..0000000 --- a/app.js +++ /dev/null @@ -1,55 +0,0 @@ -import request from 'request' -const deepCopy = (obj) => JSON.parse(JSON.stringify(obj)) -const origin = 'http://music.163.com' -let globalOption = { - headers: { - 'Origin': origin, - 'Referer': origin, - 'Content-Type': 'application/x-www-form-urlencoded' - } -} -let api = { - search: (name = null, callback = null, limit = 3, offset = 0) => { - let option = deepCopy(globalOption); - let url = origin + '/api/search/suggest/web' - let form = { - s: name, - limit, - type: 1, - offset - } - let method = 'POST' - Object.assign(option, { url, form, method }) - request(option, (error, response, body) => { - if (!error && response.statusCode == 200) { - let info = JSON.parse(body); - callback && callback(JSON.stringify(info, '', 2)) - } - }) - }, - 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, (error, response, body) => { - if (!error && response.statusCode == 200) { - let info = JSON.parse(body); - callback && callback(JSON.stringify(info, '', 2)) - } - }) - }, - 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, (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 f75d470..a08900c 100644 --- a/build/app.js +++ b/build/app.js @@ -9,19 +9,12 @@ var _request = require('request'); var _request2 = _interopRequireDefault(_request); +var _config = require('./config'); + +var _util = require('./util'); + function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } -var deepCopy = function deepCopy(obj) { - return JSON.parse(JSON.stringify(obj)); -}; -var origin = 'http://music.163.com'; -var globalOption = { - headers: { - 'Origin': origin, - 'Referer': origin, - 'Content-Type': 'application/x-www-form-urlencoded' - } -}; var api = { search: function search() { var name = arguments.length <= 0 || arguments[0] === undefined ? null : arguments[0]; @@ -29,8 +22,8 @@ var api = { 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 option = (0, _util.deepCopy)(_config.globalOption); + var url = _config.origin + '/api/search/suggest/web'; var form = { s: name, limit: limit, @@ -49,8 +42,8 @@ var api = { 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 option = (0, _util.deepCopy)(_config.globalOption); + var url = _config.origin + '/api/song/detail?ids=%5B' + id + '%5d'; var method = 'GET'; Object.assign(option, { url: url, method: method }); (0, _request2.default)(option, function (error, response, body) { @@ -64,8 +57,8 @@ var api = { 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 option = (0, _util.deepCopy)(_config.globalOption); + var url = _config.origin + '/api/song/lyric?lv=' + lv + '&id=' + id; var method = 'GET'; Object.assign(option, { url: url, method: method }); (0, _request2.default)(option, function (error, response, body) { diff --git a/build/config.js b/build/config.js new file mode 100644 index 0000000..e0c9f47 --- /dev/null +++ b/build/config.js @@ -0,0 +1,17 @@ +'use strict'; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +var origin = 'http://music.163.com'; + +var globalOption = { + headers: { + 'Origin': origin, + 'Referer': origin, + 'Content-Type': 'application/x-www-form-urlencoded' + } +}; + +exports.origin = origin; +exports.globalOption = globalOption; \ No newline at end of file diff --git a/build/util.js b/build/util.js new file mode 100644 index 0000000..6800069 --- /dev/null +++ b/build/util.js @@ -0,0 +1,10 @@ +"use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +var deepCopy = function deepCopy(obj) { + return JSON.parse(JSON.stringify(obj)); +}; + +exports.deepCopy = deepCopy; \ No newline at end of file diff --git a/package.json b/package.json index 289d772..265c495 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "build/app.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "babel app.js -o build/app.js" + "build": "babel src/ -d build/" }, "keywords": ["NeteaseCloudMusic","网易云音乐"], "author": "traveller", @@ -17,4 +17,4 @@ "babel-core": "^6.9.1", "babel-preset-es2015": "^6.9.0" } -} \ No newline at end of file +} diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..9799751 --- /dev/null +++ b/src/app.js @@ -0,0 +1,58 @@ +import request from 'request' +import { origin, globalOption } from './config' +import { deepCopy } from './util' + +let api = { + search: (name = null, callback = null, limit = 3, offset = 0) => { + let option = deepCopy(globalOption) + let url = origin + '/api/search/suggest/web' + let form = { + s: name, + limit, + type: 1, + offset + } + let method = 'POST' + let proxy = false + Object.assign(option, { url, form, method, proxy }) + request(option, (error, response, body) => { + if (!error && response.statusCode == 200) { + let info = JSON.parse(body); + callback && callback(JSON.stringify(info, '', 2)) + } else { + console.log(error) + } + }) + }, + song: (id, callback = null) => { + let option = deepCopy(globalOption) + let url = origin + '/api/song/detail?ids=%5B' + id + '%5d' + let method = 'GET' + let proxy = false + Object.assign(option, { url, method, proxy }) + request(option, (error, response, body) => { + if (!error && response.statusCode == 200) { + let info = JSON.parse(body); + callback && callback(JSON.stringify(info, '', 2)) + } else { + console.log(error) + } + }) + }, + lrc: (id, callback = null, lv = -1) => { + let option = deepCopy(globalOption) + let url = origin + '/api/song/lyric?lv=' + lv + '&id=' + id + let method = 'GET' + let proxy = false + Object.assign(option, { url, method, proxy }) + request(option, (error, response, body) => { + if (!error && response.statusCode == 200) { + let info = JSON.parse(body); + callback && callback(JSON.stringify(info, '', 2)) + } else { + console.log(error) + } + }) + } +} +export {api} diff --git a/src/config.js b/src/config.js new file mode 100644 index 0000000..0b692d9 --- /dev/null +++ b/src/config.js @@ -0,0 +1,11 @@ +const origin = 'http://music.163.com' + +const globalOption = { + headers: { + 'Origin': origin, + 'Referer': origin, + 'Content-Type': 'application/x-www-form-urlencoded' + } +} + +export { origin, globalOption } diff --git a/src/util.js b/src/util.js new file mode 100644 index 0000000..b1dced6 --- /dev/null +++ b/src/util.js @@ -0,0 +1,3 @@ +const deepCopy = (obj) => JSON.parse(JSON.stringify(obj)) + +export { deepCopy } diff --git a/test.js b/test.js deleted file mode 100644 index 115bfac..0000000 --- a/test.js +++ /dev/null @@ -1,10 +0,0 @@ -import {api }from './app.js' -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 diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..c5d31af --- /dev/null +++ b/test/test.js @@ -0,0 +1,11 @@ +import { api } from '../src/app.js' + +api.search("年度之歌",(data)=>{ + console.log(data) +}) +api.song('308169',(data)=>{ + console.log(data) +}) +api.lrc('5243023',(data)=>{ + console.log(data) +})