qqmusic.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import requests
  2. import execjs
  3. from urllib.parse import quote
  4. import json
  5. import re
  6. class qqmusic:
  7. def __init__(self, url):
  8. self.url = url
  9. self.vid = ""
  10. self.headers = {
  11. "Content-Type": "application/x-www-form-urlencoded",
  12. "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¬"
  13. }
  14. self.js = execjs.compile(open("./js/qqmusic.js").read())
  15. self.get_vid()
  16. def get_vid(self):
  17. res = requests.get(self.url).text
  18. vid = re.findall('"songmid":"(.*?)",', res)[0]
  19. self.vid = vid
  20. def get_data(self):
  21. return json.dumps({"req": {"module": "CDN.SrfCdnDispatchServer", "method": "GetCdnDispatch",
  22. "param": {"guid": "12345678", "calltype": 0, "userip": ""}},
  23. "req_0": {"module": "vkey.GetVkeyServer", "method": "CgiGetVkey",
  24. "param": {"guid": "12345678", "songmid": [self.vid], "songtype": [0],
  25. "loginflag": 1, "platform": "20"}},
  26. "comm": {"format": "json", "ct": 24, "cv": 0}})
  27. def join_url_params(self):
  28. params = {
  29. "sign": self.js.call("getSign", self.get_data()),
  30. # "format": "json",
  31. # "inCharset": "utf8",
  32. # "outCharset": "utf-8",
  33. # "notice": "0",
  34. # "platform": "yqq.json",
  35. # "needNewCode": "0",
  36. "data": quote(str(self.get_data()))
  37. }
  38. return f'https://u.y.qq.com/cgi-bin/musics.fcg?sign={params["sign"]}&data={params["data"]}'
  39. def start(self):
  40. res = requests.get(self.join_url_params())
  41. print(res.text)
  42. return res.json()
  43. if __name__ == '__main__':
  44. qqmusic().start()