Merge pull request #1 from Binaryify/master

1
This commit is contained in:
LENG小鑫 2018-01-13 23:54:24 +08:00 committed by GitHub
commit 21e33a8a9f
13 changed files with 830 additions and 830 deletions

View File

@ -1,4 +1,21 @@
# 更新日志
### 2.8.3 | 2018.01.12
优化部分功能和文档
### 2.8.2 | 2018.01.05
增加 Dockerfile支持以 Docker 容器模式运行
### 2.8.1 | 2018.01.04
添加了 proxy 功能
### 2.8.0 | 2018.01.04
用 'request' 重写了请求函数
### 2.7.9 | 2017.12.11
更新排行榜接口 , 新增云音乐 ACG 音乐榜 , 云音乐嘻哈榜

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM mhart/alpine-node:8
WORKDIR /app
COPY . /app
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

View File

@ -1,5 +1,5 @@
# 网易云音乐 API
网易云音乐 Node.js API
网易云音乐 Node.js API service
<p>
<a href="https://www.npmjs.com/package/NeteaseCloudMusicApi"><img src="https://img.shields.io/npm/v/NeteaseCloudMusicApi.svg" alt="Version"></a>
<a href="https://www.npmjs.com/package/NeteaseCloudMusicApi"><img src="https://img.shields.io/npm/l/NeteaseCloudMusicApi.svg" alt="License"></a>
@ -17,6 +17,15 @@
## 版本新特性
### 2.8.2 | 2018.01.05
增加Dockerfile支持以Docker容器模式运行
### 2.8.1 | 2018.01.04
添加了proxy功能
### 2.8.0 | 2018.01.04
用 'request' 重写了请求函数
### 2.6.5 | 2017.7.16
优化 CORS 设置
@ -173,6 +182,12 @@ windows 下使用 git-bash 或者 cmder 等终端执行以下命令:
$ set PORT=4000 && node app.js
```
## Docker容器运行
```shell
docker pull pengxiao/netease-music-api
docker run -d -p 3000:3000 pengxiao/netease-music-api
```
## 使用文档
[文档地址](https://binaryify.github.io/NeteaseCloudMusicApi)

34
app.js
View File

@ -1,5 +1,4 @@
const express = require("express");
const http = require("http");
const apicache = require("apicache");
const path = require("path");
@ -7,23 +6,32 @@ const app = express();
let cache = apicache.middleware;
// 跨域设置
// app.all('*', function (req, res, next) {
// if (req.path !== '/' && !req.path.includes('.')) {
// res.header('Access-Control-Allow-Credentials', true)
// // 这里获取 origin 请求头 而不是用 *
// res.header('Access-Control-Allow-Origin', req.headers['origin'] || '*')
// res.header('Access-Control-Allow-Headers', 'X-Requested-With')
// res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
// res.header('Content-Type', 'application/json;charset=utf-8')
// }
// next()
// })
app.all("*", function(req, res, next) {
if (req.path !== "/" && !req.path.includes(".")) {
res.header("Access-Control-Allow-Credentials", true);
// 这里获取 origin 请求头 而不是用 *
res.header("Access-Control-Allow-Origin", req.headers["origin"] || "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Content-Type", "application/json;charset=utf-8");
}
next();
});
const onlyStatus200 = (req, res) => res.statusCode === 200;
app.use(cache("2 minutes", onlyStatus200));
app.use(express.static(path.resolve(__dirname, "public")));
app.use(function(req, res, next) {
const proxy = req.query.proxy;
if (proxy) {
req.headers.cookie = req.headers.cookie + `__proxy__${proxy}`;
}
next();
});
// 获取专辑内容
app.use("/album", require("./router/album"));
@ -248,7 +256,7 @@ app.use("/user/record", require("./router/user_playrecord"));
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`server running @${port}`);
console.log(`server running @ http://localhost:${port}`);
});
module.exports = app;

File diff suppressed because it is too large Load Diff

View File

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

41
public/test.html Normal file
View File

@ -0,0 +1,41 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>test</title>
</head>
<body>
<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
<script>
$.ajax({
url: "http://localhost:3000/login/cellphone?phone=phone&password=password",
xhrFields: {
withCredentials: true
},
success: function (data) {
console.log(data)
$.ajax({
url: "http://localhost:3000/recommend/songs ",
xhrFields: {
withCredentials: true
},
success: function (data) {
console.log(data)
},
error: function (err) {
console.log(err)
}
})
},
error: function (err) {
console.log(err)
}
})
</script>
</body>
</html>

View File

@ -1,27 +1,27 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");
router.get('/', (req, res) => {
const rid = req.query.id
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
router.get("/", (req, res) => {
const rid = req.query.id;
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
offset: req.query.offset || 0,
rid: rid,
limit: req.query.limit || 20,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
"music.163.com",
`/weapi/v1/resource/comments/R_SO_4_${rid}/?csrf_token=`,
'POST',
"POST",
data,
cookie,
music_req => {
res.send(music_req)
res.send(music_req);
},
err => res.status(502).send('fetch error')
)
})
err => res.status(502).send(err.message)
);
});
module.exports = router
module.exports = router;

