music163.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from binascii import hexlify
  2. import random
  3. from Crypto.Cipher import AES
  4. from base64 import b64encode
  5. from Crypto.Util.Padding import pad
  6. import requests
  7. class music163:
  8. def __init__(self, url):
  9. self.url = url
  10. self.headers = {
  11. "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36¬"
  12. }
  13. self.vid = self.url.split("id=")[-1]
  14. self.second_key = "".join(
  15. random.choices(list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'), k=16))
  16. def get_data(self, vid):
  17. return f'{{"ids":"[{vid}]","level":"standard","encodeType":"aac","csrf_token":""}}'
  18. def get_params(self, key, iv, data):
  19. cryptos = AES.new(key=key.encode(), mode=AES.MODE_CBC, iv=iv.encode())
  20. cryptos1 = AES.new(key=self.second_key.encode(), mode=AES.MODE_CBC, iv=iv.encode())
  21. first_data = b64encode(cryptos.encrypt(pad(data.encode(), 16)))
  22. second_data = cryptos1.encrypt(pad(first_data, 16))
  23. return b64encode(second_data).decode()
  24. def get_encSecKey(self, key):
  25. rs = pow(int(hexlify(key[::-1].encode('utf-8')), 16), 65537,
  26. 157794750267131502212476817800345498121872783333389747424011531025366277535262539913701806290766479189477533597854989606803194253978660329941980786072432806427833685472618792592200595694346872951301770580765135349259590167490536138082469680638514416594216629258349130257685001248172188325316586707301643237607)
  27. return hex(rs)[2:]
  28. def start(self):
  29. params = {
  30. "params": self.get_params("0CoJUm6Qyw8W8jud", "0102030405060708", self.get_data(self.vid)),
  31. "encSecKey": self.get_encSecKey(self.second_key)
  32. }
  33. res = requests.post("https://music.163.com/weapi/song/enhance/player/url/v1?csrf_token=", data=params,
  34. headers=self.headers)
  35. print(res.json())
  36. return res.json()
  37. if __name__ == '__main__':
  38. music163().start()