支持nodejs调用 #773

This commit is contained in:
binaryify 2020-05-18 15:09:39 +08:00
parent e9fe4351b4
commit ff8d57a34a
5 changed files with 67 additions and 1 deletions

View File

@ -1,4 +1,7 @@
# 更新日志 # 更新日志
### 3.31.0 | 2020.05.18
- 支持 `Node.js` 调用,参考`module_example` 文件夹下的 `test.js`
### 3.30.0 | 2020.05.17 ### 3.30.0 | 2020.05.17
- 登录接口返回内容增加`cookie`字段,支持手动传入cookie - 登录接口返回内容增加`cookie`字段,支持手动传入cookie

View File

@ -215,6 +215,29 @@ fetch(url).then(function() {
``` ```
v3.3.0 后支持使用 PAC代理,如 `?proxy=http://192.168.0.1/proxy.pac` v3.3.0 后支持使用 PAC代理,如 `?proxy=http://192.168.0.1/proxy.pac`
## 可以在Node.js调用
v3.31.0后支持Node.js调用,导入的方法为`module`内的文件名,返回内容包含`status``body`,`status`为状态码,`body`为请求返回内容,参考`module_example` 文件夹下的 `test.js`
```js
const { login_cellphone, user_cloud } = require('NeteaseCloudMusicApi')
async function main() {
try {
const result = await login_cellphone({
phone: '手机号',
password: '密码'
})
console.log(result)
const result2 = await user_cloud({
cookie: result.body.cookie // 凭证
})
console.log(result2.body)
} catch (error) {
console.log(error)
}
}
main()
```
## 更新到 v3.0 说明 ## 更新到 v3.0 说明
!>2018.10.14 更新到 3.0.0,使用了模块化机制,因为部分接口参数和 url 做了调整,如还不想升级到 3.0.0,请查看 [v2 的文档](http://binaryify.github.io/NeteaseCloudMusicApi/#/v2), [更新日志](https://github.com/Binaryify/NeteaseCloudMusicApi/blob/master/CHANGELOG.MD), [2.0+下载地址](https://github.com/Binaryify/NeteaseCloudMusicApi/releases/tag/v2.20.5), 同时 2.0+ 将不再维护 !>2018.10.14 更新到 3.0.0,使用了模块化机制,因为部分接口参数和 url 做了调整,如还不想升级到 3.0.0,请查看 [v2 的文档](http://binaryify.github.io/NeteaseCloudMusicApi/#/v2), [更新日志](https://github.com/Binaryify/NeteaseCloudMusicApi/blob/master/CHANGELOG.MD), [2.0+下载地址](https://github.com/Binaryify/NeteaseCloudMusicApi/releases/tag/v2.20.5), 同时 2.0+ 将不再维护

17
main.js Normal file
View File

@ -0,0 +1,17 @@
const fs = require('fs')
const path = require('path')
const request = require('./util/request')
let obj = {}
fs.readdirSync(path.join(__dirname, 'module')).reverse().forEach(file => {
if(!file.endsWith('.js')) return
let fileModule = require(path.join(__dirname, 'module', file))
obj[file.split('.').shift()] = function (data) {
return fileModule({
...data,
cookie: data.cookie ? data.cookie : {}
}, request)
}
})
module.exports = obj

22
module_example/test.js Normal file
View File

@ -0,0 +1,22 @@
const { login_cellphone, user_cloud, album_sublist } = require('../main')
async function test() {
try {
const result = await login_cellphone({
phone: '手机号',
password: '密码'
})
console.log(result)
const result2 = await user_cloud({
cookie: result.body.cookie
})
console.log(result2.body)
const result3 = await album_sublist({
cookie: result.body.cookie
})
console.log(result3.body)
} catch (error) {
console.log(error)
}
}
test()

View File

@ -1,6 +1,6 @@
{ {
"name": "NeteaseCloudMusicApi", "name": "NeteaseCloudMusicApi",
"version": "3.30.0", "version": "3.31.0",
"description": "网易云音乐 NodeJS 版 API", "description": "网易云音乐 NodeJS 版 API",
"scripts": { "scripts": {
"start": "node app.js", "start": "node app.js",
@ -13,6 +13,7 @@
"音乐", "音乐",
"网易云音乐nodejs" "网易云音乐nodejs"
], ],
"main": "main.js",
"husky": { "husky": {
"hooks": { "hooks": {
"pre-commit": "lint-staged" "pre-commit": "lint-staged"