View File

@ -1,34 +1,34 @@
const express = require('express')
const crypto = require('crypto')
const router = express()
const { createWebAPIRequest } = require('../util/util')
const express = require("express");
const crypto = require("crypto");
const router = express();
const { createWebAPIRequest } = require("../util/util");
router.get('/', (req, res) => {
const phone = req.query.phone
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
const md5sum = crypto.createHash('md5')
md5sum.update(req.query.password)
router.get("/", (req, res) => {
const phone = req.query.phone;
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const md5sum = crypto.createHash("md5");
md5sum.update(req.query.password);
const data = {
phone: phone,
password: md5sum.digest('hex'),
rememberLogin: 'true'
}
password: md5sum.digest("hex"),
rememberLogin: "true"
};
createWebAPIRequest(
'music.163.com',
'/weapi/login/cellphone',
'POST',
"music.163.com",
"/weapi/login/cellphone",
"POST",
data,
cookie,
(music_req, cookie) => {
// console.log(music_req)
cookie = cookie&&cookie.map(x => x.replace("Domain=.music.163.com", ""))
cookie =
cookie && cookie.map(x => x.replace("Domain=.music.163.com", ""));
res.set({
'Set-Cookie': cookie
})
res.send(music_req)
"Set-Cookie": cookie
});
res.send(music_req);
},
err => res.status(502).send('fetch error')
)
})
err => res.status(502).send("fetch error")
);
});
module.exports = router
module.exports = router;

View File

@ -1,36 +1,29 @@
const http = require('http')
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')
const http = require("http");
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");
router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
let detail, imgurl
router.get("/", (req, res) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
id: req.query.id,
offset: 0,
total: true,
limit: 1000,
n: 1000,
csrf_token: ''
}
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
'/weapi/v3/playlist/detail',
'POST',
"music.163.com",
"/weapi/v3/playlist/detail",
"POST",
data,
cookie,
music_req => {
console.log(music_req)
// detail = music_req
res.send(music_req)
// mergeRes()
// console.log(music_req)
res.send(music_req);
},
err => {
res.status(502).send('fetch error')
res.status(502).send("fetch error");
}
)
);
// FIXME:i dont know the api to get coverimgurl
// so i get it by parsing html
@ -62,6 +55,6 @@ router.get('/', (req, res) => {
// res.send(detail)
// }
// }
})
});
module.exports = router
module.exports = router;

View File

@ -1,24 +1,27 @@
const express = require('express')
const router = express()
const { createWebAPIRequest } = require('../util/util')
const express = require("express");
const router = express();
const { createWebAPIRequest } = require("../util/util");
router.get('/', (req, res) => {
const cookie = req.get('Cookie') ? req.get('Cookie') : ''
router.get("/", (req, res) => {
const cookie = req.get("Cookie") ? req.get("Cookie") : "";
const data = {
offset: 0,
offset: req.query.offset || 0,
uid: req.query.uid,
limit: 1000,
csrf_token: ''
}
limit: req.query.limit || 30, //貌似无效
csrf_token: ""
};
createWebAPIRequest(
'music.163.com',
'/weapi/user/playlist',
'POST',
"music.163.com",
"/weapi/user/playlist",
"POST",
data,
cookie,
music_req => res.send(music_req),
err => res.status(502).send('fetch error')
)
})
music_req => {
res.send(music_req);
// console.log(JSON.parse(music_req))
},
err => res.status(502).send("fetch error")
);
});
module.exports = router
module.exports = router;

View File

@ -1,32 +1,33 @@
const assert = require('assert')
const crypto = require('crypto')
const { createWebAPIRequest } = require('../util/util')
const assert = require("assert");
const crypto = require("crypto");
const { createWebAPIRequest } = require("../util/util");
describe('测试获取歌曲是否正常', () => {
it('歌曲的 url 不应该为空', done => {
const id = 347230
const br = 999000
describe("测试获取歌曲是否正常", () => {
it("歌曲的 url 不应该为空", done => {
const id = 462791935;
const br = 999000;
const data = {
ids: [id],
br: br,
csrf_token: ''
}
const cookie = ''
csrf_token: ""
};
const cookie = "";
createWebAPIRequest(
'music.163.com',
'/weapi/song/enhance/player/url',
'POST',
"music.163.com",
"/weapi/song/enhance/player/url",
"POST",
data,
cookie,
music_req => {
console.log(JSON.parse(music_req).data[0].url)
assert(!!JSON.parse(music_req).data[0].url)
done()
console.log(music_req);
console.log(JSON.parse(music_req).data[0].url);
assert(!!JSON.parse(music_req).data[0].url);
done();
},
err => {
done(err)
done(err);
}
)
})
})
);
});
});

View File

