youku.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import time
  2. import requests
  3. import re
  4. from hashlib import md5
  5. import json
  6. class YouKu:
  7. def __init__(self, url):
  8. self.url = "https://acs.youku.com/h5/mtop.youku.play.ups.appinfo.get/1.1/"
  9. self.int_time = int(time.time()) * 1000
  10. # self.vid = "XNTQwMTgxMTE2"
  11. self.video_url = url
  12. # 用于存储show_id,videoId
  13. self.params = {}
  14. self.get_current_showid()
  15. self.cookie = ''
  16. self.cookie_dict = {}
  17. self.language = {
  18. "ja": "日语",
  19. "guoyu": "国语",
  20. "default": "默认",
  21. "yue": "粤语",
  22. }
  23. def get_current_showid(self):
  24. headers = {
  25. "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"
  26. }
  27. res = requests.get(self.video_url, headers=headers).text
  28. current_showid = re.findall("id_(.*?).html", res)[0]
  29. # video_id = re.findall("videoId: '(\d+)'", res)[0]
  30. self.params = {"show_id": current_showid}
  31. def get_steal_params(self):
  32. return json.dumps({
  33. "ccode": "0502",
  34. "client_ip": "192.168.1.1",
  35. "utid": re.findall("cna=(.*?);", self.cookie)[0],
  36. "client_ts": self.int_time,
  37. "version": "2.1.63",
  38. "ckey": "DIl58SLFxFNndSV1GFNnMQVYkx1PP5tKe1siZu/86PR1u/Wh1Ptd+WOZsHHWxysSfAOhNJpdVWsdVJNsfJ8Sxd8WKVvNfAS8aS8fAOzYARzPyPc3JvtnPHjTdKfESTdnuTW6ZPvk2pNDh4uFzotgdMEFkzQ5wZVXl2Pf1/Y6hLK0OnCNxBj3+nb0v72gZ6b0td+WOZsHHWxysSo/0y9D2K42SaB8Y/+aD2K42SaB8Y/+ahU+WOZsHcrxysooUeND",
  39. })
  40. def get_biz_params(self):
  41. return json.dumps({
  42. "vid": re.findall("id_(.*?).html", self.video_url)[0],
  43. "play_ability": "16782592", # 写死在js里的
  44. "current_showid": self.params["show_id"],
  45. "preferClarity": "4", # 貌似是清晰度
  46. "extag": "EXT-X-PRIVINF", # 写死在js里的
  47. "master_m3u8": "1",
  48. "media_type": "standard,subtitle",
  49. "app_ver": "2.1.63",
  50. "drm_type": "19",
  51. "key_index": "web01",
  52. })
  53. def get_ad_params(self):
  54. return json.dumps({
  55. "vs": "1.0",
  56. "pver": "2.1.63",
  57. "sver": "2.0",
  58. "site": 1,
  59. "aw": "w",
  60. "fu": 0,
  61. "d": "0",
  62. "bt": "pc",
  63. "os": "mac",
  64. "osv": "",
  65. "dq": "auto",
  66. "atm": "",
  67. "partnerid": "null",
  68. "wintype": "interior",
  69. "isvert": 0,
  70. "vip": 0,
  71. "p": 1,
  72. "rst": "mp4",
  73. "needbf": 2,
  74. "avs": "1.0",
  75. })
  76. def get_data(self):
  77. return json.dumps({"steal_params": self.get_steal_params(), "biz_params": self.get_biz_params(),
  78. "ad_params": self.get_ad_params()})
  79. def join_params(self):
  80. data = self.get_data()
  81. return {
  82. 'jsv': '2.5.8',
  83. 'appKey': '24679788',
  84. 't': self.int_time,
  85. 'sign': md5(str(
  86. re.findall("m_h5_tk=(.*?)_", self.cookie)[0] + "&" + str(self.int_time) + "&" + "24679788" + "&" + str(
  87. data)).encode("utf8")).hexdigest(),
  88. 'api': 'mtop.youku.play.ups.appinfo.get',
  89. 'v': '1.1',
  90. 'timeout': '20000',
  91. 'YKPid': '20160317PLF000211',
  92. 'YKLoginRequest': 'true',
  93. 'AntiFlood': 'true',
  94. 'AntiCreep': 'true',
  95. 'type': 'jsonp',
  96. 'dataType': 'jsonp',
  97. 'callback': 'mtopjsonp3',
  98. "data": f"{data}"
  99. }
  100. def loads_jsonp(self, _jsonp):
  101. try:
  102. return json.loads(re.match(".*?({.*}).*", _jsonp, re.S).group(1))
  103. except:
  104. raise ValueError('Invalid Input')
  105. def start(self):
  106. headers = {
  107. "Accept": "*/*",
  108. "Host": "acs.youku.com",
  109. "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",
  110. "cookie": self.cookie,
  111. "Referer": "https://v.youku.com/"
  112. }
  113. res = requests.get(self.url, params=self.join_params(), headers=headers)
  114. print(res.text)
  115. if __name__ == '__main__':
  116. YouKu().start()