pptv.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import base64
  2. import time
  3. from Crypto.Cipher import PKCS1_v1_5 as Cipher_pkcs1_v1_5
  4. from Crypto.PublicKey import RSA
  5. import math
  6. import random
  7. import execjs
  8. import uuid
  9. import requests
  10. import re
  11. class PPTV:
  12. def __init__(self,url):
  13. self.url = "https://oneplay.api.pptv.com/ups-service/play"
  14. self.video_url = url
  15. self.pk = '\n'.join([
  16. '-----BEGIN PUBLIC KEY-----',
  17. 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqe6XLQF2JmXWgfh09t8TTZsOb6bnj+duiWw4G7pd5Uo1/DN7Xij3Tys9E7XBX0gdXKYI9j+6Fr45bM28fzl4AxUxnhzmbExRt1NJarDGMKo49ViRg1VbL+Wh9kRi+rAxBisdRiP2JEAL+Awqu80chZxxdyoI1k3fSLoZsv/PGkwolE71qsEM4BO1J9RWNp0wlNGqgR+bTwLKkoe7oiZaKaMsSBWNIBDkwgGKFJZzXMXMnqGsDmfbdi32j6hW9DdrxjCx/i9Nzahd1TWVnw9O1AHL5PD5kM3HzqkAewBu38sZxw8DSGYqG0fgVAQtiLHhlD/19F4NKxqL8IVCinMBHQIDAQAB',
  18. '-----END PUBLIC KEY-----'
  19. ])
  20. self.publicKey = RSA.importKey(self.pk)
  21. self.js = execjs.compile(open("./js/pptv.js").read())
  22. self.headers = {
  23. "accept": "*/*",
  24. "accept-encoding": "gzip, deflate, br",
  25. "accept-language": "zh-CN,zh;q=0.9",
  26. "cache-control": "no-cache",
  27. "referer": "https://v.pptv.com/",
  28. "user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_2_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36"
  29. }
  30. def getRamNumber(self, e):
  31. t = ""
  32. for i in range(e):
  33. t += str(hex(math.floor(16 * random.random()))[2:])
  34. return t.upper()
  35. def encrypt(self, message):
  36. cipher = Cipher_pkcs1_v1_5.new(self.publicKey)
  37. cipher_text = base64.b64encode(cipher.encrypt(message))
  38. return cipher_text
  39. def get_cipher(self):
  40. t = self.getRamNumber(48)
  41. i = self.getRamNumber(16)
  42. res = self.encrypt(f"{t},{i}".encode('utf8'))
  43. encryptParams = self.get_3des(self.get_msg(), t, i)
  44. return {"cipher": res.decode(), "encryptParams": encryptParams}
  45. def get_3des(self, msg, key, iv):
  46. return self.js.call("encrypted", msg, key, iv)
  47. def get_msg(self):
  48. js_result = self.js.call("get3rdKeyRandom")
  49. type = 'mhpptv'
  50. appId = 'pptv.web.h5'
  51. appPlt = 'web'
  52. appVer = '1.0.4'
  53. channel = 'sn.cultural'
  54. sdkVer = '1.5.0'
  55. cid = self.get_cid()
  56. allowFt = '0,1,2,3'
  57. rf = '0'
  58. ppi = '302c393939'
  59. o = 'www.google.com'
  60. ahl_ver = '1'
  61. ahl_random = js_result['random_hex']
  62. ahl_signa = js_result['signature_hex']
  63. vvId = uuid.uuid4()
  64. version = '1'
  65. https = 'true'
  66. streamFormat = 3
  67. result = f"type={type}&appId={appId}&appPlt={appPlt}&appVer={appVer}&channel={channel}&sdkVer={sdkVer}&cid={cid}&allowFt={allowFt}&rf={rf}&ppi={ppi}&o={o}&ahl_ver={ahl_ver}&ahl_random={ahl_random}&ahl_signa={ahl_signa}&vvId={vvId}&version={version}&https={https}&streamFormat={streamFormat}"
  68. return result
  69. def get_cid(self):
  70. res = requests.get(self.video_url, headers=self.headers).text
  71. cid = re.findall('var webcfg = {"id":(.*?),', res)
  72. return cid[0]
  73. def get_sign(self):
  74. return self.js.call("get3rdKeyRandom")
  75. def start(self):
  76. result = self.get_cipher()
  77. params = {
  78. "cipher": result.get("cipher"),
  79. "encryptParams": result.get("encryptParams"),
  80. "format": "jsonp",
  81. "cb": f"getPlayEncode_{int(time.time())}"
  82. }
  83. res = requests.get(self.url, params=params, headers=self.headers)
  84. print(res.text)
  85. if __name__ == '__main__':
  86. PPTV().start()