@ -1,32 +1,35 @@
const Encrypt = require('./crypto.js')
const http = require('http')
const querystring = require('querystring')
const Encrypt = require("./crypto.js");
const request = require("request");
const querystring = require("querystring");
// request.debug = true;
function randomUserAgent() {
const userAgentList = [
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36',
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1',
'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
'Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36',
'Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89;GameHelper',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4',
'Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:46.0) Gecko/20100101 Firefox/46.0',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)',
'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)',
'Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586',
'Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1'
]
const num = Math.floor(Math.random() * userAgentList.length)
return userAgentList[num]
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
"Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B143 Safari/601.1",
"Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
"Mozilla/5.0 (Linux; Android 5.1.1; Nexus 6 Build/LYZ28E) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Mobile Safari/537.36",
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_3_2 like Mac OS X) AppleWebKit/603.2.4 (KHTML, like Gecko) Mobile/14F89;GameHelper",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_5) AppleWebKit/603.2.4 (KHTML, like Gecko) Version/10.1.1 Safari/603.2.4",
"Mozilla/5.0 (iPhone; CPU iPhone OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:46.0) Gecko/20100101 Firefox/46.0",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:46.0) Gecko/20100101 Firefox/46.0",
"Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)",
"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Win64; x64; Trident/6.0)",
"Mozilla/5.0 (Windows NT 6.3; Win64, x64; Trident/7.0; rv:11.0) like Gecko",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/13.10586",
"Mozilla/5.0 (iPad; CPU OS 10_0 like Mac OS X) AppleWebKit/602.1.38 (KHTML, like Gecko) Version/10.0 Mobile/14A300 Safari/602.1"
];
const num = Math.floor(Math.random() * userAgentList.length);
return userAgentList[num];
}
function createWebAPIRequest(
host,
path,
@ -36,94 +39,66 @@ function createWebAPIRequest(
callback,
errorcallback
) {
let music_req = ''
const cryptoreq = Encrypt(data)
const http_client = http.request(
{
hostname: host,
console.log(cookie);
const proxy = cookie.split("__proxy__")[1];
cookie = cookie.split("__proxy__")[0];
const cryptoreq = Encrypt(data);
const options = {
url: `http://${host}${path}`,
method: method,
path: path,
headers: {
Accept: '*/*',
'Accept-Language': 'zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4',
Connection: 'keep-alive',
'Content-Type': 'application/x-www-form-urlencoded',
Referer: 'http://music.163.com',
Host: 'music.163.com',
Accept: "*/*",
"Accept-Language": "zh-CN,zh;q=0.8,gl;q=0.6,zh-TW;q=0.4",
Connection: "keep-alive",
"Content-Type": "application/x-www-form-urlencoded",
Referer: "http://music.163.com",
Host: "music.163.com",
Cookie: cookie,
'User-Agent': randomUserAgent()
}
"User-Agent": randomUserAgent()
},
function(res) {
res.on('error', function(err) {
errorcallback(err)
})
res.setEncoding('utf8')
if (res.statusCode != 200) {
createWebAPIRequest(host, path, method, data, cookie, callback)
return
} else {
res.on('data', function(chunk) {
music_req += chunk
})
res.on('end', function() {
if (music_req == '') {
createWebAPIRequest(host, path, method, data, cookie, callback)
return
}
if (res.headers['set-cookie']) {
callback(music_req, res.headers['set-cookie'])
} else {
callback(music_req)
}
})
}
}
)
http_client.write(
querystring.stringify({
body: querystring.stringify({
params: cryptoreq.params,
encSecKey: cryptoreq.encSecKey
})
)
http_client.end()
}),
proxy: proxy
};
request(options, function(error, res, body) {
if (error) {
errorcallback(error);
} else {
callback(body, res.headers["set-cookie"]);
}
});
}
function createRequest(path, method, data, callback, errorcallback) {
function createRequest(path, method, data) {
return new Promise((resolve, reject) => {
let ne_req = ''
const http_client = http.request(
{
hostname: 'music.163.com',
const options = {
url: `http://music.163.com${path}`,
method: method,
path: path,
headers: {
Referer: 'http://music.163.com',
Cookie: 'appver=1.5.2',
'Content-Type': 'application/x-www-form-urlencoded',
'User-Agent': randomUserAgent()
Referer: "http://music.163.com",
Cookie: "appver=1.5.2",
"Content-Type": "application/x-www-form-urlencoded",
"User-Agent": randomUserAgent()
}
},
res => {
res.setEncoding('utf8')
res.on('error', err => {
reject(err)
})
res.on('data', chunk => {
ne_req += chunk
})
res.on('end', () => {
resolve(ne_req)
})
};
if (method.toLowerCase() === "post") {
options.body = data;
}
)
if (method == 'POST') {
http_client.write(data)
request(options, function(error, res, body) {
if (error) {
reject(error);
} else {
resolve(body);
}
http_client.end()
})
});
});
}
module.exports = {
createWebAPIRequest,
createRequest
}
};