Skip to content

Commit 60ee927

Browse files
committed
fix: 修复听歌打卡接口 (NeteaseCloudMusicApiEnhanced#167)
1 parent eb07a52 commit 60ee927

4 files changed

Lines changed: 90 additions & 7 deletions

File tree

AGENTS.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Agent Instructions for NeteaseCloudMusicApiEnhanced
2+
3+
## Quick Start
4+
- **Package Manager**: Use `pnpm` (not npm or yarn).
5+
- **Node Version**: Requires Node.js 18 or later.
6+
7+
## Developer Commands
8+
- **Install dependencies**: `pnpm i`
9+
- **Start server**: `pnpm start` or `node app.js`
10+
- **Start dev server**: `pnpm dev` (uses nodemon)
11+
- **Run tests**: `pnpm test` (uses Mocha)
12+
- **Linting**: `pnpm lint` (check) or `pnpm lint-fix` (auto-fix)
13+
14+
## Architecture & Entrypoints
15+
- **Executable Server**: `app.js` is the main entrypoint for running the API server.
16+
- **Module Exports**: `main.js` is the entrypoint when the project is imported as a Node.js dependency.
17+
- **API Endpoints**: Located in the `module/` directory. Each file typically corresponds to an API route.
18+
- **Core Utilities**: Request handling, encryption, and core utilities are found in the `util/` directory.
19+
20+
## Important Gotchas & Quirks
21+
- **Environment Variables**: The server defaults to port 3000 but can be overridden with the `PORT` environment variable.
22+
- **Proxy Variables**: Be very careful with proxy environment variables (`http_proxy`, `https_proxy`, `no_proxy`). The request library (like axios) will automatically pick these up. If they point to an unavailable proxy (especially common in Docker environments), requests will fail silently or throw connection errors.
23+
- **Code Style**: The project uses ESLint and Prettier. Always run `pnpm lint-fix` before committing changes to ensure formatting consistency.

module/scrobble.js

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,40 @@
11
// 听歌打卡
22

33
const createOption = require('../util/option.js')
4-
module.exports = (query, request) => {
5-
const data = {
4+
module.exports = async (query, request) => {
5+
// 注入 os=osx 的 cookie
6+
let cookie = query.cookie || ''
7+
if (typeof cookie === 'object') {
8+
cookie = Object.assign({ os: 'osx' }, cookie)
9+
} else if (typeof cookie === 'string') {
10+
if (cookie.indexOf('os=') > -1) {
11+
cookie = cookie.replace(/os=[^;]+/g, 'os=osx')
12+
} else {
13+
cookie = cookie + '; os=osx'
14+
}
15+
} else {
16+
cookie = 'os=osx'
17+
}
18+
query.cookie = cookie
19+
20+
// 1) startplay → 进「最近播放」
21+
const startplayData = {
22+
logs: JSON.stringify([
23+
{
24+
action: 'startplay',
25+
json: {
26+
id: query.id,
27+
type: 'song',
28+
mainsite: '1',
29+
mainsiteWeb: '1',
30+
content: `id=${query.sourceid}`,
31+
},
32+
},
33+
]),
34+
}
35+
36+
// 2) play → 涨「听歌排行」计数
37+
const playData = {
638
logs: JSON.stringify([
739
{
840
action: 'play',
@@ -15,12 +47,30 @@ module.exports = (query, request) => {
1547
type: 'song',
1648
wifi: 0,
1749
source: 'list',
18-
mainsite: 1,
19-
content: '',
50+
mainsite: '1',
51+
mainsiteWeb: '1',
52+
content: `id=${query.sourceid}`,
2053
},
2154
},
2255
]),
2356
}
2457

25-
return request(`/api/feedback/weblog`, data, createOption(query, 'weapi'))
58+
const option = createOption(query, 'eapi')
59+
option.domain = 'https://clientlog.music.163.com'
60+
61+
// 发送两次请求
62+
const res1 = await request(`/api/feedback/weblog`, startplayData, option)
63+
const res2 = await request(`/api/feedback/weblog`, playData, option)
64+
65+
return {
66+
status: 200,
67+
body: {
68+
code: 200,
69+
data: 'success',
70+
details: {
71+
startplay: res1.body,
72+
play: res2.body,
73+
},
74+
},
75+
}
2676
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@neteasecloudmusicapienhanced/api",
3-
"version": "4.35.1",
3+
"version": "4.35.2",
44
"description": "全网最全的网易云音乐API接口 || A revival project for NeteaseCloudMusicApi Node.js Services (Half Refactor & Enhanced) || 网易云音乐 API 备份 + 增强 || 本项目自原版v4.28.0版本后开始自行维护",
55
"scripts": {
66
"dev": "nodemon app.js",

util/request.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,12 @@ const osMap = {
8888
osver: '16.2',
8989
channel: 'distribution',
9090
},
91+
osx: {
92+
os: 'osx',
93+
appver: '3.1.10.5100',
94+
osver: '15.5',
95+
channel: 'netease',
96+
},
9197
}
9298

9399
// 预先定义userAgentMap
@@ -306,7 +312,11 @@ const createRequest = (uri, data, options) => {
306312
if (cookie.MUSIC_A) header['MUSIC_A'] = cookie.MUSIC_A
307313

308314
headers['Cookie'] = createHeaderCookie(header)
309-
headers['User-Agent'] = options.ua || chooseUserAgent('api', 'iphone')
315+
headers['User-Agent'] =
316+
options.ua ||
317+
(cookie.os === 'osx'
318+
? 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36'
319+
: chooseUserAgent('api', 'iphone'))
310320

311321
if (crypto === 'eapi') {
312322
// headers['x-aeapi'] = true // 服务器会使用gzip压缩返回值

0 commit comments

Comments
 (0)