[Python] 纯文本查看 复制代码
#coding=utf-8
import base64
import codecs
import math
import os
import random
from Crypto.Cipher import AES
import requests
class get_args():
def __init__(self,dict):
self.a_16 = self.a(16)
self.dict = dict
def a(slef,a):
b="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
c=""
for i in range(a):
e=random.random()*len(b)
e=math.floor(e)
c+=b[e]
return c
# AES加密
def to_16(self,key):
while len(key) % 16 !=0:
key += '\0'
return key.encode('utf-8')
def AES_encrypt(self,text,key,iv): ##text为密文,key为公钥,iv为偏移量
pad2 = lambda s: s + (16 - len(s) % 16) * chr(16 - len(s) % 16)
data=pad2(text)
encryptor = AES.new(self.to_16(key), AES.MODE_CBC, self.to_16(iv))
encrypt_aes = encryptor.encrypt(data.encode('utf-8'))
encrypt_text = base64.encodebytes(encrypt_aes)
enctext = encrypt_text.decode('utf-8')
return enctext
def get_params(self):#song为歌曲名 返回值为params
iv="0102030405060708"
g = '0CoJUm6Qyw8W8jud'
i=self.a_16
encText=str(self.dict)#i8a
return self.AES_encrypt(self.AES_encrypt(encText,g, iv), i, iv)
def RSA_encrypt(self,text, pubKey, modulus):
text=text[::-1]
rs=int(codecs.encode(text.encode('utf-8'), 'hex_codec'), 16) ** int(pubKey, 16) % int(modulus, 16)
return format(rs, 'x').zfill(256)
def get_encSecKey(self):
i = self.a_16
b = "010001"
c = '00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7'
return self.RSA_encrypt(i, b, c)
class music():
def __init__(self):
self.headers2 = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
}
self.headers = {
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Accept': '*/*',
'Referer': 'https://music.163.com/search/',
'Connection': 'keep-alive',
'cookie': '_iuqxldmzr_=32; _ntes_nnid=2c2050b762a20bec42d44b4ec9570db2,1596545803404; _ntes_nuid=2c2050b762a20bec42d44b4ec9570db2; WM_TID=P%2B9S%2BGxp32pBVAVRRQZqCt7Cgd%2BGver%2B; NMTID=00OeaJw_6w3Xsbamkk4mtz6A4aQ648AAAF0w8UCbA; WM_NI=uJgfo1tpOzmOzCi1zZbOYbXBDNPDlNewZXU3yQ3iumqfEggri%2BRhKdTEiIHOU2SXI3IuFjOG%2FKSi%2FAeBDlsY02AdcF3eDGC7oT5jbtTM7WD72FNddwZuZ8YelJFmI76NRXY%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6eeb6f0659a86fca5cc6093928fa6d14e968e9eaeb547898f8190d24686b7a0a7cb2af0fea7c3b92ab796a987ee3bb8b0819bf045ba9da7d2f952a2919783db4a8892a0a5ef46a7a996b9d839bb8ffd8af867b3ba8b88e67aabf59e89ca70989ba7aacb4597ee9db9cc398bab8a8bce4ded959d8aae4ba79e8c8df253f4ee8f85e24ef1bb8ca8c23dad8cfb8be1809893f9b8c73df29a8285e77d9aab96d9dc6fb0968a8ecc3df6929eb9dc37e2a3; JSESSIONID-WYYY=C1rSDXpSvPOpS%2FNHFyKe%5Cw3yY4%2F5bq5ntcfQ2QEqxSCJxkrD1%5CYeJdAGWIQmJpUIY5%5CtjA3S%2FrCedOHlKjFkPy5X4Eje7wt%2BHzgrcVRV%2BmXMRO8G%5CsxeUd9MBwwvqzIuEbuJkpv9mv6bCvQBzI9bI1qQl8WnFzFvy5Z0SqVeIAGYexqB%3A1601034012504'
} # 请求头
self.url='https://music.163.com/weapi/cloudsearch/get/web?csrf_token='
s= input('请输入想要听的歌:')
self.arg = {'csrf_token': "",'hlposttag':'<span class="s-fc\">','limit':'30','offset':'0','s':'{0}'.format(s),'total':'true','type':'1' }
self.class_arg = get_args(self.arg)
self.params={
'params':self.class_arg.get_params(),
'encSecKey':self.class_arg.get_encSecKey()
}
self.song = []
self.singer = []
self.id = []
self.dt = []
def search(self):
response = requests.post(self.url, headers=self.headers, data=self.params)
dict = response.json()
music_list = dict['result']['songs']
for music in music_list:
self.song.append(music['name'])
self.singer.append(music['ar'][0]['name'])
self.id.append(music['id'])
self.dt.append(music['dt'])
self.select()
def select(self):
ip = 0
for song, singer, id, dt in zip(self.song, self.singer, self.id, self.dt):
min = int(int(dt)/1000/60)
seconds = int(int(dt)/1000%60)
time = str(min)+":"+str(seconds)
print(ip,song,singer,time)
ip += 1
ip = input("请输入序号:")
ip = int(ip)
song = self.song[ip]
singer = self.singer[ip]
id = self.id[ip]
self.download(song,singer,id)
def download(self,song,singer,id):
url = 'https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token='
arg = {'csrf_token': '', 'encodeType': 'aac', 'ids': "[" + '{0}'.format(id) + "]",
'level': 'standard'}
class_arg = get_args(arg)
params = {
'params': class_arg.get_params(),
'encSecKey': class_arg.get_encSecKey()
}
response = requests.post(url, headers=self.headers, data=params)
dict = response.json()
url = dict['data'][0]['url']
music = requests.get(url, headers=self.headers2).content
dir = os.getcwd()
dir = os.path.join(dir, "网易云音乐 ")
if not os.path.exists(dir):
os.mkdir(dir) # 构造文件夹
os.chdir(dir) # 将下载的歌曲存储在该文件夹
print(song, singer)
file_name = song + '-' + singer + '.m4a' # 文件名
with open(file_name, 'wb') as f:
f.write(music)
print("下载成功!")
if __name__ == '__main__':
music = music()
music.search()