python 网易云音乐

import math
import random
from Crypto.Cipher import AES
from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import codecs
import requests
import base64
import rsa


class downmusic(object):
    f="00e0b509f6259df8642dbc35662901477df22677ec152b5ff68ace615bb7b725152b3ab17a876aea8a5aa76d2e417629ec4ee341f56135fccf695280104e0312ecbda92557c93870114af6c9d05c4f7f0c3685b7a46bee255932575cce10b424d813cfe4875d3e82047b97ddef52741d546b8e289dc6935b3ece0462db0a22b8e7"
    e="010001"
    g= "0CoJUm6Qyw8W8jud"
    strs='''
    content-type: application/x-www-form-urlencoded
    cookie: _iuqxldmzr_=32; _ntes_nnid=cb19ce2c34b69416ecbc14b408135460,1552790636492; _ntes_nuid=cb19ce2c34b69416ecbc14b408135460; WM_TID=SaK%2F0APMDZNFERABVUY8g8TC1JG0uMnX; JSESSIONID-WYYY=wWJCh%5CPCwCiC2NAw%5CY1hRYfUVetiDrYuzX5dbU8wubkzrowYl%5C3z%2FoOiIqbmWn3El%2Fzna7WCD%2BuylihabpSnXghsOli62vhRNeKmTWNs5TwRyxthmpED1UR32j8%2F0M8tYQ2NJo11N533Hu9uU17ov05P9y6N7gHWev7yYQ00UDhS6qPI%3A1557235777412; WM_NI=9pUW0to5KVtrdu9a28u6eZMAvkLE0nBYZuRno7mWyQaHV8MfbvR8rh4vpZ1AOXApJPjXHA%2B%2B50ISN7ucK4SHWJ6fIboQ3KzVAgkIOQbHS1AzkSmYuWGslZIVJTm8Jl3fTFA%3D; WM_NIKE=9ca17ae2e6ffcda170e2e6ee8bc172bb97bbb9c1728aa88ab6c55a878a8baaf3628f9797b1d544aa958eb3d32af0fea7c3b92afbb09bb8b380adeb85b7f77aa8abf782b134b7eaabbbd04bb6b584a6b36682879696d166ade9a3b8fc5ba3989ad9d06ea2aa9ed9f164f8aac0bbb44b8d888f91c95caa8888a2aa21f7f087b4cd3db7bd85d5d2449596fa87cc6aad8ffdacf95e83b9a5aace5ba2989dd2c472a98da3baf85d85ada4bbcc5e8a8ca3d9c968f1ba9ca7c437e2a3; playerid=99500353
    origin: https://music.163.com
    referer: https://music.163.com/
    user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36
    '''
    def __init__(self, line):
        super(downmusic, self).__init__()
        self.ids = '{ids: "['+line+']", level: "standard", encodeType: "aac", csrf_token: ""}'
        self.get_random_str=self.get_random_str()
        self.ase_encrypts=self.ase_encrypt(self.ids,self.g)
        params=self.ase_encrypt(self.ase_encrypts.decode('utf-8'),self.get_random_str).decode('utf-8')
        encseckey=self.rsa_encrypt(self.e,self.get_random_str,self.f)
        self.data={'params':params,'encseckey':encseckey}
        self.header=self.gen_headers(self.strs)
   
    def get_random_str(self):
        str='abcdefghigklmnopqretuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789'
        res=''
        for x in range(16):
            index=math.floor(random.random()*len(str))
            res += str[index]
        return res

    def ase_encrypt(self,text,key):
        iv='0102030405060708'
        pad=16-len(text.encode('utf-8'))%16
        text=text+pad*chr(pad)
        key1=str.encode(key)
        encryptor=AES.new(key1,AES.MODE_CBC,str.encode(iv))
        msg=base64.b64encode(encryptor.encrypt(str.encode(text)))
        return msg

    def rsa_encrypt(self,value,text,modulus):
        text=text[::-1]
        rs=int(codecs.encode(text.encode('utf-8'),'hex_codec'),16)**int(value,16)%int(modulus,16)
        return format(rs,'x').zfill(256)

    def gen_headers(self,s):
        ls = s.split('\n')
        lsl = []
        ls = ls[1:-1]
        headers = {}
        for l in ls:
            l = l.split(': ')
            lsl.append(l)
        for x in lsl:
            headers[str(x[0]).strip('    ')] = x[1]
        return headers

    def getdata(self):
        res=requests.post('https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=',data=self.data,headers=self.header)
        print(res.text)
        exit()

obj=downmusic('523946593')

aa=obj.getdata()
print(aa)
# afile=open('./a.txt','r+')
# for line in afile:
#     if(line != ''):
#         ids='{"ids":"['+line.strip()+']","level":"standard","encodeType":"aac","csrf_token":""}'
#         print(ids)
# afile.close()

 

posted @ 2019-05-12 08:27  酷酷的城池  阅读(377)  评论(0编辑  收藏  举报