diff --git a/CHANGELOG.MD b/CHANGELOG.MD index 9b5dacc..6d9cd20 100644 --- a/CHANGELOG.MD +++ b/CHANGELOG.MD @@ -1,4 +1,7 @@ # 更新日志 +### 4.6.1 | 2022.05.29 +- 修复请求接口提示需要验证的问题,增加游客登录接口,服务启动更新游客cookie + ### 4.6.0 | 2022.05.29 - 修复请求接口提示需要验证的问题 [#1541](https://github.com/Binaryify/NeteaseCloudMusicApi/issues/1541) diff --git a/app.js b/app.js index de14190..620e106 100644 --- a/app.js +++ b/app.js @@ -1,4 +1,9 @@ #!/usr/bin/env node -require('./server').serveNcmApi({ - checkVersion: true, -}) +const generateConfig = require('./generateConfig') +async function start() { + await generateConfig() + require('./server').serveNcmApi({ + checkVersion: true, + }) +} +start() diff --git a/docs/README.md b/docs/README.md index 9bd668e..8d0bac9 100644 --- a/docs/README.md +++ b/docs/README.md @@ -523,6 +523,13 @@ v3.30.0 后支持手动传入 cookie,登录接口返回内容新增 `cookie` 字 调用可参考项目文件例子`/public/qrlogin.html` (访问地址:http://localhost:3000/qrlogin.html) + +#### 3. 游客登录 +说明 : 直接调用此接口, 可获取游客cookie,如果遇到其他接口未登录状态报400状态码需要验证的错误,可使用此接口获取游客cookie避免报错 + +**接口地址 :** `/register/anonimous` + + #### 注意 调用登录接口的速度比调用其他接口慢 , 因为登录过程调用了加密算法 diff --git a/generateConfig.js b/generateConfig.js new file mode 100644 index 0000000..e107757 --- /dev/null +++ b/generateConfig.js @@ -0,0 +1,24 @@ +const fs = require('fs') +const { register_anonimous } = require('./main') +const { cookieToJson } = require('./util/index') +const config = require('./util/config.json') + +async function generateConfig() { + try { + const res = await register_anonimous() + const cookie = res.body.cookie + if (cookie) { + const cookieObj = cookieToJson(cookie) + let newConfig = { ...config } + newConfig.anonymous_token = cookieObj.MUSIC_A + fs.writeFileSync( + './util/config.json', + JSON.stringify(newConfig, null, 2), + 'utf-8', + ) + } + } catch (error) { + console.log(error) + } +} +module.exports = generateConfig diff --git a/main.js b/main.js index 3cca747..796298a 100644 --- a/main.js +++ b/main.js @@ -11,7 +11,7 @@ fs.readdirSync(path.join(__dirname, 'module')) if (!file.endsWith('.js')) return let fileModule = require(path.join(__dirname, 'module', file)) let fn = file.split('.').shift() || '' - obj[fn] = function (data) { + obj[fn] = function (data = {}) { if (typeof data.cookie === 'string') { data.cookie = cookieToJson(data.cookie) } diff --git a/module/homepage_dragon_ball.js b/module/homepage_dragon_ball.js index 62280d9..b6a1da7 100644 --- a/module/homepage_dragon_ball.js +++ b/module/homepage_dragon_ball.js @@ -1,8 +1,7 @@ // 首页-发现 dragon ball // 这个接口为移动端接口,首页-发现页(每日推荐、歌单、排行榜 那些入口) // 数据结构可以参考 https://github.com/hcanyz/flutter-netease-music-api/blob/master/lib/src/api/uncategorized/bean.dart#L290 HomeDragonBallWrap -// !需要登录或者匿名登录,非登录返回 [] -const config = require('../util/config.json') +// !需要登录或者游客登录,非登录返回 [] module.exports = (query, request) => { const data = {} query.cookie.os = 'ios' diff --git a/package.json b/package.json index e635fcc..41747a5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "NeteaseCloudMusicApi", - "version": "4.6.0", + "version": "4.6.1", "description": "网易云音乐 NodeJS 版 API", "scripts": { "start": "node app.js", diff --git a/util/request.js b/util/request.js index ab3328a..aa6d6ef 100644 --- a/util/request.js +++ b/util/request.js @@ -43,7 +43,7 @@ const chooseUserAgent = (ua = false) => { ? realUserAgentList[Math.floor(Math.random() * realUserAgentList.length)] : ua } -const createRequest = (method, url, data, options) => { +const createRequest = (method, url, data = {}, options) => { return new Promise((resolve, reject) => { let headers = { 'User-Agent': chooseUserAgent(options.ua) } if (method.toUpperCase() === 'POST') @@ -54,7 +54,7 @@ const createRequest = (method, url, data, options) => { // headers['X-Real-IP'] = '118.88.88.88' if (typeof options.cookie === 'object') { if (!options.cookie.MUSIC_U) { - // 匿名 + // 游客 if (!options.cookie.MUSIC_A) { options.cookie.MUSIC_A = config.anonymous_token }