Skip to content

Commit 52c6021

Browse files
committed
update: anonymous_token配置抽离, 生成稳定性问题修复
1 parent 6741d9a commit 52c6021

9 files changed

+26
-20
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ node_modules
66
.history
77
examples/moddef.json
88
bin
9+
anonymous_token

CHANGELOG.MD

+5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
# 更新日志
2+
### 4.11.1 | 2023.09.08
3+
- `anonymous_token` 配置抽离
4+
5+
- `anonymous_token` 生成稳定性问题修复
6+
27
### 4.11.0 | 2023.09.07
38
- 新增 `播客搜索`,`播客上传声音`接口 #1789
49

app.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
#!/usr/bin/env node
2-
const generateConfig = require('./generateConfig')
2+
const fs = require('fs')
3+
const path = require('path')
4+
35
async function start() {
6+
// 检测是否存在 anonymous_token 文件,没有则生成
7+
if (!fs.existsSync('./anonymous_token')) {
8+
fs.writeFileSync(path.resolve(__dirname, 'anonymous_token'), '', 'utf-8')
9+
}
10+
const generateConfig = require('./generateConfig')
411
await generateConfig()
512
require('./server').serveNcmApi({
613
checkVersion: true,

docs/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@
272272
254. 回忆坐标
273273
255. 播客搜索
274274
256. 播客声音上传
275+
257. 验证接口-二维码生成
276+
258. 验证接口-二维码检测
275277

276278
## 安装
277279

@@ -4229,6 +4231,7 @@ type='1009' 获取其 id, 如`/search?keywords= 代码时间 &type=1009`
42294231
42304232
**必选参数:**
42314233
`qr`: `/verify/getQr`接口返回的`qr`字符串
4234+
42324235
## 离线访问此文档
42334236
42344237
此文档同时也是 Progressive Web Apps(PWA), 加入了 serviceWorker, 可离线访问

generateConfig.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
const fs = require('fs')
22
const { register_anonimous } = require('./main')
33
const { cookieToJson } = require('./util/index')
4-
const config = require('./util/config.json')
54
const path = require('path')
65
async function generateConfig() {
76
try {
87
const res = await register_anonimous()
98
const cookie = res.body.cookie
109
if (cookie) {
1110
const cookieObj = cookieToJson(cookie)
12-
let newConfig = { ...config }
13-
newConfig.anonymous_token = cookieObj.MUSIC_A
1411
fs.writeFileSync(
15-
path.resolve(__dirname, 'util/config.json'),
16-
JSON.stringify(newConfig, null, 2),
12+
path.resolve(__dirname, 'anonymous_token'),
13+
cookieObj.MUSIC_A,
1714
'utf-8',
1815
)
1916
}

module/register_anonimous.js

+1-9
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,10 @@ function cloudmusic_dll_encode_id(some_id) {
1111
const digest = crypto.createHash('md5').update(xored).digest()
1212
return digest.toString('base64')
1313
}
14-
function createRandomDeviceId() {
15-
const t = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
16-
const e = 6
17-
let n = ''
18-
for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * e))
19-
return n
20-
}
2114

2215
module.exports = async (query, request) => {
2316
query.cookie.os = 'iOS'
24-
const deviceId = createRandomDeviceId()
17+
const deviceId = `NMUSIC`
2518
const encodedId = Buffer.from(
2619
`${deviceId} ${cloudmusic_dll_encode_id(deviceId)}`,
2720
)
@@ -41,7 +34,6 @@ module.exports = async (query, request) => {
4134
realIP: query.realIP,
4235
},
4336
)
44-
4537
if (result.body.code === 200) {
4638
result = {
4739
status: 200,

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "NeteaseCloudMusicApi",
3-
"version": "4.11.0",
3+
"version": "4.11.1",
44
"description": "网易云音乐 NodeJS 版 API",
55
"scripts": {
66
"start": "node app.js",

util/config.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"anonymous_token": "de91e1f8119d32e01cc73efcb82c0a30c9137e8d4f88dbf5e3d7bf3f28998f21add2bc8204eeee5e56c0bbb8743574b46ca2c10c35dc172199bef9bf4d60ecdeab066bb4dc737d1c3324751bcc9aaf44c3061cd18d77b7a0",
32
"resourceTypeMap": {
43
"0": "R_SO_4_",
54
"1": "R_MV_5_",
@@ -10,4 +9,4 @@
109
"6": "A_EV_2_",
1110
"7": "A_DR_14_"
1211
}
13-
}
12+
}

util/request.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ const { PacProxyAgent } = require('pac-proxy-agent')
55
const http = require('http')
66
const https = require('https')
77
const tunnel = require('tunnel')
8+
const fs = require('fs')
9+
const anonymous_token = fs.readFileSync('./anonymous_token', 'utf-8')
10+
console.log(anonymous_token)
811
const { URLSearchParams, URL } = require('url')
9-
const config = require('../util/config.json')
1012
// request.debug = true // 开启可看到更详细信息
1113

1214
const chooseUserAgent = (ua = false) => {
@@ -72,7 +74,7 @@ const createRequest = (method, url, data = {}, options) => {
7274
if (!options.cookie.MUSIC_U) {
7375
// 游客
7476
if (!options.cookie.MUSIC_A) {
75-
options.cookie.MUSIC_A = config.anonymous_token
77+
options.cookie.MUSIC_A = anonymous_token
7678
}
7779
}
7880
headers['Cookie'] = Object.keys(options.cookie)

0 commit comments

Comments
 (0)