1. python部分
网易云音乐.py:
# -*- coding:utf-8 -*-
# @Time : 2024/4/15 19:30
# @Author : 快乐的小猴子
# @Version :
# @Function :
import subprocess
from functools import partial
subprocess.Popen = partial(subprocess.Popen, encoding = 'utf-8')
import execjs
import requests
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36",
"Referer": "https://music.163.com/",
}
with open('网易云音乐.js', 'r', encoding='utf-8') as f:
json_data = f.read()
comp = execjs.compile(json_data)
res = comp.call('main')
# print(res)
# exit()
data = {
"params": res["encText"],
"encSecKey": res["encSecKey"],
}
# print(data)
url2 = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token='
resp = requests.post(url2, headers=headers, data=data)
# print(resp, resp.json())
# 取出歌曲url
song_url = resp.json()['data'][0]['url']
# 下载音乐
with open('音乐.mp3', 'wb') as fs:
fs.write(requests.get(song_url, headers=headers).content)
2. JavaScript部分
网易云音乐.js
var CryptoJS = require('crypto-js') // 导入加密库
var {setMaxDigits, RSAKeyPair, encryptedString} = require('./rsa.js')
// 加密函数
function fun(d, e, f, g) {
var h = {}
, i = a(16);
return h.encText = b(d, g),
h.encText = b(h.encText, i),
h.encSecKey = c(i, e, f),
h
}
// 随机值
function a(a) {
var d, e, b = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", c = "";
for (d = 0; a > d; d += 1)
e = Math.random() * b.length,
e = Math.floor(e),
c += b.charAt(e);
return c
}
// params的生成 AES加密
function b(a, b) {
var c = CryptoJS.enc.Utf8.parse(b)
, d = CryptoJS.enc.Utf8.parse("0102030405060708")
, e = CryptoJS.enc.Utf8.parse(a)
, f = CryptoJS.AES.encrypt(e, c, {
iv: d,
mode: CryptoJS.mode.CBC
});
return f.toString()
}
function c(a, b, c) {
var d, e;
return setMaxDigits(131),
d = new RSAKeyPair(b,"",c),
e = encryptedString(d, a)
}
function main() {
var d = '{"ids":"[2144714918]","level":"standard","encodeType":"aac","csrf_token":""}'
var e = '010001'
var f = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
var g = '0CoJUm6Qyw8W8jud'
var bKL9C = fun(d, e, f, g);
return bKL9C
}
说明:网易云音乐.js
中的 rsa.js
文件需要根据RSA第三方库生成,生成方法如下:
访问网址 https://ohdave.com/rsa/ 将图中的内容全部粘贴到rsa.js
文件中,如图