服务启动刷新游客cookie

This commit is contained in:
binaryify 2022-05-29 21:45:48 +08:00
parent 9e5c9372fe
commit b3ca43267a
8 changed files with 47 additions and 9 deletions

View File

@ -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)

11
app.js
View File

@ -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()

View File

@ -523,6 +523,13 @@ v3.30.0 后支持手动传入 cookie,登录接口返回内容新增 `cookie` 字
调用可参考项目文件例子`/public/qrlogin.html` (访问地址:http://localhost:3000/qrlogin.html)
#### 3. 游客登录
说明 : 直接调用此接口, 可获取游客cookie,如果遇到其他接口未登录状态报400状态码需要验证的错误,可使用此接口获取游客cookie避免报错
**接口地址 :** `/register/anonimous`
#### 注意
调用登录接口的速度比调用其他接口慢 , 因为登录过程调用了加密算法

24
generateConfig.js Normal file
View File

@ -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

View File

@ -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)
}

View File

@ -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'

View File

@ -1,6 +1,6 @@
{
"name": "NeteaseCloudMusicApi",
"version": "4.6.0",
"version": "4.6.1",
"description": "网易云音乐 NodeJS 版 API",
"scripts": {
"start": "node app.js",

View File

@ -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
}